Solved

DOS Batch - Find text throught multiple files of same name

Posted on 2008-10-22
9
534 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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
sccm importing drivers 4 43
Windows 10 taskbar - how to display labels with icons 4 79
Cannot take ownership of a folder 8 40
Create .bat File 16 23
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
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…

792 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