批处理文件尝试捕获
我有一个批处理脚本,其语句如下所示:
I have a batch script with the statement as shown below:
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set TIME=%%j
set TIME=%TIME:~0,12%
但是一旦执行,它就会返回一个我首先无法理解的错误.然后才知道错误是由于缺少路径env变量中的 wbem .该bat文件位于中央存储库中,因此任何人都可以执行此操作.因此,需要让用户知道在本地计算机中再次发生此错误的原因.
But once I executed it returned an error which I couldnt understand first. Then got to know the error was because of missing the wbem in path env variable. This bat file is located in a central repository so that anyone can execute this. So, need to let the user know the reason if this error strikes again in their local machine.
我认为Batch中没有try catch语句.另外,还知道有关ErrorLevel的信息.但是我不确定如何实现.在这方面有人可以支持我吗.
I dont think there is a try catch statement in Batch. Also, known about ErrorLevel. But I am not sure how this can be implemented..Can anyone support me in this regard..
Standard method for batch files to handle errors is to use ERRORLEVEL variable. Zero means no errors, non-zero - error:
@rem some code
if %ERRORLEVEL% neq 0 goto ProcessError
@rem ... other code
exit /b 0
:ProcessError
@rem process error
exit /b 1
退出/b exitCode 将ERRORLEVEL设置为该exitCode.
exit /b exitCode sets ERRORLEVEL to this exitCode.