Batch file and DEL errorlevel 0 issue

前端 未结 7 623
刺人心
刺人心 2020-12-03 23:32

The batch has to remove files and directories from specific locations and output success or stdout/stderr messages to a new .txt file. I have created the most of the script

相关标签:
7条回答
  • 2020-12-04 00:32

    The answer of aschipfl is great (thanks, helped me a lot!) using the code under Presetting ErrorLevel you get a nice standard function:

    Take care to use %~1 instead of %1 in the del statement, or you will get errors if you use a quoted filename.

    ::######################################################################
    ::call :DELETE "file.txt"
    ::call :DELETE "file.txt" "error message"
    :DELETE
      >nul ver && for /F "tokens=*" %%# in ('del /F /Q "%~1" 2^>^&1 1^> nul') do (2>nul set =) || (
        if NOT .%2==. echo %~2
      )
    goto :EOF
    

    BTW 1: You can give a nifty error message as a second parameter
    BTW 2: Using :: instead of REM for comments makes the code even more readable.

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