Solved

DOS Batch - Find text throught multiple files of same name

Posted on 2008-10-22
9
541 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
This article summaries thoughts and ideas from two years of sustained use. It provides good reasoning to make the jump to Windows 10.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

730 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