How to receive a parameter in a batch file

I have asked a question, Use an environment variable to point to an "Open With" program, which received a fantastic answer. To add myapp.exe, residing in %MYAPPSDIR%, to the Open With context menu, I should write in the CLI

reg add "HKCR\Applications\myapp.exe\shell\open\command" /ve /t REG_EXPAND_SZ /d "\"^%MYAPPSDIR^%\myapp\myapp.exe\" \"^%1\"" /f

Now I want to have a .bat file, receiving a parameter, which executes that sentence. E.g., executing regadd.bat myapp.exe would do it. What should the line in the .bat file be?

The points are:

  1. How to include the parameter,
  2. How to include the parameter with .exe removed (for the directory part),
  3. How to avoid expanding %1 that goes into the registry (I do not know how to escape it appropriately).

I found many resources (e.g., How to use `%` in a batch file), but I wouldn't like to tinker with the registry. My bet for 1 and 3 is (still have to work out 2):

reg add "HKCR\Applications\%1\shell\open\command" /ve /t REG_EXPAND_SZ /d "\"^%MYAPPSDIR^%\(%1 with .exe removed)\%1\" \"^%1\"" /f

I could also set the .bat to be run as regadd.bat myapp. In that case, the line would be

reg add "HKCR\Applications\%1.exe\shell\open\command" /ve /t REG_EXPAND_SZ /d "\"^%MYAPPSDIR^%\%1\%1.exe\" \"^%1\"" /f

I am interested in both options.


Solution 1:

Batch interpreter parses command line in slightly different manner than pure command line parser (read answers to another question at StackOverflow):

  • command line: to avoid expanding %MYAPPSDIR% by command line interpreter, the % percent character should be escaped by the standard CLI escape character (^ caret): ^%MYAPPSDIR^% or ^%1;
  • .bat script: to avoid expanding %MYAPPSDIR% or %1 by batch interpreter, the % percent character should be doubled as follows: %%MYAPPSDIR%% and %%1, respectively.

To distinguish parameters supplied to your batch script:

set "par1=%~1"
if "%par1%"=="" (
  echo no parameter supplied
  goto :eof
) else (
  set "par=%par1:.exe=%"
)
if "%par%"=="%par1%" (
  echo .exe not present
) else (
  echo .exe present
)

or (maybe better)

set "par1=%~1"
if "%par1%"=="" (
  echo no parameter supplied
  goto :eof
) else (
  set "par=%par1:~-4%"
)
if /I "%par%"==".exe" (
  echo .exe present
) else (
  echo .exe not present
)

or (maybe the best)

set "par1=%~1"
if "%par1%"=="" (
  echo no parameter supplied
  goto :eof
) else (
  set "par=%~x1"
)
if /I "%par%"==".exe" (
  echo .exe present
) else (
  echo .exe not present
)

Resources (required reading):

  • (command reference) An A-Z Index of the Windows CMD command line
  • (additional particularities) Windows CMD Shell Command Line Syntax
  • (%~1 etc. special page) Command Line arguments (Parameters)
  • (%par1:~-4% substring) Variables: extract part of a variable
  • (%par1:.exe=%) Variable Edit/Replace

Solution 2:

How to do I use the parameter with .exe removed (for the directory part)?

You can use %~n1.

This expands %1 to a file name without the file extension.


Parameter Extensions

When an argument is used to supply a filename then the following extended syntax can be applied:

We are using the variable %1 (but this works for any parameter)

  • %~f1 - Expand %1 to a Fully qualified path name - C:\utils\MyFile.txt

  • %~d1 - Expand %1 to a Drive letter only - C:

  • %~p1 - Expand %1 to a Path only e.g. \utils\ this includes a trailing \ which will be interpreted as an escape character by some commands.

  • %~n1 - Expand %1 to a file Name without file extension C:\utils\MyFile or if only a path is present (with no trailing backslash) - the last folder in that path.

  • %~x1 - Expand %1 to a file eXtension only - .txt

  • %~s1 - Change the meaning of f, n, s and x to reference the Short 8.3 name (if it exists.)

  • %~1 - Expand %1 removing any surrounding quotes (")

  • %~a1 - Display the file attributes of %1

  • %~t1 - Display the date/time of %1

  • %~z1 - Display the file size of %1

  • %~$PATH:1 - Search the PATH environment variable and expand %1 to the fully qualified name of the first match found.

The modifiers above can be combined:

  • %~dp1 - Expand %1 to a drive letter and path only

  • %~sp1 - Expand %1 to a path shortened to 8.3 characters

  • %~nx2 - Expand %2 to a file name and extension only

Source parameters


Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • parameters - A command line argument (or parameter) is any value passed into a batch script.