Search code examples
batch-fileregistry

Create a registry entry command to open a program its parent directory with the clicked file as an argument


I managed to make a batch file which creates registry keys to open a file with a certain extension by double-clicking.

The launch takes place in the directory where the clicked file is located. I need the program to be launched in the directory where %pathtoexe% is located, and pass the full path to the clicked file as an argument, instead.

for %%A in ("%~dp0.") do set "folder=%%~dpA"

set "ftypename=release"
set "extension=.pmp"
set "pathtoexe=%folder%TranslationUpdater.exe"
set "pathtoicon=%~dp0file_type_favicon.ico"

REG ADD HKCU\SOFTWARE\Classes\%extension% /ve /d "%ftypename%" /f
REG ADD HKCU\SOFTWARE\Classes\%ftypename%\DefaultIcon /ve /d "%pathtoicon%" /f
reg add HKCU\SOFTWARE\Classes\%ftypename%\Shell\Open\Command /ve /d ""%pathtoexe%" "%%1"" /f

ftype %ftypename%="%pathtoexe%" "%%1"
assoc %extension%=%ftypename%

So the registry command line need to hold something like this...

cd %folder%
start %pathtoexe% %1

Solution

  • You can modify the Command key in the registry to use the cmd command to change the directory and start the program.

    for %%A in ("%~dp0.") do set "folder=%%~dpA"
    
    set "ftypename=release"
    set "extension=.pmp"
    set "pathtoexe=%folder%TranslationUpdater.exe"
    set "pathtoicon=%~dp0file_type_favicon.ico"
    
    REG ADD HKCU\SOFTWARE\Classes\%extension% /ve /d "%ftypename%" /f
    REG ADD HKCU\SOFTWARE\Classes\%ftypename%\DefaultIcon /ve /d "%pathtoicon%" /f
    reg add HKCU\SOFTWARE\Classes\release\Shell\Open\Command /ve /d "\"C:\\Windows\\System32\\cmd.exe\" /c cd /d \"C:\\Users\\WDAGUtilityAccount\\Desktop\\New folder\\TranslationUpdater\" && start \"\" \"C:\\Users\\WDAGUtilityAccount\\Desktop\\New folder\\TranslationUpdater\\TranslationUpdater.exe\" \"%1\"" /f
    
    
    ftype %ftypename%="%pathtoexe%" "%%1"
    assoc %extension%=%ftypename%