Batch file - findstr command fails using multiple strings to search for - windows

This batch file loops through a list of computer IP addresses and passwords with a comma separator (IP_List.txt). Commands are then run to connect remotely to each computer. I've got another list of IP's which are exceptions and need to be skipped, so I've got a variable (set str=) which lists these IP's and I'm using findstr to search for them:
#ECHO OFF
setlocal EnableDelayedExpansion
Pushd "%~dp0"
Set Log=LogFile.log
set str=172.16.66.1 172.16.66.2 172.16.66.3 172.16.66.4
FOR /F "tokens=1,2 delims=," %%i in (IP_List.txt) do call :process %%i %%j
(ECHO+ & ECHO --- END OF REPORT ---) >> %Log%
GOTO :EOF
:process
set IP=%1
set PW=%2
#ECHO %IP% | findstr "%str%" >nul
IF NOT ERRORLEVEL 1 (ECHO+ & ECHO %IP% & ECHO This IP address was skipped) >> %Log% & GOTO :EOF
#ECHO Processing %IP% >> %Log%
---- (remainder of batch file here) ----
There was a problem with findstr in that having 172.16.66.2 on the exception list, findstr was mistakenly finding a match with the following IP's on my overall list: 172.16.66.224, 172.16.66.225, 172.16.66.226, etc.
After changing the code and putting the list of IP exceptions in a text file (IP_Exceptions.txt) I was able to get past the findstr problem and this modified batch file works without issue:
#ECHO OFF
setlocal EnableDelayedExpansion
Pushd "%~dp0"
Set Log=LogFile.log
FOR /F "tokens=1,2 delims=," %%i in (IP_List.txt) do call :process %%i %%j
(ECHO+ & ECHO --- END OF REPORT ---) >> %Log%
GOTO :EOF
:process
set IP=%1
set PW=%2
::~~ Add a right bracket to the end of the IP address variable
SET IPstr=%IP%]%x%
set match=N
::~~ Loop through a list of IP address exceptions
::~~ (A left bracket has been added to the beginning of
::~~ each IP on the list in order to use it as a delimiter)
::~~ Add a right bracket to the end of the variable "%%j"
::~~ in order to look for a match with the findstr command
for /F "delims=[" %%i in (IP_Exceptions.txt) do (
for /F "tokens=1" %%j in ("%%i") do (
ECHO %%j] | findstr "%IPstr%" >nul
IF not errorlevel 1 set match=Y
)
)
IF %match% == Y (ECHO+ & ECHO %IP% & ECHO Skipped this machine) >> %Log% & GOTO :EOF
#ECHO Processing %IP% >> %Log%
:: ---- (remainder of batch file here) ----
The specifics of getting the findstr section to work correctly without false matches (adding brackets to variables, nested for loops with delimiters, etc.) was kind of clumsy though. Any suggestions on making that section more efficient would be greatly appreciated.

A modified batch file works but it's clumsy:
#ECHO OFF
setlocal EnableDelayedExpansion
Pushd "%~dp0"
Set Log=LogFile.log
FOR /F "tokens=1,2 delims=," %%i in (IP_List.txt) do call :process %%i %%j
(ECHO+ & ECHO --- END OF REPORT ---) >> %Log%
GOTO :EOF
:process
set IP=%1
set PW=%2
::~~ Add a right bracket to the end of the IP address variable
SET IPstr=%IP%]%x%
set match=N
::~~ Loop through a list of IP address exceptions
::~~ (A left bracket has been added to the beginning of
::~~ each IP on the list in order to use it as a delimiter)
::~~ Add a right bracket to the end of the variable "%%j"
::~~ in order to look for a match with the findstr command
for /F "delims=[" %%i in (IP_Exceptions.txt) do (
for /F "tokens=1" %%j in ("%%i") do (
ECHO %%j] | findstr "%IPstr%" >nul
IF not errorlevel 1 set match=Y
)
)
IF %match% == Y (ECHO+ & ECHO %IP% & ECHO Skipped this machine) >> %Log% & GOTO :EOF
#ECHO Processing %IP% >> %Log%
:: ---- (remainder of batch file here) ----

Your subroutine could look like this:
...
:process
echo/ %str% |findstr /LC:" %1 " >nul && (
echo %1 skipped
goto :eof
)
echo processing %1 with %2
:: ---- (remainder of batch file here) ----
Note that I reversed the "search" logic (to make it easier to process spaces correctly)
Instead of echo <sub>|findstr <whole>, I switched to echo <whole> |findstr <sub>
Note: the spaces in echo/ %str% |findstr /LC:" %1 " are critical (they are also compared to avoid false positives).
/LC: tells findstr to search literal (treating the dot as a dot instead of a wildcard) and including spaces.

Related

Single line nested loop

I am experimenting with a single line cmd /c to get an inner loop without branching. (Actually i have the showLines routine which performs the loop.) I know its worst for performance but i want to know if its possible to get it run without quotes. Currently it raises "%G was unexpected at this time" error. So, it needs some correct escaping or expansion of variables.
#echo off
setlocal enableDelayedExpansion
set "param=%~1"
netstat -aonb | findstr /n $ > tmpFile_Content
for /F "tokens=*" %%A in ('type tmpFile_Content ^| findstr /r /c:"%param%" /i') do (
SET line=%%A
for /F "tokens=1 delims=:" %%I in ("!line!") DO (
set /a LineNum=%%I
rem set /a NextLineNum=LineNum+1
)
set /a lineNum=!LineNum!-1
if !lineNum!==0 ( set param="tokens=*" ) else ( set param="tokens=* skip=!lineNum!" )
rem FOLLOWING WORKS FINE in quotes
cmd /q /v:on /c "#echo off && setlocal enableDelayedExpansion && set cnt=2 && for /F %%param%% %%B in (tmpFile_Content) do ( echo %%B && set /a cnt-=1 >nul && if ^!cnt^!==0 exit /b )"
rem Following does not work even though cmd should take the rest of arguments after /c
cmd /q /v:on /c setlocal enableDelayedExpansion && FOR /F "tokens=*" %%C IN ('echo !param!') DO ( for /F %%C %%G in (tmpFile_Content) do ( echo %%G && set /a cnt-^=1 >nul && if ^!cnt^!==0 exit /b ))
rem call :showLines !LineNum!
)
del tmpFile_Content
goto :eof
:showLines
set /a lineNum=%1-1
set cnt=2
for /F "tokens=* skip=%lineNum%" %%B in (tmpFile_Content) do (
echo %%B
set /a cnt-=1
if !cnt!==0 goto exitLoop
)
:exitLoop
exit /b
to construct for loops with variable parameters, you essentially need to define and execute them as a macro. Eg:
#Echo Off & Setlocal ENABLEdelayedExpasnion
Set param="tokens=* delims="
Set "test=string line"
Set For=For /F %param% %%G in ("^!test^!") Do echo %%G
%For%
Of course you could go even further, and build the entire for loop with another for loop macro on the fly.
UPDATE:
Method for defining conditional concatenation of commands now exampled
Syntax simplified to allow the same usage form for regular expansion and within codeblocks by having the constructor macro call a subroutine to expand the new for loop once it's constructed.
delayed concatenation variable usage simplified to avoid the escaping requirement
#Echo off
::: { Macro Definition
Setlocal DisabledelayedExpansion
(set \n=^^^
%= This creates an escaped Line Feed - DO NOT ALTER =%
)
::: [ For Loop Constructor macro. ] For advanced programmers who need to use dynamic for loop options during code blocks.
::: - usage: %n.For%{For loop options}{variable set}{For Metavariable}{commands to execute}
::: - use delayed !and! variable to construct concatenated commands in the new for loop.
Set n.For=For %%n in (1 2) Do If %%n==2 (%\n%
Set FOR=%\n%
For /F "tokens=1,2,3,4 Delims={}" %%1 in ("!mac.in!") Do (%\n%
Set "FOR=For /F %%1 %%3 in ("!%%2!") Do (%%~4)"%\n%
)%\n%
Call :Exc.For%\n%
)Else Set mac.in=
Set "and.=&&"
Set "and=!and.!"
::: } End macro definition.
Setlocal EnableDelayedExpansion& rem // required to expand n.For constructor macro
::: - Usage examples:
Set "example=is a string line"
%n.For%{"tokens=* delims="}{example}{%%G}{Echo/%%~G}
%n.For%{"tokens=1,2,3,4 delims= "}{example}{%%G}{"Echo/%%~J %%~G %%~H %%~I !and! Echo/%%~I %%~G %%~H %%~J"}
Set "example2=Code block example"
For %%a in (1 2 3) do (
%n.For%{"Tokens=%%a Delims= "}{example2}{%%I}{"For /L %%# in (1 1 4) Do (Set %%I[%%#]=%%a%%#) !and! Set %%I[%%#]"}
)
Pause > Nul
Goto :EOF
:Exc.For
%FOR%
Exit /B
Example output:
is a string line
line is a string
string is a line
Code[1]=11
Code[2]=12
Code[3]=13
Code[4]=14
block[1]=21
block[2]=22
block[3]=23
block[4]=24
example[1]=31
example[2]=32
example[3]=33
example[4]=34
Finally, i came up with following to execute code given in string for anyone interested experimentally and may be for some insight about escaping and expansion.
I use macro instead of cmd which will be much more faster i think(not sure because its said that "call" also causes launch of cmd).
So, it is a simple one-liner without a lot of extra code. But things easily become complicated and when extra escaping and special characters used then #T3RR0R's macro routine would be a necessity.
#echo off
setlocal EnableDelayedExpansion
set "param=%~1"
netstat -aonb | findstr /n $ > tmpFile_Content
for /F "tokens=*" %%A in ('type tmpFile_Content ^| findstr /r /c:"%param%" /i') do (
SET line=%%A
for /F "tokens=1 delims=:" %%I in ("!line!") DO (
set /a LineNum=%%I
rem set /a NextLineNum=LineNum+1
)
set /a lineNum=!LineNum!-1
rem CORRECT QUOTING
if !lineNum!==0 ( set "param="tokens=*"" ) else ( set "param="tokens=* skip=!lineNum!"" )
rem FOLLOWING WORKS FINE in quotes
rem cmd /q /v:on /c "set cnt=2 && for /F ^!param^! %%B in (tmpFile_Content) do ( echo %%B && set /a cnt-=1 >nul && if ^!cnt^!==0 exit /b )"
rem For reading !cnt! use !x!cnt!x!.
rem Only one extra variable used, and in routine its replaced with !(exclamation) for our "cnt" variable.
set "x=^!x^!"
call :ExecCode "set cnt=2 && for /F ^!param^! %%%%B in (tmpFile_Content) do (echo %%%%B && set /a cnt=!x!cnt!x!-1 >nul && if !x!cnt!x!==0 (exit /b) )"
rem call :showLines !LineNum!
)
del tmpFile_Content
goto :eof
:ExecCode
setlocal
rem Replace with exclamation for variable
set "x=^!"
set "s=%~1"
%s%
endlocal
exit /b
:showLines
set /a lineNum=%1-1
set cnt=2
for /F "tokens=* skip=%lineNum%" %%B in (tmpFile_Content) do (
echo %%B
set /a cnt-=1
if !cnt!==0 goto exitLoop
)
:exitLoop
exit /b

Using Findstr with specific range or other method

I only want to find specific lines in his text file, so i figure that using range would be a good idea. But i can seem to find any tutorial online. please help..
An example of the text file
XXX scan report for 192.0.0.0
exampleexampleexampleexampleexample
OS: windows 8
exampleexampleexampleexample
exampleexampleexampleexample
PORT STATE SERVICE VERSION
21/tcp close ftp
80/tcp open http Microsoft ISS
exampleexampleexampleexample
exampleexampleexampleexample
XXX scan report for 192.0.0.1
exampleexampleexampleexampleexample
exampleexampleexampleexample
exampleexampleexampleexample
PORT STATE SERVICE VERSION
21/tcp close ftp
80/tcp open http Microsoft ISS
exampleexampleexampleexample
exampleexampleexampleexample
I wanted to get IP address, OS Details and Port status into a spreadsheet.
My code:
#echo off
set file=C:\Users\1.txt
set match=report for
set match1=OS
findstr /c:"%match%" %file%
findstr /c:"%match1%" %file%
findstr /c:"tcp " /c:"udp " %file%
for /f "tokens=1-5" %%a in ('findstr /c:"%match%" %file%') do (
echo "IP Address:","%%e" >> report1.csv
for /f "tokens=1,2 delims=:*" %%a in ('findstr /c:"%match1%" %file%') do
(
echo "Operating System: ","%%b" >> report1.csv
echo "PORT","STATE","SERVICE","VERSION" >> report1.csv
for /f "tokens=1-4 delims=: " %%a in ('findstr /c:"tcp " /c:"udp "
%file%') do (
echo "%%a","%%b","%%c","%%d" >> report1.csv
)
)
)
There is a big problem with this code and it is in the for loop.
The code will get the first ip then will proceed to get the os details, but not ever ip have the os details, so the os details will be placed in the wrong ip.
Another problem with the code is that it will list all os and port details under one ip address. And the next ip address will also be the same, it will have all the os and port details as well.
Please do help me to solve this problem. Or is there any other method like call?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q43335765.txt"
SET "outfile=%destdir%\outfile.txt"
>"%outfile%" ECHO IP,OS,PORT,STATUS,SERVICE,VERSION
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
CALL :process %%a
)
GOTO :EOF
:process
ECHO %*|FIND "scan report for " >NUL
IF NOT ERRORLEVEL 1 GOTO newip
IF "%~1"=="OS:" SET "$os=%*"&GOTO :eof
ECHO %~1|FIND "/">NUL
IF ERRORLEVEL 1 GOTO :EOF
FOR /f "tokens=1,2,3*" %%p IN (
"%*") DO >>"%outfile%" ECHO %$ip%,%$os:*: =%,%%p,%%q,%%r,%%s
GOTO :eof
:newip
IF "%~2" neq "" shift&GOTO newip
SET "$ip=%~1"
SET "$os=: "
GOTO :eof
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used a file named q43335765.txt containing your data for my testing.
Produces the file defined as %outfile%
Having established the filenames, put the header line in the output file and process each line of the file through :process
In :process, detect the key string indicating a ne data item. If found, go to :newip which simply shuffles the data line along until only the last entry remains and assign this last entry to $ip. Set $ip to :Space + any special string you want to represent "not found" (like unknown for instance)
If the line doesn't contain the keystring, see whether the first token is OS:, and set $os to the entire original line if it is.
Otherwise, lookk for a / in the first token. If it's not there, abandon processing this line, otherwise simply tokenise the line, selecting the first to thid and rest and output using the saved ip, the saved os, except for the part before the first :Space and the four data line entries, all separated by commas.
Revision to cater for | in data - replace with /
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q43335765.txt"
SET "outfile=%destdir%\outfile.txt"
>"%outfile%" ECHO IP,OS,PORT,STATUS,SERVICE,VERSION
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
SET "line=%%a"
CALL :preprocess
)
GOTO :EOF
:preprocess
SET "line=%Line:|=/%"
CALL :process %line%
GOTO :eof
:process
ECHO %*|FIND "scan report for " >NUL
IF NOT ERRORLEVEL 1 GOTO newip
IF "%~1"=="OS:" SET "$os=%*"&GOTO :eof
ECHO %~1|FIND "/">NUL
IF ERRORLEVEL 1 GOTO :EOF
FOR /f "tokens=1,2,3*" %%p IN (
"%*") DO >>"%outfile%" ECHO %$ip%,%$os:*: =%,%%p,%%q,%%r,%%s
GOTO :eof
:newip
IF "%~2" neq "" shift&GOTO newip
SET "$ip=%~1"
SET "$os=: "
GOTO :eof
Shows the need to provide a representative data sample...
With characters that have special meaning like |, assign to a variable and call a preprocessor to convert all | to / (or whatever else is desired) and then call the process with the result.
Formula: set "var=%somevar:string1=string2%"
will assign to var the value of somevar with all occurrences of string1 replaced by string2. The enclosing quotes in a set command ensure that any stray trailing spaces on the line are not included in the value assigned.
The flaw in your logic is that each time you execute findstr, it starts back at line 1 of your text file. If you're working with multi-line records, you have to build your line of output with in one pass of the record. I suggest a for /F loop and testing tokens is better suited to this than findstr. Here's a suggestion:
#echo off
setlocal
set "file="C:\Users\1.txt"
>"report1.csv" (
echo "IP Address","Operating System","port 21","port 80"
for /F "usebackq tokens=1-3,5" %%I in ("%file%") do (
rem // check if line begins a record
if /i "%%~J"=="scan" if /i "%%~K"=="report" (
set "IP=%%~L"
set "OS="
)
rem // check if line contains OS
if /i "%%~I"=="OS:" set OS="%%~J %%~K"
rem // check if line contains port 21
if /i "%%~I"=="21/tcp" set "state21=%%~J"
rem // port 80 indicates end of needed info in record
setlocal enabledelayedexpansion
if /i "%%~I"=="80/tcp" echo(!IP!,!OS!,!state21!,%%~J
endlocal
)
)

Windows - findstr in for loop (file content)

I have a text file which containts stuff like
M test123
S test
M abc
and so on...
I'm trying to write a batch script that will do the following:
Read this text file, search every line for "M " (with spaces!) and then save the found line in a variable, delete the "M " and store the output in a seperate output.txt
So the output.txt should containt then following:
test123
S test
abc
Here's what I have so far:
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (output_whole_check.txt) DO (
SET var!count!=%%F
findstr /lic:"M " > nul && (set var!count!=var!count!:~8%) || (echo not found)
SET /a count=!count!+1
)
ENDLOCAL
Or is there some easier way to solve that without any additional stuff installed on windows?
Try this one. It echoes all lines to output.txt with "M " replaced with nothing.
#echo off & setlocal
>output.txt (
FOR /F "usebackq delims=" %%I IN ("output_whole_check.txt") DO (
set "line=%%I"
setlocal enabledelayedexpansion
echo(!line:M =!
endlocal
)
)
Result:
test123
S test
abc
Or if your output_whole_check.txt is very large, it might be faster to loop over the lines using a for /L loop. for /L is more efficient than for /F. You just have to get a count of the lines to determine how many iterations to loop.
#echo off & setlocal
rem // get number of lines in the text file
for /f "tokens=2 delims=:" %%I in ('find /v /c "" "output_whole_check.txt"') do set /a "count=%%I"
<"output_whole_check.txt" >"output.txt" (
for /L %%I in (1,1,%count%) do (
set /P "line="
setlocal enabledelayedexpansion
echo(!line:M =!
endlocal
)
)
The result is the same output.

Issue while reading Input from a file and passing it to a file in Batch Processing

I have an issue while reading from a input file the below shown values(FILE.txt) .
The < > symbols cause an improper read . I tired using ^(escape character but its of no use ,I also tried Double quotes("") and the result is same messed up output.
Is there anyway I can read the contents of the file in iteration with the < > = characters .
Please help
FILE.txt contains
A;Select * from TablenameA where time=>yesterdaytime and time<endtime;XXX;YYY
B;Select * from TablenameB where time=>yesterdaytime and time<endtime;AAA;YYY
C;Select * from TablenameC where time=>yesterdaytime and time<endtime;BBB;YYY
--------------SCRIPT----------------------------------
SET vFILENAME=FILE.txt
for /F "tokens=1,2,3,4 delims=;" %%i in (%vFILENAME%) do call :process "%%i" "%%j" "%%k" "%%l"
goto END
:process
set VAR1=%~1
set VAR2=%~2
set VAR3=%~3
set VAR4=%~4
some processing on VAR1,2,3,4 and then
echo PrmOne=%VAR1%>Output_%VAR1%.txt
echo PrmTWO=%VAR2%>>Output_%VAR1%.txt
echo PrmTHREE=%VAR3%>>Output_%VAR1%.txt
echo PrmFOUR=%VAR4%>>Output_%VAR1%.txt
-----OutPut(new for each iteration) File will contain values like this for each iteration----------------
PrmOne=A
PrmTWO=Select * from TablenameA where time=>yesterdaytime_UPDATED and time<endtime_UPDATED
PrmTHREE=XXX
PrmFOUR=YYY
Solution was provided for this But need the output in the above mentioned manner please help
Thanks to #Aacini
#echo off
SET vFILENAME=FILE.txt
for /F "tokens=1,2,3,4 delims=;" %%i in (%vFILENAME%) do call :process "%%i" "%%j" "%%k" "%%l" < NUL
goto :EOF
:process
set "VAR1=%~1"
set "VAR2=%~2"
set "VAR3=%~3"
set "VAR4=%~4"
set /P "=%VAR1%" & echo/
set /P "=%VAR2%" & echo/
set /P "=%VAR3%" & echo/
set /P "=%VAR4%" & echo/
You can use delayed expansion to avoid problems with variable content output
>"Output_%VAR1%.txt" (
setlocal enabledelayedexpansion
echo PrmOne=!VAR1!
echo PrmTWO=!VAR2!
echo PrmTHREE=!VAR3!
echo PrmFOUR=!VAR4!
endlocal
)
Slight variation from Aacini's original code. Not necessary to call a function. If you need to call the function then adjust accordingly.
#echo off
SET vFILENAME=FILE.txt
for /F "usebackq tokens=1,2,3,4 delims=;" %%i in ("%vFILENAME%") do (
>>%%~i.txt (
set /P "=PrmOne=%%~i" & echo/
set /P "=PrmTwo=%%~j" & echo/
set /P "=PrmThree=%%~k" & echo/
set /P "=PrmFour=%%~l" & echo/
)<nul
)

Batch script help, script exiting after parentheses

I wrote a very simple script to output the host machine's MAC addresses to a text file.
The script is exiting right after line 3 - 'IF DEFINED WRITEOK ('.
#echo off
cls
copy /Y NUL "%CD%\.writable" > NUL 2>&1 && set WRITEOK=1
IF DEFINED WRITEOK (
rem ---- we have write access ----
set DIR=%CD%\interfaces
set FILE=%DIR%\%USERNAME%.txt
IF NOT EXIST "%DIR%" (
MKDIR "%DIR%"
echo DIR '%DIR%' was created
) else (
echo DIR '%DIR%' already exists
) for /f "tokens=2 delims=:" %%i in ('ipconfig /all ^| findstr /i "Physical Host"') do (
echo %%i >> "%FILE%"
echo OUTPUT written to '%FILE%'
)
) else (
rem ---- we don't ----
echo DIR '%DIR%' is not writable
)
echo.
echo DONE!
pause
Try to put the FOR one line after the closing parenthesis :
...)
for /f "tokens=2 delims=:" %%i in ('ipconfig /all ^| findstr /i "Physical Host"') do (...
you can't start a FOR with a closing parenthesis in front :
This will not work :
(echo 1
) for /l %%a in (1,1,10) do echo %%a
and this will work :
(echo 1
)
for /l %%a in (1,1,10) do echo %%a
EDIT 1 :
For the path variables containing space use double quote :
"%cd%"
when using it.

Resources