String Assertion/Throwing Exception in Batch Files - batch-file

bat file via Jenkins. I have a part of code which looks as below
IF %Status% == Completed (
Echo Process has executed successfully
)
ELSE (
<Code to be added>
)
My requirement is that if Status has a value other than 'Completed' then the bat file must throw an exception or something so that the Jenkins Build Fails. Is there any way of assertions or throwing exceptions in bat files? Please help me with this?

Each line of a batch file is seen as a new command. Else on its own is not, so as it is on a new line, it is seen as one. You need to put the parenthesis on the same line as else.
Additionally, you need to double quote your matches to ensure you eliminate any possible whitespace. I have added /i switch to the if statement to make the match case insensitive.
IF /i "%Status%" == "Completed" (
echo Process has executed successfully
) else (
echo Failed to build
pause
exit /b 1
)
A different way is to not use else.
IF not /i "%Status%" == "Completed" goto :error
echo Process has executed successfully
echo other success code goes here
goto :eof
:error
echo Your code has failed.
pause
goto :eof

Related

What :PROMPT means in dos batch file? [duplicate]

I am trying to write a bat file for a network policy that will install a program if it doesn't exist as well as several other functions. I am using GOTO statements depending on whether or not certain criterion are met. However, it seems that the labels are not firing correctly as all of them do.
I have simplified my script so as to grasp some idea of what may be happening.
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
:MISSING
echo file missing
ping localhost -n 5 >NUL
Basically it checks to see that the file "test.txt" exists in folder "c:\test" which id does. So it should echo file exists to the console. However, both "file exists" and "file missing" are echoed to the console. I find that if I remove the file from the folder or simply rename it, it only echoes "file missing"
Why is it running running both labels?
Because a GOTO is just a jump in execution to a point in the script, then execution continues sequentially from that point. If you want it to stop after running 'EXISTING', then you need to do something like this. Note the extra GOTO and new label:
#ECHO OFF
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
goto :NEXTBIT
:MISSING
echo file missing
:NEXTBIT
ping localhost -n 5 >NUL
It's worth noting though that with cmd.exe (i.e., the NT-based command shells [NT, Win2k, XP, etc]), you can do IF...ELSE blocks like this:
#ECHO OFF
IF EXIST c:\test\test.txt (
ECHO File exists
) ELSE (
ECHO File missing
)
ping localhost -n 5 >nul
...so you can eliminate your GOTOs entirely.
It's because you need to skip over the "missing" bit if it exists:
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
goto :COMMON
:MISSING
echo file missing
:COMMON
ping localhost -n 5 >NUL
You may also want to keep in mind that the current cmd.exe batch language is a fair bit more powerful than that which came with MS-DOS. I would prefer this one:
#echo off
if exist c:\test\test.txt (
echo file exists
) else (
echo file missing
)
ping localhost -n 5 >nul
After you echo file exists the next command is
echo file missing
You need to do something to skip the missing case. Perhaps another goto to a :PING label?
When you're debugging it helps to keep the echo on.
Because GOTO statement moves the execution to that label. To use it in the situation like yours, you need to add another GOTO label.
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO MISSING
:EXISTING
echo file exists
GOTO END
:MISSING
echo file missing
GOTO END
:END
ping localhost -n 5 >NUL
#echo off
IF EXIST "c:\test\test.txt" ( :: warning double quotes
GOTO EXISTING
) ELSE ( :: this format best in batch
GOTO MISSING
) :: don't forget
:EXISTING
echo file exists
goto OTHER :: if file exist jump OTHER
:MISSING
echo file missing
:: label is not required
:OTHER
timeout /t 5 >nul
pause

Why batch sometimes does not process commands like CMD does and gives unexpected and different results [duplicate]

This question already has an answer here:
wrong value of %errorlevel% in nested if in .bat file
(1 answer)
Closed 2 years ago.
When I run a command from the CMD directly it works perfectly fine but when run the same command in a batch file (or Jenkins using batch) it gives me a different result and I dont understand why.
To explain it simple, I'm running command below to search for a literal string within a log file:
findstr /C:"MY STRING WITH A %variable%" M:\Logs\output.log
If I check the %ERRORLEVEL% of the result it shows the expected values (0 = found the string or 1 = didnt find it)
However, when I run the same line from a batch file, even from Jenkins, the result is always 0, even though the string is not present in the log, the %ERRORLEVEL% is always 0.
This is the portion of the batch file which includes the command:
if %COUNTER% ==1 (
if not exist M:\Logs\current_bak (
ROBOCOPY "M:\Logs\tkcurrent" "M:\Logs\tkcurrent_bak"
REN "M:\Logs\current" "M:\Logs\current_bak"
MKDIR M:\Logs\current
echo Folders have been backed up
) else (
echo Back up folders are already in place )
findstr /C:"MY STRING WITH A %variable%" M:\Logs\output.log
if %ERRORLEVEL% == 0 (
echo Process has already being kicked off for the files with date %YYMMDD%, skipping it...
echo Download and backup compleated
exit /b 0 )
else (
echo Triggering next Jenkins Job:
curl -I http://<user>:<token>#remoteserver.domain.com:<port>/job/Hello_World/build?token=hello_world
exit /b 0 )
)
Has someone experienced this in the past or can guide me better on what I'm doing wrong or not understanding?
Thanks a lot!
Your error is a basic syntactical error that I've made plenty of times as well :P. The issue is in your else statement which is a peculiar issue, but what happens is that the else only will get processed if it is on the same line as the if statement.
PROOF OF CONCEPT
If I have the if statement and else on different lines like so:
#echo off
echo test
if errorlevel 1 (echo here)
else (echo not there)
pause
This will have the error:
'else' is not recognized as an internal or external command,
operable program or batch file.
However if I change the code a tiny bit so that the else is on the same line as the if, like so:
#echo off
echo test
if errorlevel 1 (echo here
) else (echo not there)
pause
It will not have an error and output
test
not there
Your code done correctly would be:
if %COUNTER%==1 (
if not exist M:\Logs\current_bak (
ROBOCOPY "M:\Logs\tkcurrent" "M:\Logs\tkcurrent_bak"
REN "M:\Logs\current" "M:\Logs\current_bak"
MKDIR M:\Logs\current
echo Folders have been backed up
) else (
echo Back up folders are already in place )
findstr /C:"MY STRING WITH A %variable%" M:\Logs\output.log
if %ERRORLEVEL% == 0 (
echo Process has already being kicked off for the files with date %YYMMDD%, skipping it...
echo Download and backup compleated
exit /b 0
) else (
echo Triggering next Jenkins Job:
curl -I http://<user>:<token>#remoteserver.domain.com:<port>/job/Hello_World/build?token=hello_world
exit /b 0 )
)
For more information on this type in if /? into cmd.
Note: I also fixed I believe a typo in your code of findsrt instead of findstr

How to search a text file in batch for a specific symbol and alter the script depending on the result

I need to create a command that allows me to insert a check of a text file for a very specific symbol (’) and I am having trouble. It is a single quotation mark and it occasionally is found on some folders that need to be zipped and when my batch zipper encounters the folder with the symbol in it's name, it just starts having a lot of problems and creates weird files. I am not going into a lot of detail, but I just need a way to (in plain terms) check if a text file contains the symbol (’) and if it does, send the script to an error line (just something to indicate the symbol was found, like "echo error found"). And if not, then just send it to the rest of the script...
Like FINDSTR "’" dirlist.txt
if found goto err else goto resume
I know that is very incorrect but you get the idea.
Here is what I have so far and I still have made no progress getting it to work:
findstr /i /c:"’" C:\ACFZ\FORZIP\dirlist.txt >2
if %errorlevel% EQU 0 (goto LABEL0) else (goto LABEL1)
:LABEL0
msg %username& "An invalid symbol has been found. Remove any single quotation marks (’) from the folder names and try again. If unsure, simply remove anything that looks like an apostrophe."
pause
goto ERROR
:LABEL1
echo No errors found, continuing
pause
goto ZIPSTART
:ERROR
echo an error was found, exiting...
pause
goto EXIT
It always ends up saying no errors, even though the file has the symbol in it.
Here is the text file I need to search (dirlist)
2082708 Amboy Bank
2082712 Cavender’s
2082736 Elizabeth Board of Education
2082763 Tri-Valley Developmental Services LLC
2082773 Vector Management
OK, so I finally got it working right... Thanks to Harvey, I used the method of outputting any results to a separate file, and then checking that file for contents. Which actually works great, because if it finds an issue, it will show you the full name of the problem folder(s) so you can easily fix it.
Here is the snippet of the working part:
findstr "'" C:\ACFZ\FORZIP\dirlist.txt > error.txt
findstr "." error.txt >nul
IF %ERRORLEVEL% EQU 0 GOTO POPUP
IF %ERRORLEVEL% EQU 1 GOTO ALLCLEAR
and here it is with a bit more detail:
CD C:\ACFZ\FORZIP
DIR /AD /B /ON >dirlist.txt
Echo Checking for errors in folder names...
ping -n 3 localhost >nul
REM that is not an apostrophe!
findstr "'" C:\ACFZ\FORZIP\dirlist.txt > error.txt
findstr "." error.txt >nul
IF %ERRORLEVEL% EQU 0 GOTO POPUP
IF %ERRORLEVEL% EQU 1 GOTO ALLCLEAR
REM Errorlevel 0= Something found, 1= nothing found
:POPUP
color cf
msg %username% "An invalid symbol has been found. Remove any single quotation marks (’) from the folder names and try again. If unsure, simply remove anything that looks like an apostrophe."
goto ERROR
:ALLCLEAR
echo No errors found, continuing...
ping -n 3 localhost >nul
ping -n 3 localhost >nul
goto ZIPSTART
:ERROR
echo An error was found in the following folder name(s) below:^
findstr "." error.txt
echo.
Echo Remove any symbols from the above folder name(s)
echo within your completed folder and try again.
Echo This program will now exit.
pause
goto EXIT
:ZIPSTART
REM Zip contents of each directory
for /f "tokens=*" %%a in (dirlist.txt) do (
CD "%%a"
wzzip "C:\ACFZ\ZIPPED\%%a.zip"
CD..
)
Glad I was able to fix this. I guess WinZip goes really crazy from that quotation mark. The reason I needed this was I wrote this batch script (there is more to it than what I have above, as this was the part I needed to work on) to automate the zipping and backup process at my work, so that the folders for the month's jobs are zipped up and then copied onto the server for archive. It was a pain to manually do it, so with this I can just do it all in one step.
Oh and yeah the errorlevel issue was I did not have it entered correctly. I did not space them over to the right.
Thanks to all who helped.
%error_level% indicates the status of the execution which always successful (0) unless you pass in wrong arguments (e.g. try run findstr without argument or with a wrong file name).
In your case, you need to examine the output (messages printed on the screen) of findstr. One approach is to rely on the fact that nothing is printed on the screen if findstr finds no string matched the search. For example:
set found=""
findstr "'" C:\ACFZ\FORZIP\dirlist.txt > findresult.txt
call:CheckEmptyFile findresult.txt found
if "%found%" EQU "FOUND" (
echo An invalid symbol has been found
) else (
echo No errors found, continuing
)
REM your execution goes here
REM Clean up
del findresult.txt
goto :eof
:CheckEmptyFile
if %~z1 == 0 (
set "%~2=NOTFOUND"
) else (
set "%~2=FOUND"
)
goto :eof
(Reference: Windows BAT : test if a specific file is empty)

How to detect error when ECHO output redirected to a bad path?

First time posting here. I'm writing a DOS batch script to automate the process of archiving Oracle tables. The archive tool requires a parameter file with tags that need to be populated at run-time. I am using the following logic, trying 2 different methods to catch the error, but when redirecting to a bad path, the error is not being caught. Is there an alternative to ECHO, or an alternate way to detect this write error? Or any other ideas? Thanks much!
-Greg
#ECHO OFF
FOR /F %%G IN (O:\tmp\infile.txt) DO (
...
...
ECHO TABLE=%%G > P:\BADPATH\MYOUTFILE1.TXT
IF %ERRORLEVEL% NEQ 0 GOTO END
...
ECHO TABLE=%%G > P:\BADPATH\MYOUTFILE2.TXT
IF ERRORLEVEL 1 GOTO END
...
...
)
:END
IF %ERRORLEVEL% NEQ 0 ECHO FAILED!
ECHO EXITING
Not sure if I understand correctly, but it sounds like you want to check if a file/folder exists first. If so:
IF EXIST P:\BADPATH\ goto notexist else goto exist
:notexist
echo Doesn't exist.
:exist
echo It exists!
Or use standard () tags to contain code within.

Conditional execution (IF, ELSE) NOT WORKING

I want to write a batch script statement where:
FINDSTR has to check for a string AND IF found then just print out FAILED and end the program
IF NOT FOUND then go check/look for another string AND IF FOUND then print out SUCCEEDED and close the program, IF NOT FOUND then print out the error message again.
Any ideas?
Here is what I did:
ECHO Checking the log file for errors...
FINDSTR /C:"Open failed" some_Log.txt && (ECHO Deployment FAILED.
cscript //nologo success_mail.vbs
pause) || FINDSTR "RC (return code) = 0" && (ECHO Deployment was successful.
cscript //nologo fail_mail.vbs
pause)
I don't know why it is not working. Any help would be appreciated.
Here is what i my latest one looks like:
ECHO Checking the log file for errors...
FINDSTR /C:"Open failed" some_Log.txt some_Log.txt && (
ECHO Deployment FAILED.
cscript //nologo fail_mail.vbs
GOTO offshore ) || (
FINDSTR /C:"RC (return code) = 0" some_Log.txt && (
ECHO Deployment was successful.
cscript //nologo success_mail.vbs
GOTO offshore)
)
And, it is not working. Do you see any errors? Thanks in advance.
I see a couple of potential problems.
Your %workDir% or %filenm% could contain spaces or special characters. To be safe you should enclose them in quotes if you haven't already done so in the values.
You must be careful when using both && and || operators. If the command(s) after the && fail, then it can cause the script to fall into the || section, even though the original command before the && succeeded. I'm worried about your CSCRIPT command to send mail. Even if it succeeds today, perhaps it could fail in the future and your logic could be impacted.
I'm guessing that the 2nd search string is supposed to be a phrase and not 5 different searches. Remember that search strings are delimited by space unless the /C option is used.
Your 2nd FINDSTR is hanging because you have neglected to provide a file as input, so it is waiting for data on stdin.
I would structure your code like so
ECHO Checking the log file for errors...
set "file=%workDir%\%filenm%_DEV_Log.txt"
set "search1=Error: Open failed because: No such file or directory"
set "search2=RC (return code) = 0"
set "err="
FINDSTR /C:"%search1%" "%file%" && (set err=1) || (
FINDSTR /C:"%search2%" "%file%" || set err=1
)
if defined err (
ECHO Deployment FAILED.
cscript //nologo success_mail.vbs "%filenm%_ddl_DEV.sql" "%file%"
pause
) else (
ECHO Deployment was successful.
cscript //nologo fail_mail.vbs "%filenm%_ddl_DEV.sql" "%file%"
pause
)

Resources