Home > Blockchain >  ".exe\",0" Was unexpected at this time." Error when using parentheses in file n
".exe\",0" Was unexpected at this time." Error when using parentheses in file n

Time:04-03

I am trying to register my application in the windows registry but there it seems that there is something wrong in my code:

@echo off

set "APP=MyApp"
set "APP_NAME=App"
set "APP_PATH=D:\proj 100\1\test 1(64-bit).exe"
set "APP_ICON=\"%APP_PATH%\",0"
set "APP_ARGS=\"%APP_PATH%\" \"%%1\""

if not exist "%APP_PATH%" (

echo ERROR: "%APP_PATH%" not found.

) else (


reg add "HKLM\Software\Classes\%APP%HTML" /v "" /t REG_SZ /d "%APP_NAME% Document" /f
reg add "HKLM\Software\Classes\%APP%HTML\DefaultIcon" /v "" /t REG_SZ /d "%APP_ICON%" /f
reg add "HKLM\Software\Classes\%APP%HTML\shell\open\command" /v "" /t REG_SZ /d "%APP_ARGS%" /f

)

pause

I keep getting this error

.exe\",0" was unexpected at this time.

Edit: I had changed the file name output after testing the code, the original name was test 1(64-bit).exe, so apparently it only happens when the file name/path has parentheses. I tried to escape the characters but it did not work.

CodePudding user response:

The posted batch code did not produce the posted error message as I executed it on my Windows computer. However, the following batch code would be better:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "APP_FILE=D:\proj 100\1\test 1.exe"
if not exist "%APP_FILE%" echo ERROR: "%APP_FILE%" not found.& exit /B 1

set "RegExe=%SystemRoot%\System32\reg.exe"
if exist %SystemRoot%\Sysnative\reg.exe set "RegExe=%SystemRoot%\Sysnative\reg.exe"

set "APP=MyApp"
set "APP_NAME=App"
set "APP_ICON=\"%APP_FILE%\",0"
set "APP_ARGS=\"%APP_FILE%\" %%1"

%RegExe% ADD "HKLM\Software\Classes\%APP%HTML" /f /ve /t REG_SZ /d "%APP_NAME% Document" >nul
if errorlevel 1 echo ERROR: %~nx0 must be run as administrator.& exit /B
%RegExe% ADD "HKLM\Software\Classes\%APP%HTML\DefaultIcon" /f /ve /t REG_SZ /d "%APP_ICON%" >nul
%RegExe% ADD "HKLM\Software\Classes\%APP%HTML\shell\open\command" /f /ve /t REG_SZ /d "%APP_ARGS%" >nul

endlocal

There is first defined the environment variable APP_FILE and then checked the existence of this file with printing an error message to standard output handle and exiting the processing of the batch file with error exit code 1 on application executable not existing.

The batch file is designed to work on 32-bit and 64-bit Windows and runs on 64-bit Windows always 64-bit reg.exe in directory %SystemRoot%\System32 even on batch file being processed by 32-bit cmd.exe in directory %SystemRoot%\SysWOW64. See the Microsoft documentation pages:

  1. WOW64 Implementation Details
  2. File System Redirector
  3. Registry Keys Affected by WOW64

There is used the option /ve instead of /v "" as explained by the usage help output on running reg add /? in a command prompt window.

The double quotes around %1 are removed because of Windows File Explorer adds itself double quotes to the argument string passed to the executable if that is necessary like on argument string containing a space character. However, it would be also possible to use:

set "APP_ARGS=\"%APP_FILE%\" \"%%1\""

The batch file adds on success to the Windows registry:

REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MyAppHTML]
@="App Document"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MyAppHTML\DefaultIcon]
@="\"D:\\proj 100\\1\\test 1.exe\",0"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MyAppHTML\shell]

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MyAppHTML\shell\open]

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MyAppHTML\shell\open\command]
@="\"D:\\proj 100\\1\\test 1.exe\" %1"

The batch file above does not work correct on fully qualified file name assigned to the environment variable APP_FILE contains an ampersand & like with a path C:\Temp\Development & Test(!)\test 1.exe as used in the batch file below using delayed variable expansion for handling such an unusual path, too.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "APP_FILE=C:\Temp\Development & Test(!)\test 1.exe"
if not exist "%APP_FILE%" echo ERROR: "%APP_FILE%" not found.& exit /B 1

set "RegExe=%SystemRoot%\System32\reg.exe"
if exist %SystemRoot%\Sysnative\reg.exe set "RegExe=%SystemRoot%\Sysnative\reg.exe"

setlocal EnableDelayedExpansion
set "APP=MyApp"
set "APP_NAME=App"
set "APP_ICON=\"!APP_FILE!\",0"
set "APP_ARGS=\"!APP_FILE!\" %%1"
rem set "APP_ARGS=\"!APP_FILE!\" \"%%1\""

%RegExe% ADD "HKLM\Software\Classes\%APP%HTML" /f /ve /t REG_SZ /d "%APP_NAME% Document" >nul
if errorlevel 1 echo ERROR: %~nx0 must be run as administrator.& exit /B
%RegExe% ADD "HKLM\Software\Classes\%APP%HTML\DefaultIcon" /f /ve /t REG_SZ /d "!APP_ICON!" >nul
%RegExe% ADD "HKLM\Software\Classes\%APP%HTML\shell\open\command" /f /ve /t REG_SZ /d "!APP_ARGS!" >nul

endlocal
endlocal

Please note that neither if nor echo nor exit modify the value of dynamic variable ERRORLEVEL and for that reason the second exit /B results also in an exit of batch file processing with error exit code 1 as set by reg.exe.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • exit /?
  • if /?
  • reg /?
  • reg add /?
  • rem /?
  • set /?
  • setlocal /?

See also:

  • Related