Solved

Search Within A File for Specific Text

Posted on 2011-03-11
11
470 Views
Last Modified: 2012-06-27
Hello,

I'm trying to figure out how I can search within several files for a specific string of  text. If a match is found based on the text I'm looking for I need to be able to copy the file it found the text into a separate folder. I used Windows Grep which worked great but there was no way for me to copy the files I found into an alternate directory.

Any suggestions are appreciated. Thanks,
0
Comment
Question by:tgarrity
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 10

Expert Comment

by:aboo_s
ID: 35111830
It sounds like you need to write down your own code for this job you want.

I would recommend simple good old c++ for the job!
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35111887
I would do it with findstr /M to create a temp file containing the names of the files that contained the string I"foo" in this case).

findstr /i /M foo *.* >%TEMP%myfiles.dat

Open in new window

then a for loop to copy the files:
for /F %I in (%TEMP%myfiles.dat) do copy "%I" "c:\targetdir\"

Open in new window

0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 35111906
That seems like it should be easily done.  What type of "match" criteria do you need, is it just some plain text string located anyplace in the file, or something more complicated?

And is it correct that there will be a number of files to be searched, either in a folder or matching a filter of some sort like *.txt, and all of those that contain the searched string you want copied to a new folder?

~bp
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 16

Expert Comment

by:sjklein42
ID: 35111908
You can tweak it.  You may want to add /Y to the copy so it doesn't prompt before overwriting an existing file.

Also, if the string you are searching for is not a single word like "foo", then do this:

findstr /i /M /c:"foo bar" *.* >%TEMP%myfiles.dat

Open in new window


Of course *.* may not be the set of files you are searching.  You didn't give us very many specifics.  :)
0
 
LVL 1

Author Comment

by:tgarrity
ID: 35112008
Sorry for not providing specifics:

I'm looking for the string of text "Maturity Disbursement Exceptions" which is located in E:\Reports\2007
All of the files have a .rep extension but essentially they were .txt files that were renamed to the .rep extension after being imported by our imaging system.
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 167 total points
ID: 35112137
Put these two lines into a .bat file (mycopy.bat).

Set up now for testing, it will just echo the copy commands to the window.

Remove the "@echo" to make it really do the copy.

Change the target folder as needed.

Should work. ;)

findstr /i /M /c:"Maturity Disbursement Exceptions" E:\Reports\2007\*.rep >%TEMP%myfiles.dat
for /F %%I in (%TEMP%myfiles.dat) do @echo copy "%%I" "c:\targetdir\"

Open in new window

0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 35113814
Here's how I would approach it, without getting any additional specifics:

@echo off
set BaseDir=c:\temp
set DestDir=d:\bkup
for /F "tokens=*" %%A in ('findstr /I /M "search-string" "%BaseDir%\*.*"') do (
  copy /Y "%%~A" "%DestDir%"
)

Open in new window

~bp
0
 
LVL 53

Assisted Solution

by:Bill Prew
Bill Prew earned 166 total points
ID: 35113822
Sorry, didn't see your detail reply, so adding that info:

@echo off
set BaseDir=E:\Reports\2007
set DestDir=d:\bkup
for /F "tokens=*" %%A in ('findstr /I /M "Maturity Disbursement Exceptions" "%BaseDir%\*.rep"') do (
  copy /Y "%%~A" "%DestDir%"
)

Open in new window

~bp
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 167 total points
ID: 35115828
Bill, you need to use /C in findstr, since the exact phrase should be used, not the single words:
@echo off
set BaseDir=E:\Reports\2007
set DestDir=d:\bkup
for /F "tokens=*" %%A in ('findstr /I /M /C:"Maturity Disbursement Exceptions" "%BaseDir%\*.rep"') do (
  copy /Y "%%~A" "%DestDir%\"
)

Open in new window

0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 35115855
Good catch Qlemo.

~bp
0
 
LVL 1

Author Closing Comment

by:tgarrity
ID: 35131412
Thanks!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question