Batch file help - adding output from command to csv file - batch-file

I currently have a batch file that reads a list of computer names and pings each of these and outputs the ones that reply to a csv file with the computer name and ip address.
I now need to edit this to also find out the user of the machine. I need to contact users which are online to arrange some work done to their computer. Their can be over a hundred machines in the batch file so to manually find out each user takes time. Is there a way to do this?
`IF EXIST C:\test\new.csv (del C:\test\new.csv)
IF EXIST C:\test\final.csv (del C:\test\final.csv)
set ComputerList=C:\test\ClientList.txt
Echo Computer Name,IP Address>Final.csv
setlocal enabledelayedexpansion
echo please wait...
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo %%A,!IPadd:~0,-1!>>final.csv
)
)
findstr /V "IPAddress" final.csv >> C:\test\new.csv
echo identified machines for Install
start excel C:\test\new.csv
echo opened csv file`
The command I want to use to get the username is:
`wmic.exe /NODE: %%A COMPUTERSYSTEM GET USERNAME`
Thanks
Mark

Here is a function I wrote to do just what you are trying to do:
:GetLoggedInUser comp user
for /f %%u in (
'wmic /NODE:"%1" Computersystem get username^|find "\"') do (
if not errorlevel 1 ( for /f "tokens=2 delims=\" %%a in (
'wmic /NODE:"%1" Computersystem get username^|find "\"' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
) ELSE (for /f "skip=1" %%a in (
'wmic /NODE:"%1" Computersystem get username' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
))
Exit /b
Here is my function for pinging. It returns a 0 if the ping succeeded and a 1 otherwise.
:IsPingable comp
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b
Usage example:
for /l %%a in (1,1,255) do (
call:IsPingable 10.6.1.%%a && (
echo ping 10.6.1.%%a used )||( echo ping 10.6.1.%%a unused )
)
And here is for if you're pinging IP's and want to return the hostname as well:
:IsPingable2 comp ret
setlocal
for /f "tokens=2" %%a in (
'"ping -a -n 1 -4 "%~1" | Find "Pinging" 2>nul"') do set name=%%a
endlocal & set %~2=%name%
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b
Usage example:
#echo off
setlocal enabledelayedexpansion
for /l %%a in (1,1,255) do (
call:IsPingable2 10.6.1.%%a host && (
echo ping !host! - 10.6.1.%%a used )||( echo ping !host! - 10.6.1.%%a unused )
)
I just posted these because they just might come in handy for this type of thing in the future. You can use the :IsPingable now though.
You would use it like this in your code:
IF EXIST C:\test\final.csv (del C:\test\final.csv)
set ComputerList=C:\test\ClientList.txt
Echo Computer Name,IP Address,Logged In User>Final.csv
setlocal enabledelayedexpansion
echo please wait...
echo.
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|find "TTL="') do (
if not errorlevel 1 (
set IPadd=%%B
call :GetLoggedInUser %%B uname
echo %%A,!IPadd:~0,-1!,!uname!>>final.csv
)
)
)
echo identified machines for Install
start excel C:\test\final.csv
echo opened csv file
goto :eof
:GetLoggedInUser comp user
for /f %%u in (
'wmic /NODE:"%1" Computersystem get username^|find "\"') do (
if not errorlevel 1 ( for /f "tokens=2 delims=\" %%a in (
'wmic /NODE:"%1" Computersystem get username^|find "\"' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
) ELSE (for /f "skip=1" %%a in (
'wmic /NODE:"%1" Computersystem get username' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
))
Exit /b

The below code will count the number of lines in two files sequentially and is set to the variables SalaryCount and TaxCount.
#ECHO OFF
echo Process started, please wait...
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set SalaryCount=%%C
echo Salary,%SalaryCount%
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set TaxCount=%%C
echo Tax,%TaxCount%
Now if you need to output these values to a csv file, you could use the below code.
#ECHO OFF
cd "D:\CSVOutputPath\"
echo Process started, please wait...
echo FILENAME,FILECOUNT> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set Count=%%C
echo Salary,%Count%>> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set Count=%%C
echo Tax,%Count%>> SUMMARY.csv
The > will overwrite the existing content of the file and the >> will append the new data to existing data. The CSV will be generated in D:\CSVOutputPath

Related

Formatting results in .CMD

I am attempting to create a basic script that runs and checks for basic system info but I want the output to be formatted so the results are on the same line and easily readable.
#Echo Off
systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
wmic cpu get name, status
systeminfo | findstr /C:"Total Physical Memory"
#echo off & setlocal ENABLEDELAYEDEXPANSION
SET "volume=C:"
FOR /f "tokens=1*delims=:" %%i IN ('fsutil volume diskfree %volume%') DO (
SET "diskfree=!disktotal!"
SET "disktotal=!diskavail!"
SET "diskavail=%%j"
)
FOR /f "tokens=1,2" %%i IN ("%disktotal% %diskavail%") DO SET "disktotal=%%i"& SET "diskavail=%%j"
ECHO(Total Space: %disktotal:~0,-9% GB
ECHO(Available Space: %diskavail:~0,-9% GB
systeminfo | find "System Boot Time:"
systeminfo | find "System Type:"
Echo Antivirus: & wmic /node:localhost /namespace:\\root\SecurityCenter2 path AntiVirusProduct Get DisplayName | findstr /V /B /C:displayName || echo No Antivirus installed
The main example of this would be the wmic command placing the result on the next line rather than the same line.
Also any tips of better ways of scripting what I currently have would be appreciated.
#echo off
setlocal
for /f "tokens=1,* delims=:" %%A in ('systeminfo') do (
if "%%~A" == "OS Name" (
call :print "%%~A" %%B
) else if "%%~A" == "OS Version" (
call :print "%%~A" %%B
call :cpu
) else if "%%~A" == "Total Physical Memory" (
call :print "%%~A" %%B
call :fsutil C:
) else if "%%~A" == "System Boot Time" (
call :print "%%~A" %%B
) else if "%%~A" == "System Type" (
call :print "%%~A" %%B
)
)
:next
call :antivirus
pause
exit /b
:print
setlocal enabledelayedexpansion
set "name=%~1"
if not defined name exit /b
set "name=%~1 "
set "full=%*"
set "full=!full:,=!"
set "data="
set "skip1="
for %%A in (!full!) do (
if not defined skip1 (
set "skip1=defined"
) else if not defined data (
set "data=%%~A"
) else (
set "data=!data! %%~A"
)
)
echo !name:~,30!: !data!
exit /b
:antivirus
setlocal enabledelayedexpansion
set "antivirus="
for /f "tokens=*" %%A in ('
2^>nul wmic /node:localhost
/namespace:\\root\SecurityCenter2 path AntiVirusProduct
Get DisplayName /value
^| findstr /i /b /c:"DisplayName" ^|^| echo No Antivirus installed
') do set "antivirus=%%~A"
if /i "!antivirus:~,12!" == "DisplayName=" set "antivirus=!antivirus:~12!"
call :print Antivirus "!antivirus!"
exit /b
:cpu
for /f "tokens=1,* delims==" %%A in ('wmic cpu get name^, status /value') do (
call :print "%%~A" %%B
)
exit /b
:fsutil
setlocal enabledelayedexpansion
2>nul >nul net session || exit /b
for /f "tokens=1,* delims=:" %%A in ('2^>nul fsutil volume diskfree %1') do (
set "name=%%~A"
set "value=%%~B"
call :print "!name:bytes=GBs!" !value:~,-9!
)
exit /b
The fsutil command outputs alittle different.
I chose an easy option if it is OK.
Unsure of antivirus output as you need both results of
installed or not to be sure.
It only runs systeminfo once to save time.
Look at set /? for information about substitution that
is used.
The net session command is used to test if script is run as Admin.
If not Admin, then fsutil will be skipped as it requires Admin.
Look at for /? for usage of the command that is used in the script.
Use of enabledelayedexpansion is used to prevent special
characters being exposed which may cause error otherwise.
And used in code blocks to delay expansion as needed.
Remove comma from value strings which get replaced with a space.
A comma is often used as thousands separator and numbers look odd
with a space instead. This occurs because of usage of a simple
for loop interpreting comma as a argument separator. Some other
chararters may also do this though perhaps a space maybe better
than none in their case.
The output of WMIC is unicode !
The trailing <CR> can be removed by passing the value through another FOR /F loop. This also removes the phantom "blank" line (actually a <CR>)
#Echo Off
Call :GET_CPU name CPU_Name
Set "WMIC_Antivirus=wmic /node:localhost /namespace:\\root\SecurityCenter2 path AntiVirusProduct Get DisplayName ^| findstr /V /B /C:displayName"
#For /F "delims=" %%I in ('%WMIC_Antivirus%') do (
for /f "delims=" %%A IN ("%%I") DO SET "Antivirus=%%A"
)
echo CPU : %CPU_Name%
echo Antivirus : %Antivirus%
pause & exit
::----------------------------------------------------
:GET_CPU
Set "WMIC_CPU=wmic cpu get name /Value"
FOR /F "tokens=2 delims==" %%I IN (
'%WMIC_CPU% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::----------------------------------------------------
You can try to save the output into a text file :
#Echo Off
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
REM --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >nul 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO **************************************
ECHO Running Admin shell... Please wait...
ECHO **************************************
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
SetLocal EnableDelayedExpansion
set "Log=%~dp0Log.txt"
If exist "%Log%" Del "%Log%"
Call :GET_CPU name CPU_Name
Set "WMIC_Antivirus=wmic /node:localhost /namespace:\\root\SecurityCenter2 path AntiVirusProduct Get DisplayName ^| findstr /V /B /C:displayName"
#For /F "delims=" %%I in ('%WMIC_Antivirus%') do (
for /f "delims=" %%A IN ("%%I") DO SET "Antivirus=%%A"
)
(
Echo.
Echo ***************************** General infos ***********************************
Echo.
Echo Running under: %username% on profile: %userprofile%
Echo Computer name: %computername%
Echo.
systeminfo
Echo Operating System:
Echo PROCESSOR ARCHITECTURE : %PROCESSOR_ARCHITECTURE%
echo NUMBER_OF_PROCESSORS : %NUMBER_OF_PROCESSORS%
echo PROCESSOR_IDENTIFIER : %PROCESSOR_IDENTIFIER%
echo PROCESSOR_LEVEL : %PROCESSOR_LEVEL%
echo PROCESSOR_REVISION : %PROCESSOR_REVISION%
echo OS TYPE : %OS%
echo(
echo Program files path : %Programfiles%
echo Program files(86^) path : %Programfiles(86^)%
echo ProgramW6432 path : %ProgramW6432%
echo PSModulePath : %PSModulePath%
echo SystemRoot : %SystemRoot%
echo Temp Folder : %Temp%
echo CPU : !CPU_Name!
echo Antivirus : !Antivirus!
Echo.
Echo **************************** Drives infos *************************************
Echo.
Echo Listing currently attached drives:
wmic logicaldisk get caption,description,volumename | find /v ""
Echo.
Echo Physical drives information:
for /F "tokens=1-3" %%A in ('fltmc volumes^|find ":"') do echo %%A %%B %%C
)>>"%Log%"
Start "" "%Log%" & exit
::----------------------------------------------------
:GET_CPU
Set "WMIC_CPU=wmic cpu get name /Value"
FOR /F "tokens=2 delims==" %%I IN (
'%WMIC_CPU% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::----------------------------------------------------

Parse output of Printing Admin Scripts

For some reason or another when I run the code below it truncates the very end of the string I am echoing at the end. Is there a way to prevent that?
#echo off
chdir C:\Windows\System32\Printing_Admin_Scripts\en-US
For /f "skip=1 delims=" %%a in ('wmic computersystem get name') do for /f "delims=" %%b in ("%%a") do set "compName=%%a"
Set comp=%compName:~0,5%
Set pName=%comp%P1
For /f "delims=" %%a in ('cscript prnmngr.vbs -l ^|findstr /i /b /n /c:"printer name" ^|findstr /b "6:"') do set printerName0=%%a
For /f "delims=" %%a in ('cscript prnmngr.vbs -l ^|findstr /i /b /n /c:"printer name" ^|findstr /b "25:"') do set printerName1=%%a
For /f "delims=" %%a in ('cscript prnmngr.vbs -l ^|findstr /i /b /n /c:"printer name" ^|findstr /b "44:"') do set printerName2=%%a
If "%printerName0%" == "6:Printer name %pName%" (
For /f "delims=" %%a in ('cscript prnmngr.vbs -l ^|findstr /i /b /n /c:"driver name" ^|findstr /b "8:"') do set driverName=%%a
)
If "%printerName1%" == "25:Printer name %pName%" (
For /f "delims=" %%a in ('cscript prnmngr.vbs -l ^|findstr /i /b /n /c:"driver name" ^|findstr /b "27:"') do set driverName=%%a
)
If "%printerName2%" == "44:Printer name %pName%" (
For /f "delims=" %%a in ('cscript prnmngr.vbs -l ^|findstr /i /b /n /c:"driver name" ^|findstr /b "46:"') do set driverName=%%a
)
If "%printerName0%" == "6:Printer name %pName%" (
set pDriver=%driverName:~14%
)
If "%printerName1%" == "25:Printer name %pName%" (
set pDriver=%driverName:~15%
)
If "%printerName2%" == "44:Printer name %pName%" (
set pDriver=%driverName:~15%
)
echo %pDriver%
pause
Using my original script I was able to prevent the ) from being clipped by inserting "" around the variable being set in the final If Check and then removing them from the command I am inputting the variable into.
If "%printerName0%" == "6:Printer name %pName%" (
set pDriver="%driverName:~14%"
)
If "%printerName1%" == "25:Printer name %pName%" (
set pDriver="%driverName:~15%"
)
If "%printerName2%" == "44:Printer name %pName%" (
set pDriver="%driverName:~15%"
)
Where the variable is getting used later on:
cscript prndrvr.vbs -d -m %pDriver%
No quotes are needed as the are included in the first section of code where I set the final variable name. Where as this answer does require a few more lines of code it runs faster.
The Reason For The Issue:
When ) came up in the variable string Batch was seeing it as the end of the If Check and not as an included character in the variable string. In order to retain and prevent unwanted results from the code " " must encase the variable.
This is what I think you are trying to do. Was really baffled by all your extra findstr code and set commands.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
PUSHD C:\Windows\System32\Printing_Admin_Scripts\en-US
SET I=0
For /f "tokens=1,2* delims= " %%G in ('cscript //Nologo //H:CScript prnmngr.vbs -l ^|findstr /i /c:"printer name"') do (
SET /A I+=1
SET "PRINTER.NAME[!I!]=%%I"
For /f "tokens=1,2* delims= " %%J in ('cscript //Nologo //H:CScript prncnfg.vbs -g -p "%%I" ^|findstr /i /c:"driver name"') do (
SET "PRINTER.DRIVER[!I!]=%%L"
)
)
FOR /L %%I IN (1,1,%I%) DO (
ECHO PRINTER %%I = !PRINTER.NAME[%%I]!
ECHO DRIVER %%I = !PRINTER.DRIVER[%%I]!
)
popd
pause
Output on my computer.
PRINTER 1 = Microsoft XPS Document Writer
DRIVER 1 = Microsoft XPS Document Writer
PRINTER 2 = Fax
DRIVER 2 = Microsoft Shared Fax Driver
PRINTER 3 = \\SERVER_NAME\WI5140-XEROX-9
DRIVER 3 = Xerox WorkCentre 5330 PCL6
PRINTER 4 = \\SERVER_NAME\WI5101-XEROX-12
DRIVER 4 = Xerox WorkCentre 5330 PS
PRINTER 5 = \\SERVER_NAME\WI5101-XEROX-1
DRIVER 5 = Xerox WorkCentre 7225 PS
PRINTER 6 = \\SERVER_NAME\WI5140-XEROX-31
DRIVER 6 = Xerox ColorQube 8870DN PS
PRINTER 7 = \\SERVER_NAME\WI5140-XEROX-15
DRIVER 7 = Xerox Phaser 5550DN PS
Press any key to continue . . .
EDIT: 20170225 14:35 CST
As you have said in previous comments you are only looking for one printer driver and I have said in previous comments you can do that with EXACTLY one FOR /F command since you already know the name of the printer. You don't need to use prnmngr.vbs to get the driver because prncnfg.vbs allows you to list the configuration for a printer and then you can use findstr to just get the driver. You know the name of the printer so you use that as your input to prncnfg.vbs to list the configuration for that printer.
For /f "tokens=1,2* delims= " %%J in ('cscript //Nologo //H:CScript prncnfg.vbs -g -p "%pName%" ^|findstr /i /c:"driver name"') do set "pDriver=%%L"
EDIT 20170225 15:20 CST
Lets brute force to get the driver with prnmngr.vbs since that seems to be what you want to use to get the driver.
Set "pName=%comp%P1"
set "flag="
For /f "tokens=1,2* delims= " %%G in ('cscript //nologo prnmngr.vbs -l ^|findstr /i /b /C:"printer name" /C:"driver name"') do (
IF DEFINED flag set "pDriver=%%I" &GOTO :BREAK
IF "%pName%"=="%%~I" SET flag=1
)
:BREAK
echo %pDriver%
pause

For file loop in a for file loop batch file

I have a program to ping computers, check there registry, and tell me the results.
I am now trying to script it so that I don't have to know the ip address just the host name of the computer.
I found a script to give me the ip off of a hostname
for /f "tokens=1,2 delims=[]" %%a in ('ping -4 %%a ^| find "Pinging"') do set ip=%%c >nul
I have tried to simply insert this into a biger loop that uses a file to give it the host names.
for /f %%A in (%1) do (
for /f "tokens=1,2 delims=[]" %%a in ('ping -4 %%a ^| find "Pinging"') do (
set host=%%a
set ip=%%c
echo "."
echo %ip% %host%
pause
) >>%2
)
I have also tried it like this,
for /f %%a in (%1) do (
set /p hostname=%%a
for /f "tokens=1,2 delims=[]" %%b in ('ping -4 %%a ^| find "Pinging"') do set ip=%%c >nul
echo %ipaddress% %hostname%
)
Please any help on this would be greatly appreciated.
Thank you.
AFTER MUTCH HEAD BANGING
#echo off
SETLOCAL EnableDelayedExpansion
for /f %%A in (%1) do (
for /f "tokens=1,2 delims=[]" %%a in ('ping /4 /n 1 %%A ^| findstr "Pinging"') do echo "%%A %%b"
)

Unable to get batch script to display IP address, Ping status, and hostname into a single 3 column .csv

I'm trying to ping against a list of websites and get the following output in a .csv, Hostname, success of ping, IP address. So the output might look like this:
Google.com, Successful (or failure), 123.456.789.012 (if failure N/A)
I have two scripts that can do these things seperatley, but can't seem to get them to work together.
Gets IP addresses:
#echo off
set ComputerList=C:\Scripts\pinglist.txt
Echo Computername,IP Address>>pingresult.csv
setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo %%A,!IPadd:~0,-1!>>pingresult.csv
))*
Gets hostname and ping status:
REM Set the "PingList" variable to the location of the text file you want to ping against
set PingList=C:\Scripts\pinglist.txt
REM Set the "Results" variable to the location where you want to pipe your output
set Results=C:\Scripts\pingresult.csv
echo COMPUTERNAME, STATUS >> %Results%
REM failed pings appened to file
for /f "delims=" %%a in (%PingList%) do ping -n 1 %%a >nul && (echo %%a ok) || (echo %%a, failure ) >> %Results%
REM successful pings appened to file
for /f "delims=" %%a in (%PingList%) do ping -n 1 %%a >nul && (echo %%a, success ) >> %Results% || (echo %%a failed to respond)
Reformulated another answer; not optimized:
#ECHO OFF >NUL
#SETLOCAL enableextensions
echo COMPUTERNAME, STATUS, IP
for /F "usebackq delims=" %%i in ("files\30852528.txt") do (
set "_found="
for /F "delims=" %%G in ('
ping -4 -n 1 %%i^|findstr /i /C:"Ping request could not find host"') do (
echo %%i, "Could not find host", ""
set "_found=%%G"
)
if not defined _found (
for /F "tokens=3 delims=: " %%G in ('
ping -4 -n 1 %%i^|findstr /i "TTL="') do (
echo %%i, "ok", %%G
set "_found=%%G"
)
)
if not defined _found (
for /F "tokens=2 delims=[]" %%G in ('
ping -4 -n 1 %%i^|findstr /ib "Pinging"') do (
echo %%i, "Request timed out", %%G
set "_found=%%G"
)
)
if not defined _found (
echo unknown ? %%i
)
)
Output
==>D:\bat\SO\31525308.bat
COMPUTERNAME, STATUS, IP
foo.bar, "Could not find host", ""
google.com, "ok", 173.194.112.100
yahoo.com, "ok", 98.139.183.24
toyota.com, "ok", 95.101.1.91
bmw.com, "Request timed out", 160.46.244.131
==>

Count number of |

I need help with a script, the script is supposed to count the number of | before a specific string.
info.txt
text=jam|hello=123|result=ok|cow=cat|...
So in this example the answer should be 2 if you search for result=
Is this possible in batch?
try this:
#ECHO OFF &SETLOCAL
SET "string=text=jam|hello=123|result=ok|cow=cat|..."
SET "stop=result=ok"
SET "char=|"
SET /a count=-1
SET "org=%string%"
:loop
FOR /f "tokens=1*delims=%char%" %%a IN ("%string%") DO SET "this=%%a"&SET "that=%%b"
IF DEFINED that (SET "string=%that%") ELSE (SET "string=%this%")
SET /a count+=1
IF NOT DEFINED string (ECHO NOT found: "%stop%" &GOTO :EOF)
IF NOT "%this%"=="%stop%" GOTO :loop
ECHO Number of "%char%" IN "%org%" until "%stop%": %count%
This uses a helper batch file called repl.bat: from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
If you call this code below searchstring.bat then you can launch it like this
searchstring "result="
It expects only one match per file and is case sensitive.
#echo off
type "file.txt" | find "%~1" | repl "(.*).%~1.*" "$1" | repl "\x7c" "\r\n" x | find /c /v ""
This batch file below will return a count of line number and the number itself, when the number is greater than zero, per each line in the file.txt
#echo off
if "%~1"=="" ( echo add a search term&pause&goto :EOF)
for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" "file.txt" ') do (
for /f %%c in (' echo "%%b"^| find "%~1" ^| repl "(.*).%~1.*" "$1" ^| repl "\|" "\r\n" x ^| find /c /v "" ') do (
if %%c GTR 0 echo Line %%a: %%c
)
)
pause
If you want, eg, 3rd string:
SET "text=jam|hello=123|result=ok|cow=cat|..."
FOR /F "TOKENS=3" %%t IN ("%text%") DO ECHO %%t
If you want, eg, 3rd string and following:
SET "text=jam|hello=123|result=ok|cow=cat|..."
FOR /F "TOKENS=2,*" %%t IN ("%text%") DO ECHO %%u
Here's another way (uses your info.txt file).
Case insensitive. Handles multiple lines with matching string in file.
#echo off
set "SpecificString=result=ok"
set /A cnt=0
for /F "tokens=*" %%A IN (info.txt) do (
for /F "usebackq tokens=*" %%B IN (`echo."%%A" ^| find /I "%SpecificString%"`) do (
call :Parse "%%~A"
)
)
pause
goto :eof
:Parse
for /F "usebackq tokens=1* delims=^|" %%B IN (`echo."%~1"`) do (
if /I "%%~B"=="%SpecificString%" (
echo.Cnt=%Cnt% in "%%A"
echo.
set /A Cnt=0
goto :eof
)
set /A Cnt+=1
call :Parse "%%~C
)
goto :eof

Resources