0

Possible to use Long Path Variables in Batch file to call an EXE with those variables as command-line arguments? How?

  • I read that we can SET various kind of variables in a Batch file.

  • Also, some of these arguments are really long paths.

  • Can they be used as values to pass as Arguments to an EXE being called/ invoked within that Batch file?

  • I understand & have seen examples of this in PowerShell scripts. Can one do something similar with Batch files & their variables?

Been facing issues trying to get the SET command and Path Variables to work.

Alex S
  • 951
  • 3
  • 16
  • 42
  • perhaps you can post your work so far. if you have a complicated expression, variable expansion may be your issue. – Frank Thomas May 18 '19 at 09:09
  • @FrankThomas - Not really complex just a non native .EXE - This is the .exe that I have to use - ln.exe - See if any example works with it? https://superuser.com/a/999345/183467 – Alex S May 18 '19 at 09:18
  • @FrankThomas - Simplest example - ln --list %MYSR% – Alex S May 18 '19 at 09:23
  • @FrankThomas : SET MYSR = "List.txt" ln --list %MYSR% - http://schinagl.priv.at/nt/ln/ln.html – Alex S May 18 '19 at 09:25
  • your equal sign must be immediately after the variable name. `SET MYSR="List.txt"` . https://ss64.com/nt/set.html if that doesn't completely address the issue, also provide the full path to the file. – Frank Thomas May 18 '19 at 09:30
  • 1
    Note from the article on SET I linked, "Any extra spaces around either the variable name or the string, will not be ignored, SET is not forgiving of extra spaces like many other scripting languages.". so your command would probably work if you entered `ln --list %MYSR %` (a space following the 'R'), since the trailing space became part of the variable name. – Frank Thomas May 18 '19 at 09:37
  • Please note that https://superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read [How do I ask a good question?](https://superuser.com/help/how-to-ask). – DavidPostill May 18 '19 at 19:52
  • I will post some samples that have been tried. Realised one flaw was the space. Will update question in a day or two with examples tried. – Alex S May 19 '19 at 19:03
  • @DavidPostill - 2 major flaws happened. The SPACES were causing issues and want to have long paths required part of this linked insight https://stackoverflow.com/a/55951234/1937901 - If allowed I can reformulate the question and also post a full answer for it with examples in the Q and Answer – Alex S May 20 '19 at 17:34
  • @AlexS Reopen so you can update the question and self answer. – DavidPostill May 20 '19 at 17:36

3 Answers3

0
SET MYVAR="Hello World"
ECHO %MYVAR%

Replace echo with whatever command you want.
I recommend you run SET /? in a CommandPrompt Window and read the output carefully. It will give you a lot of very useful additional information.

Tonny
  • 29,601
  • 7
  • 52
  • 84
  • What part of this is calling an EXE with Arguments defined in the Batch file? – Alex S May 18 '19 at 08:30
  • @AlexS, `echo` is a command, but passing an agurment to it is no differant than with an exe. for instance `notepad.exe %myvar%` would pass the contents of myvar (presumably a textfile path) to notepad, which will attempt to open it. In the batch file, you are essentially building a string, that is the contents of the command you wish to invoke, with its arguments, and then executing it. putting a variable inline will cause it to be expanded at execution time. – Frank Thomas May 18 '19 at 08:51
  • @FrankThomas - I am familiar with Echo and its ability to RePrint the SET variable. I tried it, it does not work - So far my experience the SET variables I create I am unable to pass to a .EXE as an argument. Notepad wont open it. – Alex S May 18 '19 at 09:00
  • It just did for me. `SET MYVAR="MyText.txt" notepad.exe %MYVAR%` (put each command on one line). just save it in a batch file, and execute the batch from a shell. Notepad will prompt to ask if you want to create the file if it doesn't exist (you would say Create), or it opens it if it exists. – Frank Thomas May 18 '19 at 09:02
  • @FrankThomas - Please try this with a non windows native .exe cause it seems to not work for "other .EXEs" – Alex S May 18 '19 at 09:15
  • `SET MYVAR="https:www.superuser.com" "c:\Program Files\Mozilla Firefox\firefox.exe" %MYVAR%` just worked for me. – Frank Thomas May 18 '19 at 09:23
0

Yes, its possible and better way to write .BAT Batch files for ease of reading and ease of changing values & arguments.

2 major flaws happened and got resolved.

A) The SPACES in the SET commands were causing issues

  • The equal sign must be immediately after the variable name. SET MYVAR="List.txt"

  • From the article on SET linked by Frank Thomas above,

    • https://ss64.com/nt/set.html

    • "Any extra spaces around either the variable name or the string, will not be ignored, SET is not forgiving of extra spaces like many other scripting languages.". so your command would probably work if you entered ln --list %MYVAR % (a space following the 'R'), since the trailing space became part of the variable name

B) The "Quotes" in the Long Paths need a solution I found elsewhere.

To have long paths required part of this linked insight and hence pulled part of the answer information from here: https://stackoverflow.com/a/55951234/1937901

If you need to concatenate paths with quotes, you can use = to replace quotes in a variable. This does not require you to know if the path already contains quotes or not. If there are no quotes, nothing is changed.

@echo off
rem Paths to combine
set DIRECTORY="C:\Directory with spaces"
set FILENAME="sub directory\filename.txt"
echo %DIRECTORY%
echo %FILENAME%

rem This is just to illustrate how the = operator works
set DIR_WITHOUT_SPACES=%DIRECTORY:"=%
echo %DIR_WITHOUT_SPACES%

rem Combine two paths
set COMBINED="%DIRECTORY:"=%\%FILENAME:"=%"
echo %COMBINED%

Using the above example, this was leveraged to create & concatenate Path strings using SET and then pushed as commandline arguments to ln.exe

SET FolA1="..Folder Sub Path 1.."
SET FolA2="..Folder Sub Path 1.."

SET SrcRoot="C:\Users\UserName\AppData\Roaming\ApplicationName\Backups"

SET DstRoot="S:\HL_TEST\LN"

SET DstCountFol="003--includedir--cp-bk"
SET DstLog=%DstCountFol:"=%_Log.txt

SET DstPlus=%DstRoot:"=%\%DstCountFol:"=%

SET SrcA1=%SrcRoot:"=%\%FolA1:"=%
SET SrcA2=%SrcRoot:"=%\%FolA2:"=%

SET DstA1=%DstRoot:"=%\%DstCountFol:"=%\%FolA1:"=%
SET DstA2=%DstRoot:"=%\%DstCountFol:"=%\%FolA2:"=%

ECHO %SrcA1%
ECHO %SrcA2%

ECHO %DstA1%
ECHO %DstA2%

ECHO ON  

ln --progress --json --output %DstLog% --source %SrcA2% --destination %DstA2% --backup --copy %SrcA1% %DstA1%  

This was the final command line for LN.exe in Batch file called with Longer Path Variables as arguments.

Alex S
  • 951
  • 3
  • 16
  • 42
0
rem from another variable
set "add_path=C:\food\foo bar"
set "path=%add_path%;%path%"

rem ..mode simple append to path
set "path=C:\food\foo bar;%path%"

See also https://ss64.com/nt/set.html for more information

sionta
  • 21
  • 3