reading a word in text file via batch files

December 7, 2010 at 08:25:17
Specs: Windows XP
Hi,

I need to write a batch file which can goto each sub-folders under parent directory, read each text file (Each Sub folder has 100 text files) and look for word warning. If the word is found in the file, then delete that file and goto the next one.

This is my folder structure:

Main Folder
Sub Folder - 1_Cell1 - contains 100 text files
Sub Folder - 1_Cell2 - contains 100 text files
.......................
.........................
Sub Folder - 5_Cell6 - contains 100 text files

Please help


See More: reading a word in text file via batch files

Report •


#1
December 7, 2010 at 08:30:39
This is what I have so far:

@echo off
SET retcode = 0

%retcode% = find /C /I "Warning" Rep1.txt

IF "%retcode%" >= 0 (del Rep1.txt)

But find command not returning anything


Report •

#2
December 7, 2010 at 12:27:48
retcode, whatever that is, will not be recognized as a command. This is also not how you set variables: "%retcode% = find /C /I "Warning" Rep1.txt".

Use this:

FOR /f "tokens=*" %%a IN ('FINDSTR /i /m /s "warning" "P:\a\t\h\*.txt"') DO (
	IF "!ERRORLEVEL!"=="0" ECHO DEL "%%a"
)

When you are satisfied it works, remove the "ECHO" on line 2. You also have to change "P:\a\t\h\" to the main directory.

Can I put a question here?


Report •

#3
December 7, 2010 at 13:13:48
Thanks Matt...Its picking up the right file which has warning word but not deleting that file..do you know why?

Report •

Related Solutions

#4
December 7, 2010 at 13:27:41
Did you read what I posted?

When you are satisfied it works, remove the "ECHO" on line 2

Can I put a question here?


Report •

#5
December 11, 2010 at 16:24:40
Hi Matt,

yes I did delete the ECHO but it still just picking up the right files with word warning but its not actually deleting them. Here is the code now:

FOR /f "tokens=*" %%a IN ('FINDSTR /i /m /s "warning" "C:\Test\1_Cell1\*.txt"') DO (
IF "!ERRORLEVEL!"=="0" del %%a
)


Report •

#6
December 11, 2010 at 21:26:52
There is no point in checking the errorlevel, findstr is listing only files that contain "warning".


It may as well just be

... do del "%%a"


Report •

#7
December 12, 2010 at 07:57:54
Perfect...worked like a charm.. :) Thanks a ton

1 more thing as I asked in my Original question that I have multiple Sub-folders (for each cell I have a sub-folder)...How can I goto each sub-folder and delete the files like this?

Right now I'm going directly to 1_Cell1 sub-folder

V


Report •

#8
December 12, 2010 at 08:14:49
Findstr is already subrecursing.....


"C:\Test\1_Cell1\*.txt"

Take away the "1_Cell1" part and everything in "c\test" will be processed.


Report •

#9
December 12, 2010 at 12:52:16
Perfect..thanks a ton

Report •


Ask Question