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
  • 1,019

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
  • 33,276
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
  • 1,019
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
  • 31