1

I have a complex program. It needs to execute a series of commands, and I need to use && to combine them into a single one command, but it will make this command very long and thus very difficult to read and maintain.

Thus I tried to use set to combine them, the combined command can print correctly, but it can not execute correctly. Below is a example of it. How can I correct this code so it is syntactically valid?

@echo off 
set command=dir
set command=%command% ^^^&^^^& tree

rem this line will print the combined string
echo %command%

rem this line will not execute the combined string
%command%

pause

dir && tree is just an example that I used as an example. In my program there are many command combined together, such as cmd1 && cmd2 && cmd3 && .... I can not run them one by one, I need to use && to only run each if the previous one succeeded.

5
  • Note the differing counts of "watchers" for the 2 tags I have added. Your Q seems perfectly acceptable. Good luck.
    – shellter
    Mar 6 at 4:10
  • I believe you've run into a syntax issue. What exactly does dir && tree mean? It could be interpreted as you expect; dir and then tree or it could be interpreted as "I want a directory listing of objects named && or tree" From the prompt, you get the first interpretation, within batch the second. If you use (dir) in place of dir in your batch, it acts as you expect.
    – Magoo
    Mar 6 at 4:56
  • @Magoo Thanks for your reply, I just make it as an example,actually there are many command combined together,such as cmd1 && cmd2 && cmd3 && ...,for such reason,I need to run them together with &&
    – flyingfox
    Mar 6 at 5:57
  • Just to be clear, to run a sequence of commands one after the other, it would be cmd1 & cmd2 & cmd3 & …. This means run cmd1, then run cmd2, then run cmd3, then run . && has a different meaning, so cmd1 && cmd2 && cmd3 && … means run cmd1, and if that command runs successfully run cmd2, and if that command runs successfully run cmd3, and if that command runs successfully run . The difference between the use of & and && can therefore make a very important difference to your intended goal.
    – Compo
    Mar 6 at 19:20
  • @Compo Thanks for your reply,but && is what I need and Mark's answer meets my demand!
    – flyingfox
    Mar 7 at 5:03

3 Answers 3

1

Ugly, but below works. Quote the string, but remove the quotes to execute it:

@echo off
set command=dir
set command="%command% && tree"

rem this line will print the combined string (quotes as well)
echo %command%

rem this line will execute the combined string
%command:~1,-1%

Sample output:

"dir && tree"
 Volume in drive C has no label.
 Volume Serial Number is CE8B-D448

 Directory of C:\x

03/05/2023  10:44 PM    <DIR>          .
03/05/2023  10:44 PM    <DIR>          ..
03/05/2023  10:44 PM    <DIR>          a
03/05/2023  10:44 PM    <DIR>          b
03/05/2023  10:43 PM               204 x.bat
               1 File(s)            204 bytes
               4 Dir(s)  19,423,408,128 bytes free
Folder PATH listing
Volume serial number is CE8B-D448
C:.
├───a
└───b
3
  • Hi Mark,I just tested it and find that if I put more commands like set command="%command% && xxx",it will not work,can you help me,please? Such as set command=ver set command="%command% && dir" set command="%command% && tree"
    – flyingfox
    Mar 6 at 7:53
  • That’s a different issue but similar solution. set command="%command:~1,-1% && tree" should work to chain the commands if the variable already has quotes. Start with set command="ver" as well Mar 6 at 7:57
  • Thanks,I just tested your solution and it works!
    – flyingfox
    Mar 6 at 11:04
1

Not using a one-liner is hopefully a much cleaner solution.

if dir tree

or, in the general case,

if cmd1 (if cmd2 (if cmd3 (if cmd4 cmd5))))

The parentheses let you easily split the code over multiple lines for improved legibility, too.

1

&& means "if the prior command succeeded"

"Succeeded" is naturally observer-dependent.

So, I'd suggest

set "commands="dir""
set "commands=%commands% "tree""
for %%e in (%commands%) do if not errorlevel 1 %%~e

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.