简体   繁体   中英

Batch File Pass Delayed Variable Expansion As Argument

I have a batch variable, which contains the name of another variable. When I perform a CALL ECHO on the variable, the output is what I expect. However, when I try to pipe the output of the CALL ECHO command as an argument to another command, I am running into issues. This is what I am attempting:

@SET myCmd=myCmd -flag1 -flag2
@SET myInnerVar=Bar
@SET myCmdArg="Value of myInnerVar is %%myInnerVar%% for this call"
:: The following correctly prints "Value of myInnerVar is Bar for this call"
@CALL ECHO %myCmdArg%
:: The following runs without passing the output as an argument to myCmd
@CALL ECHO %myCmdArg%| %myCmd%

Does anyone know how to call myCmd using delayed variable expansion, as if it was called directly with the arguments:

@myCmd -flag1 -flag2 "Value of myInnerVar is Bar for this call"

The reason I am trying to do this is that I need to call myCmd multiple times, where the only thing that is changing is the string "Bar". I know that I could do this using three variables (myCmdPre, myInnerVar, and myCmdPost) without delayed variable expansion. However, it seems like there should be another (more concise) way to accomplish my goal without having to type %myCmdPre%%myInnerVar%%myCmdPost% every time I want to execute myCmd.

EDIT

To clarify, I wrote a second batch file in the same directory called myCmd.bat, with the following content:

@ECHO %*

When I run my example batch file, the output that is generated is:

-flag1 -flag2

It is as if nothing is being piped to myCmd.

This is another way to do the same thing

@setlocal EnableDelayedExpansion

@SET myCmd=myCmd -flag1 -flag2
@SET Foo=Bar
@SET myCmdArg="Value of Foo is ^!Foo^! for this call"
:: The following correctly prints "Value of Foo is Bar for this call"
@ECHO %myCmdArg%
:: The following runs passing the output as an argument to myCmd
@CMD /V:ON /C ECHO %myCmdArg% | %myCmd%

The problem here is that when two processes are executed via a pipe | , the current value of setlocal is lost for each side of the pipe, so each side is executed with no delayed expansion.

EDIT : The following also works and does not require Delayed Expansion:

@SET myCmd=myCmd -flag1 -flag2
@SET Foo=Bar
@SET myCmdArg="Value of Foo is %%Foo%% for this call"
:: The following correctly prints "Value of Foo is Bar for this call"
@CALL ECHO %myCmdArg%
:: The following runs passing the output as an argument to myCmd
@CALL ECHO %myCmdArg% | %myCmd%

EDIT

Ops! I entirely confused the core point of the question! If you want to pass the value of a variable AS ARGUMENT of a Batch file, why do you PIPE IT to the Batch file? If -file1 and -file2 are arguments of your Batch file and you want the value of myCmdArg variable be also an argument (the third one), then just put it after the -file1 and file2 arguments :

:: The following runs passing the value of myCmdArg variable as an argument to myCmd
@CALL %myCmd% "%myCmdArg%"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM