Specify exit code in batch exit

你说的曾经没有我的故事 提交于 2019-12-10 23:57:19

问题


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

回答1:


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%

It should show:

C:\Temp>script.bat

Hello from script.

C:\Temp>echo %errorlevel%

5



来源:https://stackoverflow.com/questions/21338903/specify-exit-code-in-batch-exit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!