Solved

Search Within A File for Specific Text

Posted on 2011-03-11
11
471 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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