1

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.

0

3 Answers 3

2

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):

5
  • 1
    @sancho.s Use %~n1 - expands %1 to a file name without file extension. See Command Line arguments (Parameters) for this and other parameter extensions.
    – DavidPostill
    Commented Jun 25, 2015 at 11:17
  • Very thorough answer! I will check the options and post feedback. Commented Jun 25, 2015 at 16:27
  • Feedback #1: I do not know why you set 3 levels of "worthiness" for your solutions (perhaps you could comment on the reasons). At any rate, for the intended use, as posted in the OP, I found the first one is useful, since it allows for extracting the app name without .exe, for later use. The following two alternatives only detect if .exe is present, but they do not isolate the app name. Commented Jul 30, 2015 at 10:44
  • @DavidPostill - Your comment is worth an answer. Commented Jul 30, 2015 at 10:49
  • I have added a summarizing answer below, showing the appropriate line of code, superuser.com/a/947474/245595 Commented Jul 30, 2015 at 11:15
1

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

0

I will answer the specific question posted: What should the line in the .bat file be?, which was split into 3 necessary steps.

JosefZ answered separately the 3 points leading to the answer. DavidPostill gave another option for point 2.

The resulting code would be

set "app1=%~1"
if "%app1%"=="" (
  echo No parameter supplied. This should be run as
  echo   regadd.bat myapp.exe
  echo or
  echo   regadd.bat myapp
  goto :eof
) else (
  REM set "app=%app1:.exe=%"
  set "app=%~n1"
)
if "%app%"=="%app1%" (
  REM echo .exe not present
) else (
  REM echo .exe present
)
echo App is %app%
reg add "HKCR\Applications\%app%.exe\shell\open\command" /ve /t REG_EXPAND_SZ /d "\"%%MYAPPSDIR%%\%app%\%app%.exe\" \"%%1\"" /f

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .