Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify exit code in batch exit

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
like image 495
staples89 Avatar asked Mar 21 '23 20:03

staples89


1 Answers

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

like image 132
mihai_mandis Avatar answered Apr 02 '23 07:04

mihai_mandis