How do I echo conditional finds in a for /f

May 24, 2011 at 10:28:36
Specs: Windows Server 2003, 8GB
Using a batch file...I want to search a text file for a specific word and if found I want to echo all the found lines to a new file "UNLESS" that line also has another specific word.
This is what I have tried so far, but it seems to write everything to outbound.txt and not loop through the error handling:

setlocal enabledelayedexpansion
for /f "tokens=* skip=2 " %%b in (D:\Tivoli_Exclude\Filling_Temp.txt) do (
find /i "DIRINFO" \\D:\Tivoli_Exclude\Filling_Temp.txt > NUL
echo %%b >>D:\Tivoli_Exclude\Outbound.txt
) Else (
echo %%b >>D:\Tivoli_Exclude\Keep_Onsite.txt

Contents of input file(Filling_Temp.txt):

000229 CTAPEBACK01 Filling
000528 CTBACK2 Filling
000539 DIRINFO_TA- Filling
000567 CTBACK4 Filling
000605 CTBACK1 Filling
000620 CTAPEBACK01 Filling
000622 CTAPEBACK01 Filling
000630 DIRINFO_TA- Filling
000652 CTBACK2 Filling
000663 CTBACK3 Filling
000670 CTAPEBACK01 Filling
000702 CTBACK2 Filling
000706 CTBACK3 Filling
000754 CTAPEBACK01 Filling
000799 CTBACK1 Filling
000913 CTBACK2 Filling

See More: How do I echo conditional finds in a for /f

Report •

May 24, 2011 at 12:41:40
Standard %vars% don't update in loops, you could use this instead:

if not errorlevel 1 (

It won't work properly with negative errorlevels, but I'm pretty sure that fine only returns zero or greater.

... but if I understand correctly you want all lines that contain "DIRINFO" in one file and lines that don't in another. Th "/v" switch to find could be helpful:


>> D:\Tivoli_Exclude\Outbound.txt find /i "DIRINFO" < D:\Tivoli_Exclude\Filling_Temp.txt
>> D:\Tivoli_Exclude\Keep_Onsite.txt find /v /i "DIRINFO" < D:\Tivoli_Exclude\Filling_Temp.txt

Report •
Related Solutions

Ask Question