在批处理出口中指定出口代码 [英] Specify exit code in batch exit

查看:66
本文介绍了在批处理出口中指定出口代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当它在if存在子句中成功退出时,我需要在批处理脚本中指定自己的退出代码.您会注意到,我将其指定为exit 5,我也尝试了exit /b 5,但没有任何效果.有什么建议吗?

I need to specify my own exit code in my batch script when it successful exits on a if exist clause. As you will notice, I specified it as an exit 5, I have also tried exit /b 5 but nothing has worked. Any suggestions?

@ECHO OFF
CLS

set SOURCE_PARENT=%1
set FOLDER=%2
set TARGET_FILE=%3

net use S: %SOURCE_PARENT% 
net use T: %TARGET_FILE%

if exist T:\%FOLDER% (
  echo Folder exists, process exiting
  net use S: /Delete /y
  net use T: /Delete /y
  exit 5
) else (
  mkdir T:\%FOLDER%
)

xcopy S:\%FOLDER% T:\%FOLDER% /E /I /H

net use S: /Delete /y
net use T: /Delete /y

exit

推荐答案

如果您的脚本script.bat包含:

If you have a script script.bat containing:

@echo off
echo Hello from script.
exit /b 5

在命令提示符下或通过另一个脚本调用

In the command prompt or from another script call

test.bat
echo %errorlevel%

它应该显示:

C:\ Temp> script.bat

C:\Temp>script.bat

您好,来自脚本.

C:\ Temp>回声%errorlevel%

C:\Temp>echo %errorlevel%

5

这篇关于在批处理出口中指定出口代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆