-1

When I double any batch file (even something as simple as "echo test") in the explorer or from anywhere else (windows 10 ONLY in my user account). I get this error message:

The filename, directory name, or volume label syntax is incorrect.

If I right click the batch file and run it as an admin, it will run with no problem. Opening a cmd.exe and running the file with ./test.bat will also works. It seem it's a problem with the file associations?

If I run assoc to get the ProgId, the entry with .bat is the following:

.bat=batfile

If I check the executable associated with the batfile ProgId (ftype | find "batfile), I get this:

batfile="%1" %*

I've tried changing the executable associated with the batfile ProgId to

cmd.exe /c "%1" %*

in the registry (HKEY_CLASSES_ROOT\batfile\shell\open\command) and it does seem to work, but is it really right answer to this?

I have also used procmon to see the difference with the fix and without it.

With cmd.exe as progId

Event Path: *C:\Users\User\test.bat*\
Process Path: *C:\WINDOWS\system32\cmd.exe*\
Process Command: *"cmd.exe" /c "C:\Users\User\test.bat*\

Without it (default)

Event Path: *C:\Windows\System32\"C:\Users\User\test.bat"*\
Process Path: *C:\WINDOWS\system32\svchost.exe*\
Process Command: *C:\WINDOWS\system32\svchost.exe -k netsvcs -p -s Appinfo*
Eea Manco
  • 11
  • 1
  • 4
  • Your settings are correct. Please edit your question to include the code you're using so that we can tell you which line needs Administrator privileges. – SomethingDark Aug 26 '23 at 00:23
  • @SomethingDark no lines need administrator privileges. I'm simply printing test with echo. – Eea Manco Aug 26 '23 at 00:35
  • It sounds like your script was saved with a coding other than ANSI and you need to resave it. My settings are also `.bat=batfile` and `batfile="%1" %*` and scripts work correctly for me. – SomethingDark Aug 26 '23 at 00:42
  • @SomethingDark The script was saved in UTF-8. The problem does not lie in any of the scripts I'm trying to run. This is happening on all batch files in my, the ones created by me or the ones that came with programs in my PC. – Eea Manco Aug 26 '23 at 00:43
  • Perhaps it is a problem with the `PATH` environment variable. Carefully review all the values in such a variable. – Aacini Aug 26 '23 at 02:54
  • Okay, once more. You have yourself corrupted the *Windows Command Processor* configuration by adding to registry hive of your user account an `AutoRun` registry value which `cmd.exe` always executes on being started by __any__ process visible for you with opening a console window or invisible in background without opening a console window in the execution environment of your account as long as not using the __CMD__ option `/D`. The usage of __Run as administrator__ results in starting `cmd.exe` in a different execution environment not using your *Command Processor* settings. – Mofi Aug 26 '23 at 15:28
  • The solution is pressing __Win+R__ on the keyboard (Windows logo key + R) and enter `cmd.exe /D` for opening a command prompt window and ignoring the `AutoRun` registry value as explained by the usage help of __CMD__ output on running next `cmd /?` in the command prompt window. Then run `%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\Microsoft\Command Processor" /v AutoRun`. There is output an `AutoRun` registry value of type `REG_SZ` or `REG_EXPAND_SZ` while by Windows default is output the error message: `ERROR: The system was unable to find the specified registry key or value.` – Mofi Aug 26 '23 at 15:33
  • You can see most likely on the output which application corrupted your *Windows Command Processor* configuration by adding an `AutoRun` registry value with a command line executed by `cmd.exe` running with your account without option `/D`. There is most likely a batch file in the command line string which results in the error message you always see on running `cmd.exe` with your account. Run now `%SystemRoot%\System32\reg.exe Delete "HKCU\Software\Microsoft\Command Processor" /v AutoRun /f` and the malicious `AutoRun` registry value is deleted and `cmd` works normally. – Mofi Aug 26 '23 at 15:42
  • Now with *Command Processor* configuration being back to Windows default, at least regarding to the `AutoRun` registry value, please click on gray displayed __Delete__ link of your question because of being off-topic for Stack Overflow according to the reasons described in the help topics [What types of questions should I avoid asking?](https://stackoverflow.com/help/dont-ask) and [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) Thank you. – Mofi Aug 26 '23 at 15:42

1 Answers1

-2

That depends if your bat script need to be share with any end-user for deployment purpose or if it will deploy as task job. Anyway, reliable option to fix privilege issue is to transform the .bat file into portable .exe file. This link can be used how to create .exe via visual basic. Create a standalone exe file in Visual Studio 2019

  • 2
    Not only is that completely irrelevant to the question, turning the script into an exe file just makes it a self-extracting archive that keeps it as a script but runs it from `%TEMP%`. – SomethingDark Aug 26 '23 at 00:40
  • 1
    **1.** Your answer is not related to the problem posted. **2.** Converting a .bat file to an .exe is _never_ good advice; It has been repeatedly proven to cause several problems. **3.** The link posted has nothing to do with converting a .bat file to an .exe. – Aacini Aug 26 '23 at 03:03