Solved

DOS Batch - Find text throught multiple files of same name

Posted on 2008-10-22
9
504 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

757 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

22 Experts available now in Live!

Get 1:1 Help Now