When you double-click a Windows batch file (.bat
) or a .cmd
file, one of the following symptoms may happen:
- The error “Windows cannot find ‘filename.bat’. Make sure you typed the name correctly, and then try again.” occurs when you run a .bat or a .cmd file.
- The batch file opens a Command Prompt window, but its commands don’t execute.
- Notepad or any other text editor opens the batch file.
Cause
The user may have inadvertently associated .bat
files with a text editor, or associated .bat files with cmd.exe
, or tampered with the settings in the registry. Once you set a file association for .bat
files using the Open with dialog or default programs, there is no way to revert to the default setting using the user interface. The only way to fix it is by changing the settings in the registry.
The error Windows cannot find ‘filename.cmd’ or ‘filename.bat’ occurs if the COMSPEC environment variable is incorrect.
Resolution
Windows Batch file (.bat
) and .cmd files are special file types that you can run or automate commands. The Command Prompt reads and interprets the batch/cmd file and runs each command specified in the file.
Step 1: Fix the COMPSPEC variable
If you receive the error “Windows cannot find ‘filename” when running a .bat or a .cmd file, fix the COMSPEC variable.
Open an admin Command Prompt window and run:
setx ComSpec %SystemRoot%\system32\cmd.exe /m
Press Enter.
You should see the message SUCCESS: Specified value was saved in the output.
Exit the Command Prompt window.
The above step should fix the error “Windows cannot find ‘filename’ when running .bat or .cmd files.
Step 2: Fix the .BAT file association
To fix symptoms 2 & 3 explained earlier, the .bat file association must be repaired.
Method 1: Use .BAT/.cmd file association registry fix
- Download the .bat fix or .cmd fix for Windows 10/11 from the file association fixes page.
- Unzip the archive and double-click the enclosed registry file.
- Click Yes when you’re asked for confirmation to continue.
- Click OK.
Method 2: Fix the .BAT file association manually
- Start the Registry Editor (
regedit.exe
) - Go to the following key:
HKEY_CLASSES_ROOT\.bat
- Set the
(default)
value data tobatfile
- Go to the following key:
HKEY_CLASSES_ROOT\batfile\shell
- In the right-pane, make sure that the
(default)
value data is not set. It should read asvalue not set
. In case you see some other text there, then right-click the(default)
value and chooseDelete
. - Then, proceed to the following key:
HKEY_CLASSES_ROOT\batfile\shell\open\command
- Double-click
(default)
and set its value data to:"%1" %*
- Go to the following branch:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.bat
The values present under one of its subkeys (
OpenWithList
,OpenWithProgids
andUserChoice
) may be causing the problem. - Right-click on the
.bat
key, and chooseDelete
. ClickYes
when prompted for confirmation. Don’t worry! This registry key and its three subkeys are trivial, and they’re only required if you want to override the.bat
file association defaults. - Exit the Registry Editor.
Automate the above steps using REG file
To automate the steps listed in Method 2 using a REG file, do the following..
- Open Notepad.
- Copy the following lines and paste them into Notepad.
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.bat] @="batfile" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\batfile\shell] @=- [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\batfile\shell\open\command] @="\"%1\" %*" [-HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.bat]
- Save the file with any name having the
.reg
extension, sayfix_bat.reg
- Double-click
fix_bat.reg
to apply the configuration in the registry. Click Yes when prompted for confirmation.
That’s it! You’ve now fixed the batch file association settings. Batch files should now execute correctly when double-clicked.
One small request: If you liked this post, please share this?
One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:- Pin it!
- Share it to your favorite blog + Facebook, Reddit
- Tweet it!
thanks for the information.
i got rid of the problem through the reset this pc icon
i bet your solution would work if i followed your method.
I followed your manual registry method. All the keys were already correct, so all I did was delete the .bat key as instructed. It had no effect. *.bat files still don’t execute in Win10. The just open in notepad.
@vikingvista: Can you send me the FileTypeDiag report for .bat files?
If your batch file is running something like python, you may have to add it to the system environment tables.
Hi
I have another Question
I open a bat file with notepad and now all of my bat file open with note pad How I can make my bat file RUN on my windows???
I use windows 7
still not working
I tried all the methods, still my windows batch file doesn’t open. The command prompt either flashes and goes off or nothing happens when I try to open the file. Can you help please?
I was able to open the file before a few weeks
@Viswes: Create a batch file with the following contents:
Run the file. What happens now?
command prompt opens saying
hi
press any key to continue . . .
@Viswes: That means the .bat file association is not the issue here. Adding the
Pause
command in between your batch file code would tell what’s happening when the file runs.It’s a file downloaded from the internet and it runs correctly for others,
is there any other thing I can do open the file. Thank you so much for your help.
I tried to follow the steps, but they are not working. Furthermore, now also other software have problems. How can I ripristinate the subkeys (OpenWithList, OpenWithProgids and UserChoice) that I deleted?
Thank you.
Matteo
@Matteo:
Here’s the default registry entries for .bat under Fileexts
The
UserChoice
key doesn’t exist by default.I tried all of these (including the .[@echo hi / @pause] bat file and bat files still open in a text editor. Anything else I can try?
@Riley: Can you send me a FileTypeDiag report for .bat file? This can be fixed.
Well, after trying all of these methods to modify the regedit, it didn’t work. I’m sure I did something wrong. The problem is the system think the bat file is a text file. Simply rename it in explorer, will not work. But if you use the command to rename it back to the *.bat, it will work.
CMD into the directory where you have the bat file. You will see your bat file has an extension like *.bat.txt. Use the command ren file.bat.txt file.bat. It should rename it back to the correct *.bat file format. Now it works… for me.
I have had this issue for a while and I knew something had changed the ability to run commands and batch files so I eventualy got around to it today.
I never thought it would be so simple!
IU merely copied your REG file commands into a txt doc and saved it as fixbat.reg. I ran the file and hey presto, the batchfile I had been trying to run just worked right away!
THANK YOU!
This article does not help. I don’t have .bat associated with a text editor and the shell window just opens for a split second. Where’s the explanation and workaround for “The batch file opens a Command Prompt window but its commands doesn’t execute.”?
@Gebbi: Can you send me the .bat file association details. You can generate it using FileTypeDiag
The registry keys are not there.
HKEY_CLASSES_ROOT\batfile\shell and HKEY_CLASSES_ROOT\batfile\shell\open\command don’t exist.
@Mary: If they don’t exist, you need to create them using any of those three methods.
Hi, I am getting this message while running .bat file it says “Windows cannot find ‘C\Program Files\MPV\mpv-install.bat’. Make sure you have typed the name correctly, and then try again.
The instructions seem appropriate and clear. Can you tell me how I can resolve this same issue, but with Windows 11?
@Peter: The instructions on this page should apply to Windows 11 as well.
You were the only one that solved my problem. Thank you so much!!