1.

Solve : Using find to Search files in Different directories for a string?

Answer»

Hello I have a windows directory structure that contains some logs I want to search. With in the logs folder there is user1...user2... sub folders. I would like to point to the top level "logs" folder and have the .bat file search the subfolders automatically. Right now I have to point to the user folder. Here is the code I have

Code: [Select]echo off
echo.
set /p thesearchfolder= "Folder to search for data (Drag folder into window): "
find "Error" %thesearchfolder%\*.txt > "%TEMP%\errorlog.txt"


echo "%TEMP%\errorlog.txt"
notepad "%TEMP%\errorlog.txt"

Any help great appreciated.Others will probably come up with better answers but you could do something like this.
a) use dir with /s and redirect output to a text file. This gives you a list of all files in all subfolders.
b) use a for loop and use find on each file read from the text file.Thanks for the reply emanresu

How would they look in terms of code. I tried this but it didnt work

Code: [Select]for /d %f in (%thesearchfolder%\*) do find "Error" %f\*.txt > "%TEMP%\errorlog.txt"
Guess I'd write something like this (untested!)...

Code: [Select]dir /w /s  %thesearchfolder%\*.txt  >  tempdir.txt

for /f "skip=5 tokens=1,2,3" %%f in (tempdir.txt) do find "Error" %%g.%%h >> %TEMP%\errorlog.txt
emanresu is on the right track with his suggestions. Your output errorlog.txt file should also be more helpful when errors are found, so I've taken some artistic license and MODIFIED the code a touch. See if it works for you.

Code: [Select]echo off
set /p thesearchfolder="Folder to search for data (Drag folder into window): "

for /f "delims=" %%A in ('dir %thesearchfolder% /s /b') do (
  echo %%A >> %TEMP%\errorlog.txt
  findstr /i /n "Error" %%A >> %TEMP%\errorlog.txt
  if errorlevel 1 (echo 0 errors found >> %TEMP%\errorlog.txt)
)

echo %TEMP%\errorlog.txt
start %TEMP%\errorlog.txt
  Have modified my, still untested, code above. However, Raven has given a much more elegant solution.   HAY Raven Thanks a lot for the nice solution one last question on the "findstr" It turns out the actual error string I want to search for is
"Error bad" I tried to add option to the findstr command so that it would search for "Error bad" as a literal sting. However, the command finds every line that contain "Error" or "bad" opposed to "Error bad"

This is what I tried
Code: [Select]findstr /i /n /C:"\<Error Bad\>" %%A >> %TEMP%\errorlog.txt

Not sure what I am doing worngTry:
Code: [Select]findstr /i /n /c:"Error Bad"
I'm not sure if you are wanting the slashes and carets, but try just the words and you should grab the right stuff. You may want to try a setlocal enableextensions at the beginning of the code if you are still running into trouble and see if that helps anything.My text file contains lines that have "Error bad" and lines that contain "Error good"

I tried using the command

Code: [Select]findstr /i /n /c:"Error Bad"

For some reason it give me the "Error bad" and "Error good"
It is perform in OR on the words for some reason.

I also put
Code: [Select]setlocal enableextensions
at the topFor some odd reason I have to change the name of the file I was writing to. Not sure why.

Opposed to errorlog.txt i have to use

errorlogX.txt each time I run. where X=1,2,3....

A little bit of a pain but I can LIVE with it.
Anyway around having to modify the code name for each run? So long as you don't move the batch file around much, the following will ALLOW for a simple file to keep your instance count and accomplish the numbering you are looking to do.

Code: [Select]set /p seq=<seq.log
set /a seq+=1
echo %seq%>seq.log

Now modify all of of your errorlog.txt files to errorlog%seq%.txt.

Also, before running the first time, open a cmd prompt, change to the directory your batch file is in and type the following echo 0>seq.log

On your second issue, try running your findstr command from the command prompt and see what you get. I did so and I got only Error Bad lines from a test file I set up. So the command should be working, and without seeing it I wouldn't know of any reason why it would be giving you the error. Did you remember to save the .bat file after made the changes and before you ran it? Seems simple, but it has happened to me more often than I care to admit.



Discussion

No Comment Found