How to know which file was right-clicked on?

0

I'm trying to write a script and add it to the right click context menu. To add the items to the menu, I've used this: Windows: How to add batch-script action to Right Click menu, except instead of

HKEY_CLASSES_ROOT\Directory\shell\MyScript1

I've used

HKEY_CLASSES_ROOT\*\shell\MyScript1

so they would appear on all files, regardless of the extension.

Now, in the actual bat file, I need a way to know which file was right-clicked on, so I can use it as an input. Is there any way to put the full path of the file in a variable for later use?

user304822

Posted 2014-03-04T00:29:17.013

Reputation: 113

Answers

0

If an argument passed to a batch script is a file, you can use environment variables to get things such as path, file name, file extension, etc. For example:

@echo off
::
::If no parameter is passed, exit.
::
if [%~1]==[] echo Missing parameter! Script will exit... && exit /b

::
::If file does not exist, exit.
::
if not exist "%~1" echo File does not exist. Are you sure "%~1" is a file? && exit /b

echo.
echo File info
echo ---------
echo Path: %~dp1
echo Name: %~n1
echo Extension: %~x1
echo.
pause
exit /b

This works regardless of you dragging the file to the script or pass it as a parameter from the command line. You can learn more about arguments and their extended syntax at SS64.com.

JSanchez

Posted 2014-03-04T00:29:17.013

Reputation: 1 582

1Great. I've still got a lot to learn. Thank you. – user304822 – 2014-03-04T03:41:11.067