Running the .bat file located in% AppData% from the context menu

When I add an entry to the Windows Explorer context menu using registry entries like this:

[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell]
[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Similar Files]
[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Similar Files\command]
 @="%AppData%\\FindAlike\\AddRightClickFile.bat  \"%1\""

      

I am getting the error

Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access the item.

      

If I copy the AddRightClickFile.bat file to C: \ Windows \ System32 and change the registry entries to

[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell]
[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Similar Files]
[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Similar Files\command]
@="AddRightClickFile.bat  \"%1\""

      

no error occurs. However, I would like to store AddRightClickFile.bat in% Appdata% \ FindAlike.

Code in AddRightClickFile.bat

reg add  "HKEY_CURRENT_USER\Software\FindAlike"  /f /v "TestFilePath" /t REG_SZ /d  %1

      

Is there a way to make the .bat file run from the context menu command by saving it in the% AppData% subfolder?

EDIT

I created a registry key in code using the following code:

RegistryKey rk = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Classes\*\shell\Similar Files\command");
        string sValue = @"%AppData%\FindAlike\AddRightClickFile.bat ""%1""";
        rk.SetValue("",sValue, RegistryValueKind.ExpandString);

      

and set AddRightClickFile.bat like this:

start  
reg add  "HKEY_CURRENT_USER\Software\FindAlike"  /f /v "RightClickFileName"       /t REG_SZ /d  %1 
exit 0

      

This works OK, but creates a command window in the directory of the file I right click on. Looking at the processes with the task manager, I see that the cmd.exe and conhost.exe processes are being created. If I remove the command window, both processes disappear. Is there a way to end these processes automatically without killing them by name, which could have unwanted consequences.

+3


source to share


2 answers


The main problem is that it is %AppData%

stored as a reference to a variable, but when the registry value is read, the variable is not expanded to its value.

This is because the value (default)

for the registry key is of type REG_SZ

. If you want to keep a reference to a variable and automatically expand its value, you need the registry key to be of type REG_EXPAND_SZ

.

So you have two options

  • Don't use a variable, use the full path.

  • Change the type of the registry key.

The first option has no problems, but the second one cannot be made from regedit

. From the command line, you can use



reg.exe add "HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Similar Files\command" /f /ve /t REG_EXPAND_SZ /d "\"%^AppData%\FindAlike\AddRightClickFile.bat\" \"%1\""

      

or from a batch file

    reg.exe add "HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Similar Files\command" ^
        /f /ve /t REG_EXPAND_SZ ^
        /d "\"%%AppData%%\FindAlike\AddRightClickFile.bat\" \"%%1\""

      

note: The only difference between both is the escaping of the variable references

+3


source


I don't see the need to expand the variable at all at runtime, so just enter the command as:

In a batch file:

@REG ADD "HKCU\Software\Classes\*\shell\Similar Files\command" /VE /D "\"%APPDATA%\FindAlike\AddRightClickFile.bat\" \"%%~1\"" /F>NUL

      

On the command line:

REG ADD "HKCU\Software\Classes\*\shell\Similar Files\command" /VE /D "\"%APPDATA%\FindAlike\AddRightClickFile.bat\" \"%~1\"" /F>NUL

      



[EDIT]

If that's all you put into yours AddRightClickFile.bat

, you can simply bypass that file entirely and enter the information directly as a command to run:

From the batch file:

@REG ADD "HKCU\Software\Classes\*\shell\Similar Files\command" /VE /D "CMD /C \"REG ADD \"HKCU\Software\FindAlike\" /V \"TestFilePath\" /D \"\\\"%%L\\\"\" /F^>NUL\"" /F>NUL

      

+1


source







All Articles