Single line with multiple commands using Windows batch file

前端 未结 1 1439
一个人的身影
一个人的身影 2020-11-21 04:19

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

dir & md folder1 & rename folder1 mainfolder
相关标签:
1条回答
  • 2020-11-21 05:11

    & 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:

    • Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?
    • What are the ERRORLEVEL values set by internal cmd.exe commands?

    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.

    0 讨论(0)
提交回复
热议问题