If else function in .bat script + findstr command - bash

IF find tcp.client == 1
(
findstr tcp.client *_chkpackage.log>summary.txt
)
ELSE
(
#write only "filename" & "N/A" >> summary.txt
)
i want to search a file that search some value in all text files in folder
if found the line that contain information i need it is going to write that line to text fiel
if not found the it will write only filename and "N/A" to the line
i know it wrong but my coding skill suck so i have to ask
thank you so much for the answer

Something that works according to your "specification" would be that one:
#echo off
if exist summary.txt del summary.txt
for %%f in (*_chkpackage.log) do (
find "tcp.client" %%f>NUL:
IF errorlevel 1 (
echo %%f N/A >> summary.txt
) ELSE (
findstr "tcp.client" %%f >> summary.txt
)
)
type summary.txt
Note that the if errorlevel 1 means "if errorlevel is greater or equal 1", for that I swapped your comparison, because if errorlevel 0 is true even when errorlevel equals 1.

I'm assuming the dot is a dot and not a regex wildcard, in which case you need to tell FINDSTR to do a literal search.
FINDSTR returns success if found, error if not found. The || operator conditionally executes commands if the prior command failed.
Use FOR loop to get each file individually, and add an additional file (nul) that will never match to force inclusion of file name in output.
Enclose entire construct in parentheses and redirect the entire block
#echo off
>summary.txt (
for %%F in (*_chkpackage.log) do findstr /l "tcp.client" "%%F" nul||echo %%F N/A
)

Related

Not getting the required output through "find" command in a for loop in Windows Batch Scripting

My requirement is simple, i just want to scan all the files in the current directory for a particular string and if that string is found i just want a display saying "String is found" otherwise "String not found"
#ECHO OFF
for %%f in (C:\Users\aalvoor\Desktop\BatchScript\*) do (
echo File is %%f
find /c "defaultModel" %%f >NUL
if %errorlevel% equ 1 (echo File is notfound) else (echo String is found)
)
But the problem is it works when i am not putting it in a for loop but when i put it in for loop for some reason for every file i get a message String is found which is not true.
Use conditional operators && and ||
&& being if errorlevel equ 0
|| being if errorlevel neq 0
#echo off
for %%f in ("%userprofile%\Desktop\BatchScript\*") do (
echo File is %%f
find /c "defaultModel" %%f >nul 2>&1 && echo String found || echo String NOT found
)

Using CMD to find and count occurences?

Simplification :
A long command yields counts of occurences :
echo 2 | find /C "2" //output "1"
This is because "2" appears only once (per line) hence the output 1.
But now I want to echo success only if the value is greater than 1. ( else throw error)
something like this :
echo 2 | find /C "2" | check if val >1 && echo "success" ELSE throw
Question:
I've managed to do the left part. But How can I create the right section ?
use conditional executing.
If you only want to check, if a string occures at least once, you don't need /c:
echo 2|find "2" >nul && echo success || echo fail
For other numbers (e.g "more than one"), you need to count /c and to capture the output with a for /f loop:
#echo off
setlocal
for /f %%a in ('type "%~f0"^|find /c "e"') do set count=%%a
echo %count%
if %count% gtr 1 (
echo more than one
) else (
echo one or less
)
For demo, I search the batchfile itself and count the lines containing (at least one) e. A for /f loop is used to capture the output of a command into a variable, which you then can compare with another value.
Just for academic reasons, without a for loop:
type test.txt|find /c "2"|findstr /xv "0 1" >nul && echo more than one
This works in your special case, but the for loop is more generic.
(I know, you are aware of it, but to make clear for future readers: find /c counts lines that contain the search string (at least once), not overall occurrences)

ERRORLEVEL in FOR /F Command Loop Returns Unexpected Result

I am trying to log the output of net stop while also capturing its ERRORLEVEL.
Based on this question, I attempted the following from within a nested subroutine:
set /a loopIndex=0
for /F "usebackq delims=" %%i in (`net stop %SERVICE_NAME%`) do (
if !loopIndex! EQU 0 if !errorlevel! EQU 1 set statementError=1
set /a loopIndex+=1
call :logMessage "%%i"
)
echo statementError: %statementError%
However, this does not work, throwing 1 even when net stop succeeds.
Is this possible without a temp file? If not, what would a temp file solution look like?
As #drruruu asked in this question :
Is this possible without a temp file?
Yes, it's possible without a temp file. By sending ERRORLEVEL to STDOUT in the IN clause and parse it in the LOOP clause. And it could be done with delayed expansion too.
For convenience, here is an example. It's somewhat a FINDSTR wrapper that search for a string in the batch itself. It covers all the common cases where you need to know what was going wrong, where and why :
Error in the DO () clause (aka the loop) and get the corresponding exit code
Error in the IN () clause and get the corresponding exit code
Error directly at the FOR clause (wrong syntax, bad delimiters, etc.)
The following script simulates theses situations with FINDSTR and flags as parameters :
The first parameter is the string to search.
The second parameter is a 0/1 flag to simulate an error not related to FINDSTR in the loop.
The third parameter is a way to simulate an error on the FOR clause itself (not on IN nor LOOP)
The fourth parameter is a way to test a FINDSTR which exit 255 when the file to search does not exist. Sadly, FINDSTR exit with 1 when it can't find a string in the file/files, but also exit with 1 when it can't find any files.. With the fourth parameter, we simulate a situation where FINDSTR exit with 255 when it can't find the file.
#echo off
SETLOCAL ENABLEEXTENSIONS
IF ERRORLEVEL 1 (
ECHO Can't use extensions
EXIT /B 1
)
SETLOCAL ENABLEDELAYEDEXPANSION
IF ERRORLEVEL 1 (
ECHO Can't use delayed expansion
EXIT /B 1
)
REM The string to search
SET "LOCALV_STRING=%1"
REM The file to search. Myself.
SET "LOCALV_THIS=%0"
REM Store the exit code for the LOOP
SET "LOCALV_ERR="
REM Store the exit code for the IN
SET "LOCALV_RET="
REM Flag to stop parsing output for error simulation
SET "LOCALV_END="
REM To get the exit code of the IN clause, we get it through expansion with a second FOR loop using the well known CALL expansion and send it on STDOUT in the form "__<code>"
FOR /F "%~3tokens=*" %%M IN ('FINDSTR "!LOCALV_STRING!" "!LOCALV_THIS%~4!" ^
^& FOR /F %%A IN ^("ERRORLEVEL"^) DO #CALL ECHO __%%%%A%%') DO (
SET "LOCALV_TMP=%%~M"
REM Simulate that something goes wrong with FINDSTR I/O
IF NOT EXIST "!LOCALV_THIS!%~4" (
SET "LOCALV_RET=255"
SET LOCALV_END=1
)
IF "!LOCALV_END!" == "" (
REM SImulate a problem in the loop
IF "%2" == "1" (
(CMD /C EXIT /B 127)
SET LOCALV_END=1
) ELSE (
IF NOT "!LOCALV_TMP:~0,2!" == "__" ECHO Found: !LOCALV_TMP!
)
)
IF "!LOCALV_TMP:~0,2!!LOCALV_RET!" == "__" SET "LOCALV_RET=!LOCALV_TMP:__=!"
)
SET "LOCALV_ERR=!ERRORLEVEL!"
REM LOCALV_ERR get the exit code from the last iteration of the for loop
REM LOCALV_RET get the exit code from the IN command of the for loop
REM Sadly, FINDSTR exit with 1 if it did not find the string, but also with 1 if it could not found the file. To simulate a proper handling of exit code for
REM abnormal hardware/software situation, %2 is used to force a 255 exit code
REM If LOCALV_RET is not defined, this means the FOR...ECHO__.. wasn't executed, therefore there is a pb with the FOR LOOP
IF "!LOCALV_RET!" == "" (
ECHO Something went wrong with FOR...
EXIT /B 1
)
REM If LOCALV_RET is defined, this means the FOR...ECHO__.. was executed and the last loop operation has parsed the FINDSTR exit code, LOCALV_RET get its exit code
REM If LOCALV_RET is defined but LOCALV_ERR is not "0", something went wrong in the loop (I/O error, out of memory, wathever you could think), the problem is not FINDSTR
IF NOT "!LOCALV_ERR!" == "0" (
ECHO Error in the loop while searching "!LOCALV_STRING!" in "!LOCALV_THIS!", exit code !LOCALV_RET!. Loop exit code : !LOCALV_ERR!.
EXIT /B 4
)
REM If LOCALV_RET is "0", FINDSTR got matching strings in the file, if "1", FINDSTR don't find any matching string, if anything else, FINDSTR got a problem like failed I/O.
REM If LOCALV_RET is "0" and LOCALV_ERR is "0", everything is ok.
IF "!LOCALV_RET!" == "0" (
ECHO Success.
EXIT /B 0
)
REM If LOCALV_RET is "1" and LOCALV_ERR is "0", FINDSTR failed to find the string in the file "or" failed to find file, for the latter we simulate that FINDSTR exit with 255 .
IF "!LOCALV_RET!" == "1" (
ECHO FINDSTR failed to find "!LOCALV_STRING!" in "!LOCALV_THIS!", exit code !LOCALV_RET!. Loop exit code : !LOCALV_ERR!.
EXIT /B 2
)
REM If LOCALV_RET isn't "0" nor "1" and LOCALV_ERR is "0", FINDSTR failed to do the job and LOCALV_RET got the exit code.
ECHO FINDSTR: Houst^W OP, we've got a problem here while searching "!LOCALV_STRING!" in "!LOCALV_THIS!", exit code !LOCALV_RET!. Loop exit code : !LOCALV_ERR!.
EXIT /B 3
Script output :
Normal operation, no error simulation.
PROMPT>.\for.bat FOR 0 "" ""
Found: FOR /F "%~3tokens=*" %%M IN ('FINDSTR "FOR" ""
Found: ^& FOR /F %%A IN ^("ERRORLEVEL"^) DO #CALL ECHO __%%%%A%%') DO (
Found: REM If LOCALV_RET is not defined, this means the FOR...ECHO__.. wasn't executed, therefore there is a pb with the FOR LOOP
Found: ECHO Something went wrong with FOR...
Found: REM If LOCALV_RET is defined, this means the FOR...ECHO__.. was executed and the last loop operation has parsed the FINDSTR exit code, LOCALV_RET get its exit code
Success.
Normal operation, no error simulation, with a string that FINDSTR can't find in the file.
PROMPT>.\for.bat ZZZ 0 "" ""
FINDSTR failed to find "ZZZ" in ".\for.bat", exit code 1. Loop exit code : 0.
Simulate an error in the LOOP clause, not related to FINDSTR.
PROMPT>.\for.bat FOR 1 "" ""
Error in the loop while searching "FOR" in ".\for.bat", exit code 0. Loop exit code : 127.
Simulate an error in the FOR clause at start with an unknow "delimstoken" option.
PROMPT>.\for.bat FOR 0 "delims" ""
delimstokens=*" was unexpected.
Something went wrong with FOR...
Simulate FINDSTR exiting 255 if it can't find the file.
PROMPT>.\for.bat FOR 1 "" "ERR"
FINDSTRĀ : Can't open
FINDSTR: HoustW OP, we've got a problem here while searching "FOR" in ".\for.bat", exit code 255. Loop exit code : 0.
The FOR /F command executes NET STOP in a new cmd.exe process. FOR /F processes stdout, but that is it. There is no way for the main script to see any variable values that the FOR /F command might create, as they are gone once the sub-process terminates.
The simplest and most efficient solutions use a temporary file. I'm assuming NET STOP has two possible error codes - Success = 0, and Error = 1. So the simplest solution is to simply create a temporary error signal file if there was error.
The following demonstrates the concept in a generic way:
#echo off
del error.flag 2>nul
for /f "delims=" %%A in ('net stop %SERVICE_NAME% ^|^| echo error>error.flag') do (
...
)
if exist error.flag (
echo There was an error
del error.flag
)
You could just as easily put the error test within the DO() code if desired.
While #dbenham's answer is suitable for cases where %ERRORLEVEL% returns a binary value, I was not able to confirm or deny if the returned exit codes for net stop are in fact binary and so opted for an n-ary solution.
As per #dbenham's
DOS tips forum post:
FOR /F "delims=" %%i IN ('net stop MyService 2^>^&1 ^& CALL ECHO %%^^ERRORLEVEL%%^>error.level') DO (
CALL :logMessage "%%i"
)
FOR /F "delims=" %%i IN (error.level) DO (SET /A statementError=%%i)
DEL error.level
IF %statementError% NEQ 0 ()
Breaking down the statement parsing:
net stop MyService 2^>^&1 ^& CALL ECHO %%^^ERRORLEVEL%%^>error.level
net stop MyService 2>&1 & CALL ECHO %^ERRORLEVEL%>error.level
echo %ERRORLEVEL%>error.level
Here, CALL is used specifically to delay parsing of %ERRORLEVEL% until execution of ECHO.

Windows Batch Script parsing multiline file with variable line formats?

I need to parse a file that has the following format:
A + tags/RWSTestConsole_tag1/
(from trunk/RWSTestConsole/:r776)
So I'm using a FOR /F loop with a counter and inspecting the tokens based on whether I'm looking at line 1 or line 2. Everything works fine except the first token for line 2 includes the leading spaces (" from") and a) I thought the delims on my FOR would take care of spaces and b) theoretically I could just compare to a constant string that is set to " from" but that's kind of hokey.
This is my FOR command:
for /F "tokens=1,2,3,4,5 delims=():/ " %%a in (svn.txt) DO (
Is there a change I can make to the FOR command to ignore the spaces? If not is there a way to trim the token inside the FOR loop's DO clause so I only get the word without the leading spaces?
Edit:
This is output from a Subversion SVNLOOK command in a script that is managing whether or not to allow a "tag" to be created. The output can be 1 line that must be formatted as:
D /tags/tagfoldername/
If it's one line but it's not a delete for the actual tag folder then it's an error. This case is handled.
If it's more than 2 lines it's a list of files and that's an error. I have that handled.
The case I'm having problems with is if it is 2 lines it needs to be in the format shown above:
A + tags/RWSTestConsole_tag1/
(from trunk/RWSTestConsole/:r776)
Where col 1 = "A". col 3 = "+", col 5 = "tags" and the remainder of line one is the tag folder name. The second line is the source of the create request so it has to start with "from", followed by "trunk", "branches" or "tags" followed by a single-level folder name and a revision number.
I used the FOR command as described above. In the DO clause I look at a counter to tell if I'm parsing line 1 or line 2. Line 1 is simple, I have all the logic to handle it.
Line 2 is parsed by the same FOR command and the first token (%%a) removes the "{" from delims, but leaves behind all the leading blanks so I get back %%a=" from".
I need to know if there's a way I can modify the FOR command to remove the blanks or a way to trim %%a within the FOR DO clause to remove the blanks.
Edit 2: FOR Loop Code
set c=1
for /F "tokens=1,2,3,4,5 delims=():/ " %%a in (svn.txt) DO (
echo op=%%a, %%b, %%c, %%d, %%e
if !c!==1 (
set rc1=0
if /I %%a EQU A (
if "%%b" EQU "+" (
if [%%e] EQU [] (
echo Tag from a Copy Op
set rc1=0
) else (
echo Found a subfolder: %%e - not a tag delete
set rc1=1
)
) else (
echo Tag not a Copy
set rc1=1
)
)
)
if !c!==2 (
set rc2=0
set str1=%%a
echo String replace 1 = !str!
set str2=!str1:~-4!
echo String Replace 2 *!str2!*
if /I !str2! EQU FROM (
set isvalid=false
if %%b EQU trunk set isvalid=true
if %%b EQU branches set isvalid=true
if %%b EQU tags set isvalid=true
if !isvalid! EQU true (
set rc2=0
) else (
set rc2=1
echo Invalid source for Tag Creation
)
) else (
set rc2=1
echo Tag not FROM
)
)
set /a c+=1
)
echo RC1=!rc1!
echo RC2=!rc2!
set /a rc=!rc1!+!rc2!
echo final RC = !rc!

Batch file: Find if substring is in string (not in a file)

In a batch file, I have a string abcdefg. I want to check if bcd is in the string.
Unfortunately it seems all of the solutions I'm finding search a file for a substring, not a string for a substring.
Is there an easy solution for this?
Yes, you can use substitutions and check against the original string:
if not x%str1:bcd=%==x%str1% echo It contains bcd
The %str1:bcd=% bit will replace a bcd in str1 with an empty string, making it different from the original.
If the original didn't contain a bcd string in it, the modified version will be identical.
Testing with the following script will show it in action:
#setlocal enableextensions enabledelayedexpansion
#echo off
set str1=%1
if not x%str1:bcd=%==x%str1% echo It contains bcd
endlocal
And the results of various runs:
c:\testarea> testprog hello
c:\testarea> testprog abcdef
It contains bcd
c:\testarea> testprog bcd
It contains bcd
A couple of notes:
The if statement is the meat of this solution, everything else is support stuff.
The x before the two sides of the equality is to ensure that the string bcd works okay. It also protects against certain "improper" starting characters.
You can pipe the source string to findstr and check the value of ERRORLEVEL to see if the pattern string was found. A value of zero indicates success and the pattern was found. Here is an example:
::
: Y.CMD - Test if pattern in string
: P1 - the pattern
: P2 - the string to check
::
#echo off
echo.%2 | findstr /C:"%1" 1>nul
if errorlevel 1 (
echo. got one - pattern not found
) ELSE (
echo. got zero - found pattern
)
When this is run in CMD.EXE, we get:
C:\DemoDev>y pqrs "abc def pqr 123"
got one - pattern not found
C:\DemoDev>y pqr "abc def pqr 123"
got zero - found pattern
I usually do something like this:
Echo.%1 | findstr /C:"%2">nul && (
REM TRUE
) || (
REM FALSE
)
Example:
Echo.Hello world | findstr /C:"world">nul && (
Echo.TRUE
) || (
Echo.FALSE
)
Echo.Hello world | findstr /C:"World">nul && (Echo.TRUE) || (Echo.FALSE)
Output:
TRUE
FALSE
I don't know if this is the best way.
For compatibility and ease of use it's often better to use FIND to do this.
You must also consider if you would like to match case sensitively or case insensitively.
The method with 78 points (I believe I was referring to paxdiablo's post) will only match Case Sensitively, so you must put a separate check for every case variation for every possible iteration you may want to match.
( What a pain! At only 3 letters that means 9 different tests in order to accomplish the check! )
In addition, many times it is preferable to match command output, a variable in a loop, or the value of a pointer variable in your batch/CMD which is not as straight forward.
For these reasons this is a preferable alternative methodology:
Use: Find [/I] [/V] "Characters to Match"
[/I] (case Insensitive)
[/V] (Must NOT contain the characters)
As Single Line:
ECHO.%Variable% | FIND /I "ABC">Nul && ( Echo.Found "ABC" ) || ( Echo.Did not find "ABC" )
Multi-line:
ECHO.%Variable%| FIND /I "ABC">Nul && (
Echo.Found "ABC"
) || (
Echo.Did not find "ABC"
)
As mentioned this is great for things which are not in variables which allow string substitution as well:
FOR %A IN (
"Some long string with Spaces does not contain the expected string"
oihu AljB
lojkAbCk
Something_Else
"Going to evaluate this entire string for ABC as well!"
) DO (
ECHO.%~A| FIND /I "ABC">Nul && (
Echo.Found "ABC" in "%A"
) || ( Echo.Did not find "ABC" )
)
Output From a command:
NLTest | FIND /I "ABC">Nul && ( Echo.Found "ABC" ) || ( Echo.Did not find "ABC" )
As you can see this is the superior way to handle the check for multiple reasons.
If you are detecting for presence, here's the easiest solution:
SET STRING=F00BAH
SET SUBSTRING=F00
ECHO %STRING% | FINDSTR /C:"%SUBSTRING%" >nul & IF ERRORLEVEL 1 (ECHO CASE TRUE) else (ECHO CASE FALSE)
This works great for dropping the output of windows commands into a boolean variable. Just replace the echo with the command you want to run. You can also string Findstr's together to further qualify a statement using pipes. E.G. for Service Control (SC.exe)
SC QUERY WUAUSERV | findstr /C:"STATE" | FINDSTR /C:"RUNNING" & IF ERRORLEVEL 1 (ECHO case True) else (ECHO CASE FALSE)
That one evaluates the output of SC Query for windows update services which comes out as a multiline text, finds the line containing "state" then finds if the word "running" occurs on that line, and sets the errorlevel accordingly.
I'm probably coming a bit too late with this answer, but the accepted answer only works for checking whether a "hard-coded string" is a part of the search string.
For dynamic search, you would have to do this:
SET searchString=abcd1234
SET key=cd123
CALL SET keyRemoved=%%searchString:%key%=%%
IF NOT "x%keyRemoved%"=="x%searchString%" (
ECHO Contains.
)
Note: You can take the two variables as arguments.
To find a text in the Var, Example:
var_text="demo string test"
Echo.%var_text% | findstr /C:"test">nul && (
echo "found test"
) || Echo.%var_text% | findstr /C:"String">nul && (
echo "found String with S uppercase letter"
) || (
echo "Not Found "
)
LEGEND:
& Execute_that AND execute_this
|| Ex: Execute_that IF_FAIL execute this
&& Ex: Execute_that IF_SUCCESSFUL execute this
>nul no echo result of command
findstr
/C: Use string as a literal search string
Best approach for me would be this:
call :findInString "HelloWorld" "World"
:findInString
ECHO.%1 | FIND /I %2>Nul && (
Echo.Found
) || (
Echo.Not found
)
ECHO %String%| FINDSTR /C:"%Substring%" && (Instructions)
Better answer was here:
set "i=hello " world"
set i|find """" >nul && echo contains || echo not_contains
The solutions that search a file for a substring can also search a string, eg. find or findstr.
In your case, the easy solution would be to pipe a string into the command instead of supplying a filename eg.
case-sensitive string:
echo "abcdefg" | find "bcd"
ignore case of string:
echo "abcdefg" | find /I "bcd"
IF no match found, you will get a blank line response on CMD and %ERRORLEVEL% set to 1
Built on #user839791's answer, but I've added a few more things.
#echo off
rem --Set variable below--
set var=condition
rem --Uncomment below line to display contents of variable--
::echo The variable is %var%
rem --Change condition to desired string below--
ECHO.%var%| FIND /I "condition">Nul && (
rem --Occurs if the string is found--
Echo.Variable is "condition"
color C
pause
) || (
rem --Occurs if the string isn't found--
Echo.Variable is not "condition"
color A
pause
)
Yes, We can find the subString in the String:
echo.%data% | FINDSTR /I "POS">Nul && (SET var=POS) || (SET noVar="variable not found")
echo.%data% | FINDSTR /I "TD1">Nul && (SET var=TD1) || (SET noVar="variable not found")
GOTO %var%
:POS
echo processes inside POS
GOTO END
:TD1
echo processes inside TD1
:END

Resources