2

Running Windows 8.1. This worked a few days ago. Really do not know what has changed, nor am I aware of any update that might have stopped this working.

When I run a .bat (batch) file (right-click > Open or double-click) the batch file is displayed in Notepad++ and does NOT run. If I load a command prompt and manually run the .bat file from inside the command prompt, the batch file runs perfectly.

I've carefully checked these two articles:

How do I restore .bat files association with the system (make them run when double-clicked)? https://stackoverflow.com/questions/4905708

and nothing makes a difference. Specifically:

From a command prompt:

  • assoc .bat returns .bat=batfile
  • ftype batfile returns batfile="%1" %*

After everything I've tried, the PC has been rebooted with no difference. This is driving me crazy...why won't batch files run any more?

AlainD
  • 4,447
  • 15
  • 49
  • 96
  • 4
    I would assume the .bat extension is no longer linked to the command prompt, and notepad++ may open any textfile by default. If you rename the .bat file to .cmd, does it work? If so, look into how the .cmd is setup and restore that for the .bat filetype as well. – LPChip Mar 18 '19 at 20:59
  • What do you have for `assoc .cmd` ? Use `assoc .cmd=batfile` if required (in an elevated CMD). – harrymc Mar 18 '19 at 21:00
  • also, running those commands on my system gives me the same results. I would say the problem lies somewhere else. – LPChip Mar 18 '19 at 21:02
  • 1
    @LPChip: Good test! I renamed the `.bat` file to `.cmd` and it worked! From the command line I typed `assoc .cmd` and `.cdm=cmdfile` was returned. Am now in the process and work out what the differences are between `HKEY_CLASSES_ROOT\batfile` and `HKEY_CLASSES_ROOT\cmdfile`. Weird thought, but will *all* my batch files work if rename them from `.bat` to `.cmd`? – AlainD Mar 18 '19 at 21:16
  • Yes, you can rename a .bat to .cmd and it will work. .bat files are executed slightly differently than .cmd but in 99% of the cases you will never have to deal with this. .bat is basically using some logic from back in the windows 16bit days, where cmd is not. .cmd is mostly backwards compatible though, but it are small nuances that are different. Its worth fixing though, its cmd that knows by extension what execution type to use so all you have to do is getting .bat open with .cmd again. – LPChip Mar 18 '19 at 21:24
  • 1
    Also, in the registry under HK_CR, look for the .bat and .cmd too. Especially for subfolders rather than associations – LPChip Mar 18 '19 at 21:25
  • @LPChip: Thanks. I've now made exactly one change to `[HKEY_CLASSES_ROOT\batfile\shell\Open\command]`. This was to change the value from `@="\"C:\\Windows\\System32\\cmd.exe\" \"%1\""` to `@="\"%1\" %*"`. Batch files now work...which is great but I'm beginning to doubt my sanity. I could have sworn that I checked this and tried that exact same registry setting as I was going round the loop! It feels like there are some hidden Windows settings here... – AlainD Mar 18 '19 at 21:35
  • It looks like this was solved in the comments. Another cause is when "Hide extensions for known file types" is checked in Windows Explorer Folder Options, and the real extension for a file is hidden, but a fake one is visible. That is, myfile.txt showing, but myfile.txt.exe is the real name of the file. This technique is used for malware. In your case, it's reversed, so that yourfile.exe is really yourfile.exe.txt and thus opens in the text editor. – Christopher Hostage Mar 18 '19 at 21:39
  • 1
    @ChristopherHostage: That is not the case here. Hiding file extensions is off and the files in question were definitely `.bat` files. Am curious why Microsoft even have a `Hide file extensions` option. Why would it be sensible to allow the user to accidentally run `DoSomething.txt.exe`? Its just a recipe for accidental errors or security risks. Should never be an option - the full filename with extension should be displayed. – AlainD Mar 18 '19 at 21:46
  • Was suffering from same. [This](https://stackoverflow.com/a/20114887) worked for me.(see my comment too) – Nagarjuna Borra Apr 14 '23 at 10:36

2 Answers2

2

Open the "classic" control panel: Win + R keys: Control
(View by: small icons)
All control Panel Items -> Default Programs -> Associate a file type or protocol with a program (Set Associations):
- Find .BAT in the list -> Change Program (more options) -> Look for another app on this PC
- Choose C:\Windows\System32\cmd.exe

KidACrimson
  • 394
  • 1
  • 7
  • 23
  • 1
    Hmm, good thought. This did change the action...but it now loads the command prompt pointing at the folder containing the batch file. This did not actually run the batch file itself. – AlainD Mar 18 '19 at 21:22
  • You can try making a shortcut _to_ the batch file then changing properties of the "run in" to make it launch where you want..? I'd also try putting @ECHO ON and pause commands in the batch script to keep the CMD.exe window open. – KidACrimson Mar 19 '19 at 02:06
1

First thing you want to do is verify if it is a problem in the entire cmd structure tree, or just .bat files.

If you rename the .bat file to .cmd, does it work? If so, compare .bat and .cmd and see what's different.

Good test! I renamed the .bat file to .cmd and it worked! From the command line I typed assoc .cmd and .cdm=cmdfile was returned. Am now in the process and work out what the differences are between HKEY_CLASSES_ROOT\batfile and HKEY_CLASSES_ROOT\cmdfile.

Awesome. So now we need to fix the .bat extension. You can look at how the .cmd extension is setup and set that for the .bat extension as well.

I executed the other answer, but now an empty command prompt is opening when I doubleclick the .bat file, the actual batfile is not running though.

Okay, so you have reset the association with .bat files.

You should look into the HKEY_CLASSES_ROOT .bat and .cmd keys too and especially check the subfolders.

I've now made exactly one change to [HKEY_CLASSES_ROOT\batfile\shell\Open\command]. This was to change the value from @="\"C:\Windows\System32\cmd.exe\" \"%1\"" to @="\"%1\" %*". Batch files now work...which is great but I'm beginning to doubt my sanity. I could have sworn that I checked this and tried that exact same registry setting as I was going round the loop! It feels like there are some hidden Windows settings here...

No, you basically reset the key when executing the other answer, and now it really does solve the problem. This is really fixed. :)

LPChip
  • 59,229
  • 10
  • 98
  • 140