Batch exit if drive device not ready/unavailable - batch-file

I need to force a batch file to stop and exit the batch program (but not the command prompt) if selecting a drive/device fails because it is not ready/unavailable.
I've tried...
d: || exit /b
...and I also tried...
IF %ERRORLEVEL% NEQ 0 exit /b %errorlevel%
...after every line without success.

Like this :
if not exist d: exit/b

Related

batch file: how to check if %~dp0 is a UNC network path?

I have seen How to run batch file from network share without "UNC path are not supported" message? - and the pushd %~dp0 to mount the network path as a drive, and popd to unmount the drive, seems to work for me.
I'd just like to make the batch script capable of detecting whether the location the script is started from is a local, or a network/UNC path. How could I do that? I'd guess, one could check if the first two characters in the path are backslashes \\, but I have no idea how to do it in batch.
So, as somewhat of a pseudocode, I'd need something like:
call :IsUncPath %~dp0 is_unc_path
if %is_unc_path% == 1 (
echo "Mounting UNC path as local drive"
REM #pushd %~dp0
)
echo "Script does whatever here..."
if %is_unc_path% == 1 (
echo "Unmounting local drive for UNC path"
REM #popd
)
pause
:: "force execution to quit at the end of the "main" logic" http://steve-jansen.github.io/guides/windows-batch-scripting/part-7-functions.html
EXIT /B %ERRORLEVEL%
:IsUncPath
echo The value of parameter 1 is %~1
:: NB: there must not be spaces around the assignment!
set "%~2=1"
EXIT /B 0
You could use the errorlevel of net use <drive letter>.
call :isUncPath C:
if %errorlevel% == 0 echo C: is on UNC
exit /b
:IsUncPath
::: param1 - drive letter with colon, ex Y:
::: #return exitcode 0 - drive is network mapped
net use %~1 >nul 2>&1
EXIT /B %errorlevel%
You can also use this:
#echo off
call :chkunc
if %errorlevel% equ 0 echo "%~dp0" is on UNC
:: rest of your script
exit /b
:chkunc
set "testpath=%~dp0"
if "%testpath:~0,2%"=="\\" exit /b 0
exit /b 1

JENKINS job is failed even if ROBOCOPY command successfully executed in a BAT script

I am executing a windows bat script through jenkins. Batch file is giving the desired output,however build is failing.My batch file is..
cd /d D:\\Bank\\Member\\ID
if %errorlevel% neq 0 exit /b %errorlevel%
mkdir OTP
if %errorlevel% neq 0 exit /b %errorlevel%
robocopy C:\Corporate D:\\Bank\\Member\\ID\ /E /XF *.bat
if %errorlevel% neq 1 exit /b %errorlevel%
cd /d D:\\Bank\\Staff\\ID
ROBOCOPY GIVES EXIT CODE 1 AFTER SUCESSFULLY COPYING FILES.
BUT JENKINS FAILS BUILD AND GIVING BELOW ERROR:
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
I Want the build to be successful if robocopy exits code 1.
My best advise would be to use jenkins-pipeline, try/catch block, and use bat commands as few as possible (or do not use at all).
But considering your case there's a simple solution as well: just set the field "ERRORLEVEL to set build unstable" to 1 (or other suitable number). The field appears if you click "Advanced" button under the "Execute Windows batch command" block:
This method will check your build as "Unstable", but will continue to execute.
please use like following to avoid:
bat "robocopy /s source dest & EXIT /B 0"
The above will continue the jenkins build even if robocopy returns non-zero errorlevel. Robocopy does not return 0 for various reasons even after successfull copy, as it compared the two folders. Please lookup for it's return code to know more details
As mentioned here, the first criteria to check is the account used to run Jenkins.
Type services.msc to open the Windows services and look for the Jenkins service.
Instead of "Local Service Account", use your own account: that will avoid any right issue.
But: the other criteria is to display the error code.
As mentioned here:
All exit codes up to '3' are fine.
So after robocopy, you can add:
#echo robocopy exit code: %ERRORLEVEL%
#if %ERRORLEVEL% GTR 3 ( echo robocopy ERROR )
#if %ERRORLEVEL% GTR 3 ( exit %ERRORLEVEL% )
#set ERRORLEVEL=0
REM at the end:
exit /b 0
That will ensure Jenkins don't fail the batch step, even though the original error level for robocopy was 1.

Batch script starting javafx bundled application shows no output [duplicate]

I have a batch file that will run several other file (lets call it procedure file) such as .bat,.exe,.py, etc...
if Not Exist JobStreamUnitTest_CreateTextPython_4-27-2015.txt (
Start /wait /b C:\Users\blee2\Documents\UnitTest\CreateTextFile.py || exit %errorlevel%
copy /y nul JobStreamUnitTest_CreateTextPython_4-27-2015.txt
)
if Not Exist JobStreamUnitTest_CreateTextBatch_4-27-2015.txt (
Start /wait /b C:\Users\blee2\Documents\UnitTest\CreateNewFile.bat || exit %errorlevel%
copy /y nul JobStreamUnitTest_CreateTextBatch_4-27-2015.txt
)
if Not Exist JobStreamUnitTest_CreateTextConsole_4-27-2015.txt (
Start /wait /b C:\Users\blee2\Documents\UnitTest\TestConsole.exe apple || exit %errorlevel%
copy /y nul JobStreamUnitTest_CreateTextConsole_4-27-2015.txt
)
if Not Exist JobStreamUnitTest_HelloWorld_4-27-2015.txt (
Start /wait /b C:\Users\blee2\Documents\UnitTest\HelloWorld.bat || exit %errorlevel%
copy /y nul JobStreamUnitTest_HelloWorld_4-27-2015.txt
)
So basically, the batch file will check if the following file need to be run based on the existence of dummy file associate with each of the procedure file.
This will prevent us from running successfully run if we are to run the batch file the second time.
If there is no error in any of the procedure file then the code will work fine.
The exit error will only work if the file/filepath is incorrect.
The problem I am facing is that, since the Start /wait /b will always execute regardless of if one of my procedure file have an error. Therefore the exit %errorlevel% would not be run.
How do I allow the batch file to detect an error if a procedure file is broken?
I would like to exit/terminal the batch file if one of the procedure file is not working. Any thoughts?
PS. /wait is needed because the start should be running in a sequential order.
/b is needed or else the program will stop after running a .bat ; /b allow us to run the batch file in the same cmd window.
Appreciate any help and thank you
Edited:
The code would work if i do the following. But I am hoping to have a consistency format in my batch file, since the batch file is being generated by C# with parsing of .xml files.
if Not Exist JobStreamUnitTest_CreateTextPython_4-27-2015.txt (
C:\Users\blee2\Documents\UnitTest\CreateTextFile.py || exit %errorlevel%
copy /y nul JobStreamUnitTest_CreateTextPython_4-27-2015.txt
)
if Not Exist JobStreamUnitTest_CreateTextBatch_4-27-2015.txt (
Start /wait /b C:\Users\blee2\Documents\UnitTest\CreateNewFile.bat || exit %errorlevel%
copy /y nul JobStreamUnitTest_CreateTextBatch_4-27-2015.txt
)
I have found some issues in start /WAIT /B any_program || exit %errorlevel%:
#1 - %errorlevel% variable will be expanded at parse time. Thus your script never returns proper exit code. See EnableDelayedExpansion.
#2 - || conditional command execution: unfortunately I can't document it properly, but all my attempts with it failed in relation to start command...
IMHO next code snippet (the only example) could work as expected:
if Not Exist JobStreamUnitTest_CreateTextBatch_4-27-2015.txt (
start /B /WAIT C:\Users\blee2\Documents\UnitTest\CreateNewFile.bat
SETLOCAL enabledelayedexpansion
if !errorlevel! NEQ 0 exit !errorlevel!
ENDLOCAL
copy /y nul JobStreamUnitTest_CreateTextBatch_4-27-2015.txt
)
#3 - a bug in the implementation of the start command.
start /WAIT /B doesn't work (the /wait argument is ignored):
==>start /WAIT /B wmic OS GET Caption & echo xxx
xxx
==>Caption
Microsoft Windows 8.1
There's a simple workaround (from SupeUser) as start /B /WAIT works:
==>start /B /WAIT wmic OS GET Caption & echo xxx
Caption
Microsoft Windows 8.1
xxx

Batch file - check output of exe

I haven't worked with batch files before but I would like to create a batch file that runs a command line program which will output one of two lines depending on success or failure. Is there any way I can capture the executable's output without writing it to a temporary file?
Thanks in advance
put the program in a for /f loop (example):
for /f "delims=" %%a in ('myProgram.exe -a -b -c') do if /i "%%~a"=="failure" (call:dothis) else call:success
if %errorlevel%==0 call:success
if %errorlevel%==1 call:dothis
goto:eof
:dothis
echo Error found.
exit /b 1
:success
echo No error found.
exit /b 0

How can I exit a batch file from within a function?

I have a simple function written to check for directories:
:direxist
if not exist %~1 (
echo %~1 could not be found, check to make sure your location is correct.
goto:end
) else (
echo %~1 is a real directory
goto:eof
)
:end is written as
:end
endlocal
I don't understand why the program would not stop after goto:end has been called. I have another function that uses the same method to stop the program and it work fine.
:PRINT_USAGE
echo Usage:
echo ------
echo <file usage information>
goto:end
In this instance, the program is stopped after calling :end; why would this not work in :direxist? Thank you for your help!
I suppose you are mixing call and goto statements here.
A label in a batch file can be used with a call or a goto, but the behaviour is different.
If you call such a function it will return when the function reached the end of the file or an explicit exit /b or goto :eof (like your goto :end).
Therefore you can't cancel your batch if you use a label as a function.
However, goto to a label, will not return to the caller.
Using a synatx error:
But there is also a way to exit the batch from a function.
You can create a syntax error, this forces the batch to stop.
But it has the side effect, that the local (setlocal) variables will not be removed.
#echo off
call :label hello
call :label stop
echo Never returns
exit /b
:label
echo %1
if "%1"=="stop" goto :halt
exit /b
:halt
call :haltHelper 2> nul
:haltHelper
()
exit /b
Using CTRL-C:
Creating an errorcode similar to the CTRL-C errorcode stops also the batch processing.
After the exit, the setlocal state is clean!
See #dbenham's answer Exit batch script from inside a function
Using advanced exception handling:
This is the most powerful solutions, as it's able to remove an arbitrary amount of stack levels, it can be used to exit only the current batch file and also to show the stack trace.
It uses the fact, that (goto), without arguments, removes one element from the stack.
See Does Windows batch support exception handling?
jeb's solution works great. But it may not be appropriate in all circumstances. It has 2 potential drawbacks:
1) The syntax error will halt all batch processing. So if a batch script called your script, and your script is halted with the syntax error, then control is not returned to the caller. That might be bad.
2) Normally there is an implicit ENDLOCAL for every SETLOCAL when batch processing terminates. But the fatal syntax error terminates batch processing without the implicit ENDLOCAL! This can have nasty consequences :-( See my DosTips post SETLOCAL continues after batch termination! for more information.
Update 2015-03-20 See https://stackoverflow.com/a/25474648/1012053 for a clean way to immediately terminate all batch processing.
The other way to halt a batch file within a function is to use the EXIT command, which will exit the command shell entirely. But a little creative use of CMD can make it useful for solving the problem.
#echo off
if "%~1" equ "_GO_" goto :main
cmd /c ^""%~f0" _GO_ %*^"
exit /b
:main
call :label hello
call :label stop
echo Never returns
exit /b
:label
echo %1
if "%1"=="stop" exit
exit /b
I've got both my version named "daveExit.bat" and jeb's version named "jebExit.bat" on my PC.
I then test them using this batch script
#echo off
echo before calling %1
call %1
echo returned from %1
And here are the results
>test jebExit
before calling jebExit
hello
stop
>test daveExit
before calling daveExit
hello
stop
returned from daveExit
>
One potential disadvantage of the EXIT solution is that changes to the environment are not preserved. That can be partially solved by writing the environent to a temporary file before exiting, and then reading it back in.
#echo off
if "%~1" equ "_GO_" goto :main
cmd /c ^""%~f0" _GO_ %*^"
for /f "eol== delims=" %%A in (env.tmp) do set %%A
del env.tmp
exit /b
:main
call :label hello
set junk=saved
call :label stop
echo Never returns
exit /b
:label
echo %1
if "%1"=="stop" goto :saveEnvAndExit
exit /b
:saveEnvAndExit
set >env.tmp
exit
But variables with newline character (0x0A) in the value will not be preserved properly.
If you use exit /b X to exit from the function then it will set ERRORLEVEL to the value of X. You can then use the || conditional processing symbol to execute a command if ERRORLEVEL is non zero.
#echo off
setlocal
call :myfunction PASS || goto :eof
call :myfunction FAIL || goto :eof
echo Execution never gets here
goto :eof
:myfunction
if "%1"=="FAIL" (
echo myfunction: got a FAIL. Will exit.
exit /b 1
)
echo myfunction: Everything is good.
exit /b 0
Output from this script is:
myfunction: Everything is good.
myfunction: got a FAIL. Will exit.
Here's my solution that will support nested routines if all are checked for errorlevel
I add the test for errolevel at all my calls (internal or external)
#echo off
call :error message&if errorlevel 1 exit /b %errorlevel%<
#echo continuing
exit /b 0
:error
#echo in %0
#echo message: %1
set yes=
set /p yes=[no]^|yes to continue
if /i "%yes%" == "yes" exit /b 0
exit /b 1

Resources