Solved

Search Within A File for Specific Text

Posted on 2011-03-11
11
468 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 51

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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 51

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 51

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 68

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 51

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now