Important Notice: On February 29th, this community was put into read-only mode. All existing posts will remain but customers are unable to add new posts or comment on existing. Please feel to join our Community Discord for any questions and discussions.

Quitting script with an error code denoting a failure

Hi all, I have a batch file to check if a process is running. If the process is running then it should quit the script and if the process is not running then it should go ahead and copy files to the client. If I run the batch file as it is it works no problem.

@echo off
SETLOCAL EnableExtensions

set EXE=Program.exe

FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto ProcessFound

goto ProcessNotFound

:ProcessFound

echo %EXE% is running
echo quitting script
goto END

:ProcessNotFound

echo %EXE% is not running
xcopy \\xxxxxx\xxx\newestversion\* C:\xxxxxx\ /Y /E
echo Program should be installed at this point

goto END

:END
echo Finished!

I need to create an install package in PDQ Deploy and this is where the problems start. In the package I have a Command Step with the following.

CALL "\\xxxxx\update_files.bat" /Q

This also works, it calls the batch file which checks for the running process and then proceeds as defined.

If the script finds the process not running it proceeds with copying the files, enters the output in the Output Log and continues the script with a Success return code. This is good.

If the script finds the process running it sends to the Output Log 

Program.exe is running
quitting script
Finished!

also with a Success return code and then continues (more steps in the package), this is not good.

The problem is that both parts return Success codes and I cannot differentiate if the files were copied or not. If the files were copied then it is a Success, if the file copy was skipped then it is a Failure.

Is there any way to get the right result?

1

Comments

1 comment
Date Votes
  • Just put an exit /B 1 at the end of process found.

    Personally I would probably just split these up into steps in pdq and do just below in step 1

    set EXE=Program.exe
    FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% exit /B 1

    Then assuming you don't have 1 as a success code, it would stop there. Alternatively, you could do

    tasklist | findstr Program.exe

    Where findstr will return 0 (true) if Program.exe is found and 1 if not (so set 1 as success code instead of 0 in this case).

     

    1