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.

10
  • perhaps you can post your work so far. if you have a complicated expression, variable expansion may be your issue. May 18, 2019 at 9: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? superuser.com/a/999345/183467
    – Alex S
    May 18, 2019 at 9:18
  • @FrankThomas - Simplest example - ln --list %MYSR%
    – Alex S
    May 18, 2019 at 9:23
  • @FrankThomas : SET MYSR = "List.txt" ln --list %MYSR% - schinagl.priv.at/nt/ln/ln.html
    – Alex S
    May 18, 2019 at 9:25
  • 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. May 18, 2019 at 9:37

3 Answers 3

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.

6
  • What part of this is calling an EXE with Arguments defined in the Batch file?
    – Alex S
    May 18, 2019 at 8: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. May 18, 2019 at 8: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, 2019 at 9: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. May 18, 2019 at 9:02
  • @FrankThomas - Please try this with a non windows native .exe cause it seems to not work for "other .EXEs"
    – Alex S
    May 18, 2019 at 9:15
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.

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

2
  • Code without any explanation is useless. Can you elaborate on this a little more?
    – Toto
    Sep 9, 2022 at 18:04
  • See also ss64.com/nt/set.html for more information
    – sionta
    Sep 13, 2022 at 9:58

You must log in to answer this question.

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