Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Search Within A File for Specific Text

Posted on 2011-03-11
11
Medium Priority
?
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 57

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 57

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 57

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 57

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

661 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