Domanda

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
È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top