Solved

DOS Batch - Find text throught multiple files of same name

Posted on 2008-10-22
9
516 Views
Last Modified: 2012-05-05
Hi there,

I'd like to use a DOS Batch file to do the following...

Search Z:\ (All subdirectories)
for all files named, "appconfig.txt"

If appconfig.txt contains, <dependencylist>
whereas each appconfig.txt file may contain many instances of "<dependencylist>"

then write to output file,

C:\temp\appconfigoutput.txt

Full directory path
full text from line containing "<Dependencylist>"

Hope I was clear in what I'm looking to do.  If not, please ask me to elaborate.  This is probably a simple thing to do... I just don't recall how to do it.  

Winver is 5.1.2600 / 5.2.3790


and
0
Comment
Question by:Paraleptropy
  • 5
  • 4
9 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 22779864
Paste the script below into a text file with a .cmd extension.  Running it will write results to the file output.txt.


@echo off

setlocal
 

set root=z:

set name=appconfig.txt

set string=^<dependencylist^>

set log=output.txt
 

for /F "tokens=*" %%G in ('dir "%root%\%name%" /A:-D /B /S') do (

 for /F "tokens=*" %%H in ('findstr /I /C:"%string%" %%G') do (

  echo %%G>>"%log%"

  echo %%H>>"%log%"

  echo.>>"%log%"

 )

)

Open in new window

0
 

Author Comment

by:Paraleptropy
ID: 22790914
This doesn't seem to work for me.  Kinda just looks like it's hanging and never creates the "output.txt" file.

0
 
LVL 38

Expert Comment

by:Shift-3
ID: 22792061
How big are the text files?
0
 

Author Comment

by:Paraleptropy
ID: 22792366
Text files are small... 1k or so...  Might be over a thousand of them though.  :)

Anyway to output so I can see what's going?

Thanks again.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 38

Expert Comment

by:Shift-3
ID: 22796390
Oops, I missed a pair of quotation marks.  This version should work better.

If not, REM out the @echo off on the first line.  That will let you see where it's getting hung up.


@echo off

setlocal

 

set root=z:

set name=appconfig.txt

set string=^<dependencylist^>

set log=output.txt

 

for /F "tokens=*" %%G in ('dir "%root%\%name%" /A:-D /B /S') do (

 for /F "tokens=*" %%H in ('findstr /I /C:"%string%" "%%G"') do (

  echo %%G>>"%log%"

  echo %%H>>"%log%"

  echo.>>"%log%"

 )

)

Open in new window

0
 

Author Comment

by:Paraleptropy
ID: 22796577
This is the output I receive:

There doesn't seem to be any change in the actual output being generated.  "Output.txt" is never created.  

C:\temp>for /F "tokens=*" %G in ('dir "z:\appconfig.txt" /A:-D /B /S') do (for /F "tokens=*" %H in ('findstr /I /C:"<dependencylist>" "%G"') do (
echo %G 1>>"output.txt"
 echo %H 1>>"output.txt"
 echo.1>>"output.txt"
) )
0
 

Author Comment

by:Paraleptropy
ID: 22796794
Ok... scratch my last comment...  I let it run on another machine for a while and it finally kicked off and completed.

The only issue is that it only returns the frist value of "<dependencylist>".  Each file might contain 10 lines with that value.  I need the script to return all lines containing that value.

0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 300 total points
ID: 22803807
I'm unable to duplicate your result.  It returns all lines containing <dependencylist> in each file for me.  Is the problem that each one is prefaced by the filename individually?

Here's a version which groups the lines under a single filename heading.


@echo off

setlocal

 

set root=z:

set name=appconfig.txt

set string=^<dependencylist^>

set log=output.txt

 

for /F "tokens=*" %%G in ('dir "%root%\%name%" /A:-D /B /S') do (

 echo %%G>>"%log%"

 for /F "tokens=*" %%H in ('findstr /I /C:"%string%" "%%G"') do (

  echo %%H>>"%log%"

 )

)

Open in new window

0
 

Author Closing Comment

by:Paraleptropy
ID: 31508881
I ran the latest version of this batch file and have to say, Great work.  This did exactly what I was looking for.

Thanks a bunch.
0

Featured Post

Can’t get the mobile email signature right?

Not having any luck when trying to create an email signature for mobile devices? Does the formatting keep messing up? Make sure you have great email signatures on all devices by using Exclaimer Cloud - Signatures for Office 365.

Question has a verified solution.

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

A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

910 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

20 Experts available now in Live!

Get 1:1 Help Now