user8458095
user8458095

Reputation:

How to insert multiple commands into a batch file using a batch file

I am making a batch file (Let's call it Create.bat) that will create a batch file (Let's call it Created.bat) that will get multiple commands inserted in it.

One of the commands is as follows:

FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %TEST%"') DO IF %%%x == %TEST% goto ProgramON

But when I open Created.bat to edit after running Create.bat, I see the following code inserted:

FOR /F %%x == %TEST% goto ProgramON

Why does it cut out a portion of the code, and how can I fix it?

Upvotes: 2

Views: 293

Answers (1)

Stephan
Stephan

Reputation: 56180

Some characters have to be escaped. Most of them (&<>|) with a caret (^). Percent signs are escaped with another percent sign:

>>created.bat echo DIR ^>nul
>>created.bat echo FOR /F %%%%x IN ('tasklist /NH /FI "IMAGENAME eq %%TEST%%"') DO IF %%%%x == %%TEST%% goto ProgramON

Upvotes: 1

Related Questions