Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Search Within A File for Specific Text

Posted on 2011-03-11
11
Medium Priority
?
479 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 59

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 668 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 59

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 59

Assisted Solution

by:Bill Prew
Bill Prew earned 664 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 71

Assisted Solution

by:Qlemo
Qlemo earned 668 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 59

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

877 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