windowsbatch-filecommand-linecmdsimultaneous

Single line with multiple commands using Windows batch file


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?


Solution

  • & 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.

    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 command start or with none of those two commands from within a batch file.