2

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.

DaveJol2
  • 97
  • 5
  • I'd suggest that the `\"` sequence should be removed from each of the variables in which it is used, and that `%%1` should then be `"%%1"`, but I've not tried it - I don't play games with the registry. – Magoo Apr 02 '22 at 08:53
  • Put `set APP_` after the variable assignments and post the output ([edit] the question for that)… – aschipfl Apr 02 '22 at 09:42
  • @aschipfl Thank you it seems that it only happens when i use parenthesis in the file name – DaveJol2 Apr 02 '22 at 19:41

1 Answers1

1

The posted batch code does not work because of ) in the file name in the command line with all environment variable references already expanded

reg add "HKLM\Software\Classes\MyAppHTML\DefaultIcon" /v "" /t REG_SZ /d "\"D:\proj 100\1\test 1(64-bit).exe\",0" /f

is interpreted as end of the else command block because of being outside a double quoted argument string. For the Windows command processor the first argument string after REG option /d is "\" and so D:\proj 100\1\test 1(64-bit) is outside of a double quoted argument string.
See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?

The following batch code avoiding command blocks would be better:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "APP_FILE=D:\proj 100\1\test 1(64-bit).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(64-bit).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(64-bit).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! 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(64-bit).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.

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

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

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143