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.
dir && tree
mean? It could be interpreted as you expect;dir
and thentree
or it could be interpreted as "I want a directory listing of objects named&&
ortree
" From the prompt, you get the first interpretation, within batch the second. If you use(dir)
in place ofdir
in your batch, it acts as you expect.cmd1 && cmd2 && cmd3 && ...
,for such reason,I need to run them together with&&
cmd1 & cmd2 & cmd3 & …
. This means runcmd1
, then runcmd2
, then runcmd3
, then run…
.&&
has a different meaning, socmd1 && cmd2 && cmd3 && …
means runcmd1
, and if that command runs successfully runcmd2
, and if that command runs successfully runcmd3
, 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.&&
is what I need and Mark's answer meets my demand!