By Quanti Monati


2014-08-16 19:48:19 8 Comments

I try to understand how multiple commands in a single command line in a batch file work.

dir & md folder1 & rename folder1 mainfolder

And other case with similar commands, but & substituted with &&.

dir && md folder1 && rename folder1 mainfolder

1. What is the difference between this two cases?


Other thing I want to ask:

One-liner batch.bat:

dir & md folder1 & rename folder1 mainfolder

Multi-liner batch.bat:

dir
md folder1
rename folder1 mainfolder

2. Are this one-liner and multi-liner equal in terms of batch file procedure?


And one more thing I would like to know:

3. If I call other batch files from a main.bat, are they run independent and simultaneously? Main batch file does not wait for ending procedures in other batch files? How to do that?

1 comments

@Mofi 2014-08-16 21:09:32

& between two commands simply results in executing both commands independent on result of first command. The command right of & is executed after command left of & finished independent on success or error of the previous command, i.e. independent on exit / return value of previous command.

&& results in a conditional execution of second command. The second command is executed only if first command was successful which means exited with return code 0.

For an alternate explanation see Conditional Execution.

dir & md folder1 & rename folder1 mainfolder

is therefore equal

dir
md folder1
rename folder1 mainfolder

A multiline replacement for

dir && md folder1 && rename folder1 mainfolder

would be

dir
if not errorlevel 1 (
   md folder1
   if not errorlevel 1 (
      rename folder1 mainfolder
   )
)

if not errorlevel 1 means the command before did not terminate with an exit code greater 0. As the commands dir and md never exit with a negative value, just with 0 or greater (as nearly all commands and console applications) and value 0 is the exit code for success, this is a correct method to test on successful execution of dir and md. See the Microsoft support article Testing for a Specific Error Level in Batch Files.

Other helpful Stack Overflow topics about errorlevel:

Care must be taken on mixing unconditional operator & with conditional operators like && and || because of the execution order is not necessarily the order of the commands on command line.

Example:

dir "C:\Users\%UserName%" /AD 2>nul || dir "%UserProfile%" /AD & echo User profile path: "%UserProfile%"

This command line is executed as:

dir "C:\Users\%UserName%" /AD 2>nul
if errorlevel 1 dir "%UserProfile%" /AD
echo User profile path: "%UserProfile%"

The ECHO command is always executed independent on result of execution of first DIR whereas second DIR is executed only if first DIR fails like on Windows XP or the user's profile folder is not on drive C: or not in a folder Users at all.

It is necessary to use ( and ) on executing ECHO only if first DIR fails after second DIR independent on result of second DIR.

dir "C:\Users\%UserName%" /AD 2>nul || ( dir "%UserProfile%" /AD & echo User profile path: "%UserProfile%" )

This command line is executed as:

dir "C:\Users\%UserName%" /AD 2>nul
if errorlevel 1 (
    dir "%UserProfile%" /AD
    echo User profile path: "%UserProfile%"
)

For the answer on third question see my answer on How to call a batch file in the parent folder of current batch file? where I have explained the differences on running a batch file with command call or with start or with none of those two commands from within a batch file.

Related Questions

Sponsored Content

30 Answered Questions

[SOLVED] How do I parse command line arguments in Bash?

22 Answered Questions

[SOLVED] Is there an equivalent of 'which' on the Windows command line?

40 Answered Questions

[SOLVED] How to import an SQL file using the command line in MySQL?

15 Answered Questions

[SOLVED] How do I pass command line parameters to a batch file?

15 Answered Questions

[SOLVED] Windows batch files: .bat vs .cmd?

16 Answered Questions

[SOLVED] How do I run two commands in one line in Windows CMD?

7 Answered Questions

5 Answered Questions

[SOLVED] Long commands split over multiple lines in Windows Vista batch (.bat) file

  • 2008-09-16 03:04:34
  • Dan
  • 327614 View
  • 595 Score
  • 5 Answer
  • Tags:   batch-file cmd

7 Answered Questions

[SOLVED] Redirect Windows cmd stdout and stderr to a single file

9 Answered Questions

[SOLVED] List all environment variables from command line?

Sponsored Content