help!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
glob5
Posts: 34
Joined: 23 Nov 2010 00:47

help!

#1 Post by glob5 » 13 Oct 2011 01:45

I AM NEW TO BAT SCRIPT WORLD. MY SCRIPT DOES NOT WANT TO WORK. MY BAT SCRIPT HAS TO start from a path and RECURSIVELY SEARCH ALL THE FILES OF TYPE .TXT THAT HAS THE WORD "MYWORD!" AND WRITE THEIR NAMES TO A LOG FILE.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: help!

#2 Post by dbenham » 13 Oct 2011 08:53

Code: Select all

@echo off
set "log=output.log"
set "search=MYWORD!"
set "startPath=."
::Dot implies current directory
(for /f "delims=" %%F in ('dir /s /b "%startPath%\*.txt"') do findstr /c:"%search%" "%%F" >nul && echo %%F)>"%log%"


Dave Benham

glob5
Posts: 34
Joined: 23 Nov 2010 00:47

Re: help!

#3 Post by glob5 » 15 Oct 2011 11:38

How about if I look for a phrase which has two words like this: set "search=MYWORD1 MYWORD2" . Notice the space.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: help!

#4 Post by nitt » 15 Oct 2011 14:30

glob5 wrote:I AM NEW TO BAT SCRIPT WORLD. MY SCRIPT DOES NOT WANT TO WORK. MY BAT SCRIPT HAS TO start from a path and RECURSIVELY SEARCH ALL THE FILES OF TYPE .TXT THAT HAS THE WORD "MYWORD!" AND WRITE THEIR NAMES TO A LOG FILE.


This seems like trollish spam, but I will take it seriously.

Code: Select all

@echo off
dir /b /a /s | find "MYWORD!" > ~tmp.txt
type ~tmp.txt | find ".txt" > log.txt
type ~tmp.txt | find ".TXT" >> log.txt
del ~tmp.txt


That will search all directories and list files containing "MYWORD!" and will only list them if the extensions are ".txt" or ".TXT".

glob5
Posts: 34
Joined: 23 Nov 2010 00:47

Re: help!

#5 Post by glob5 » 15 Oct 2011 18:37

can we search for phrase like "MYWORD1 MYWORD2"?.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: help!

#6 Post by aGerman » 16 Oct 2011 09:13

What about

Code: Select all

>"my.log" findstr /msc:"MYWORD1 MYWORD2" "C:\wherever\*.txt"

Note that it's searching in a case-sensitive manner (that could be a problem because it seems your caps lock key is out of order). Otherwise you have to include the /i option (/imsc:).

Regards
aGerman

Post Reply