?
Solved

DOS Batch - Find text throught multiple files of same name

Posted on 2008-10-22
9
Medium Priority
?
555 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
[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
  • 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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
 
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 1200 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
A small collection of useful tips and tricks for Windows 10 users that I decided to write as a result of recent questions that were asked and answered at Experts Exchange. Two short video tutorials included. Enjoy..
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

762 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