Difference in Delayed expansion of ERRORLEVEL on cmd prompt and win32_process

前端 未结 1 1427
自闭症患者
自闭症患者 2021-01-13 21:02
cmd /V:ON /c dir c:\\ & echo %ERRORLEVEL%     
Volume in drive C is PC COE  Volume Serial Number is 9C37-D0B7

 Directory of c         


        
相关标签:
1条回答
  • 2021-01-13 21:59

    By using

    cmd /V:ON /c dir c:\ERt & echo !ERRORLEVEL!
    

    the new command process started with cmd /V:ON executes just the command dir c:\ERt and then closes and the second command echo !ERRORLEVEL! is executed by current command process on which delayed expansion is not enabled.

    The command line

    cmd /V:ON /c "dir c:\ERt & echo !ERRORLEVEL!"
    

    must be used to run dir c:\ERt AND echo !ERRORLEVEL! in new command process before exiting this command process.

    The double quotes around entire command line to execute in new command process makes the difference.

    Without the double quotes around command line to execute in new command process the current command process interprets the line with the two commands as when typing

    cmd /V:ON /c dir c:\ERt
    echo !ERRORLEVEL!
    

    Also possible would be

    cmd /V:ON /c dir c:\ERt ^& echo !ERRORLEVEL!
    

    Now & operator is escaped for current command process being interpreted as literal character and therefore the entire line is executed with caret character ^ removed by the new command process.

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