Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my batch file I have many calls of the same program with the same parameters:

program.exe file1.txt -parameter1 -parameter2 -parameter3 -parameter4 >> output.log
program.exe file2.txt -parameter1 -parameter2 -parameter3 -parameter4 >> output.log

Can I declare a variable which will contain the string "-parameter1 -parameter2 -parameter3 -parameter4 >> output.log" and use it like this:

Set variable=-parameter1 -parameter2 -parameter3 -parameter4 >> output.log 
program.exe file2.txt %variable%

I tried to do this, but it doesn't work. If it's possible, what is the correct syntax?

share|improve this question
up vote 3 down vote accepted

In the SET command you will have to escape each > sign with a ^.
This prevents the SET command from interpreting the >> as re-direction of the output.


Set variable=-parameter1 -parameter2 -parameter3 -parameter4 ^>^> output.log
program.exe file2.txt %variable%

Lemur's answer is the correct version if you are going to call subshells (with CMD /C) but for direct invocation this isn't needed.

share|improve this answer
This works fine. Thank you. – Eugene Maksimov Dec 11 '13 at 6:24

I began answering this question to say "Sorry, but no." To my surprise, though, you can! It's not very pretty, or efficient, but you can do it.

Storing the normal arguments to program.exe in a variable is simple. This works (which is just your example, without redirection):

SET MYARGS=-parameter1 -parameter2 -parameter3
program.exe file1.txt %MYARGS%

Redirection, though, is not handled as arguments to the program, but as directives to the shell. It happens before variable setting or expansion, so including the >> output.log suffix writes output to the log file immediately.

To get this to work, you have to escape the command string, and start a new process for each invocation of your program.

SET MYARGS=-parameter1 -parameter2 -parameter3 -parameter4 ^^^>^^^> output.log
CMD.EXE /C program.exe file1.txt %MYARGS%
CMD.EXE /C program.exe file2.txt %MYARGS%

Of course, if the name or location of your logfile ever needs to change, this way makes it a little cleaner:

SET LOGFILE=output.log
SET MYCMD=-parameter1 -parameter2 -parameter3 -parameter4
CMD.EXE /C program.exe file1.txt %MYARGS% %TOLOG%
CMD.EXE /C program.exe file2.txt %MYARGS% %TOLOG%

Spinning up a whole command interpreter for each call is a little heavy, but maybe that's OK for your situation. If you try it, I'd be interested to see how it affects your run times.

NOTE: This trick has at least one other possible trap. Variables you set in the batch file won't be available in the CMD shells you invoke. You can force them to be, but that's more code, and didn't look necessary for what you're doing.

See Hidden features of Windows batch files for the ^ escaping trick, and lots more.

share|improve this answer
Thank you. But I don't understand why do you use three '^' symbols? – Eugene Maksimov Dec 11 '13 at 6:27
@EugeneMaksimov Because Lemur invokes a subshell (with cmd /c) the variable will be parsed twice (the set itself and on the CD /C line). So it need double escaping. ^> becomes ^^^>. – Tonny Dec 11 '13 at 11:57
Thank you, Tonny. – Eugene Maksimov Dec 13 '13 at 5:07

Is it possible to use a variable to represent multiple parameters of a program in the Windows command line?

Yes, definitely

Can I declare a variable which will contain the string:
    -parameter1 -parameter2 -parameter3 -parameter4 >> output.log

and use it like this:
    set variable=-parameter1 -parameter2 -parameter3 -parameter4 >> output.log
    program.exe file2.txt %variable%

Yes, you can definitely do that, but as already mentioned, you need to escape the redirection characters.

You did not specify if you must set the variable in the batch-file. If not, then you can use the regular Environment Variable dialog to set the variable (as seen below).

If you must set the variable in the batch-file, then you can either escape it as others have pointed out, but that tends to be unwieldy due to the syntax. Instead, you can simply quote the call to set:

set "variable=-parameter1 -parameter2 -parameter3 -parameter4 >> output.log"
program.exe file2.txt %variable%

Screenshot of Environment Variables dialog box with variable containing redirection characters

share|improve this answer

You must log in to answer this question.

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