1

A .bat script that launches a program stopped working today on Windows 10 Pro 21H1.

Investigating, the batch file contains start "" "%~dp0winnfsd" -log %2 -pathFile %3 -id %4 %5 which filled in is:

start "" "C:\Users\Peter\.vagrant.d\gems\2.6.7\gems\vagrant-winnfsd-1.4.0\bin\winnfsd" -log on -pathFile "C:/Users/Peter/.vagrant.d/nfspaths" -id 0 0

When this is executed from a batch file or command prompt, today it gives the error:

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

Press Enter or Esc to exit...

If I remove start "" and run the command from the command prompt it works:

"C:\Users\Peter\.vagrant.d\gems\2.6.7\gems\vagrant-winnfsd-1.4.0\bin\winnfsd" -log on -pathFile "C:/Users/Peter/.vagrant.d/nfspaths" -id 0 0

And after reading https://stackoverflow.com/a/8351295/119750 I modified the batch file to contain this and it works:

start cmd /c call "C:\Users\Peter\.vagrant.d\gems\2.6.7\gems\vagrant-winnfsd-1.4.0\bin\winnfsd.exe" -log on -pathFile "C:/Users/Peter/.vagrant.d/nfspaths" -id 0 0

I don't understand what's going on here or why the first has stopped working.

  1. Why would the first command not work/stop working?
  2. Why does calling cmd make a difference?
aschipfl
  • 33,626
  • 12
  • 54
  • 99
PeterB
  • 2,212
  • 2
  • 21
  • 33
  • 2
    I suggest to run in a command prompt window `%SystemRoot%\System32\where.exe start`. The expected output would be the error message: `INFO: Could not find files for the given pattern(s).` But it is possible that `where.exe` finds like `cmd.exe` a file with name `start` in current directory or any directory listed in __local__ environment variable `PATH` with a file extension listed in __local__ environment variable `PATHEXT` and for that reason this executable or script file is executed instead of using the internal command `start` of `cmd.exe`. – Mofi Sep 07 '21 at 17:04
  • 2
    I am curious as to where this message is coming from? `Press Enter or Esc to exit` – Squashman Sep 07 '21 at 17:05
  • 1
    Some off-topic hints: `verify other 2>nul` and `if errorlevel 1 echo Unable to enable command extensions` as well as the comments above are unnecessary. The batch file uses `%~dp0` which can be used only on interpreter is `cmd.exe` and command extensions are enabled. So the batch file does not work with `COMMAND.COM` of MS-DOS, Windows 95/98/ME. Therefore the comments and these two command lines can be removed and `setlocal enableextensions` should be better extended to `setlocal EnableExtensions DisableDelayedExpansion` to define together with `@echo off` completely the required environment. – Mofi Sep 07 '21 at 17:18
  • 1
    `for /f` and `CALL :LoCase %result` work also only with `cmd.exe` and with enabled command extensions, whereby the percent sign should be removed, i.e. the command line should be `CALL :LoCase result`. The `@` left to `@set result=%%y` is also unnecessary as the command echo mode is disabled already with the first line `@echo off`. – Mofi Sep 07 '21 at 17:21
  • 1
    `tasklist` should be better replaced by `%SystemRoot%\System32\tasklist.exe` to specify this executable with full qualified file name. The usage of this executable means that Windows XP or a newer version of Windows is required as `tasklist.exe` is by default not available on Windows 2000 and former versions of Windows. Not good is `if %1==status` as better would be `if /I "%~1" == "status"`. `if %1==start` is also better written as `if /I "%~1" == "start"` and `if %1==halt` should be written as `if /I "%~1" == "halt"`. – Mofi Sep 07 '21 at 17:24
  • 1
    Then all `exit` with an exit code value should be modified to `exit /B` and the exit code value, i.e. `exit 0` to `exit /B 0` and `exit 1` to `exit /B 1`. `GOTO:EOF` should be modified to `GOTO :EOF` with a space between argument 0 - the command `GOTO` and its first (and only) argument - the predefined label `:EOF`. That syntax can be seen also in help of __GOTO__ output on running `goto /?` in a command prompt window and also in help of command __CALL__ output on running `call /?`. – Mofi Sep 07 '21 at 17:33
  • 1
    However, the entire subroutine `LoCase` and its call with `CALL :LoCase result` would not be needed at all on using `if /I "%result%" == "winnfsd.exe"` for a case-insensitive string comparison instead of `if "%result%"=="winnfsd.exe"`. It would be even better to use `%SystemRoot%\System32\tasklist.exe /NH /FI "IMAGENAME eq winnfsd.exe" 2>nul | %SystemRoot%\System32\find.exe /I "winnfsd.exe" >nul` and as next line `if not errorlevel 1 (` instead of `if "%result%"=="winnfsd.exe" (`. So no need for the `for /f` loop and for the environment variable `result` and the subroutine `LoCase`. – Mofi Sep 07 '21 at 17:40
  • @Mofi "I suggest to run in a command prompt window `%SystemRoot%\System32\where.exe start`. " Well that I didn't expect: `c:\Program Files\Git\usr\bin\start` no wonder it's messed up – PeterB Sep 07 '21 at 19:28
  • @Squashman good question: this appears in the new command window that's launched by the Bat file. Adding "pause" into the batch script, nothing is shown. – PeterB Sep 07 '21 at 19:30
  • @Mofi thanks for your extensive answers; seems there's much to improve. After my last comment I tested further. In a command prompt if I run `start /?` I see the help so am confused why `where` goes for the Git-provided binary? And that binary doesn't work if I substitute the whole path into the command `"c:\Program Files\git\usr\bin\start" "" "C:\Users\Peter\.vagrant.d\gems\2.6.7\gems\vagrant-winnfsd-1.4.0\bin\winnfsd.exe" -log on -pathFile "C:/Users/Peter/.vagrant.d/nfspaths" -id 0 0` so confused what is going on still. Can I force the batch script to use the in-built `start`? – PeterB Sep 07 '21 at 19:40
  • 1
    There is no possibility as far as I know to force the usage of an internal command of `cmd.exe` rather than the usage of an executable or script found by `cmd.exe` with same name as an internal command. I think, you have only the possibilities to rename the file `start` in `C:\Program Files\git\usr\bin` or make sure in the batch file that a folder containing a file with name `start` found by `%SystemRoot%\System32\where.exe` is not in __local__ `PATH`. – Mofi Sep 08 '21 at 06:17
  • 1
    Your batch file sets up already a local environment which means you can use after `setlocal EnableExtensions DisableDelayedExpansion` the command line `set "PATH=%SystemRoot%\System32"` to redefine the __local__ `PATH` with just the single folder path really needed by your batch file. After `exit /B 0` or `exit /B 1` the previous environment with default __local__ `PATH` is restored and so this temporary redefinition of `PATH` helps to avoid that `C:\Program Files\git\usr\bin\start` is found at all by `cmd.exe` and the batch file is additionally executed a bit faster as less folders searched. – Mofi Sep 08 '21 at 06:22

0 Answers0