Solved Batch Scripting to read XML file

November 20, 2012 at 07:45:35
Specs: Windows XP

I have a list of XML files in a text file i.e myfiles.txt . myfiles.txt has list of XML files with full path.
I have to read each XML file and search for a specific string and need to know how many times the string appeared in a XML file(Count) and redirect string and count to a different file using batch scripting.

I have 10 to 15 strings (DEFINE, UPPER,LOWER, > ,>=...) to search in each XML file.

Please help me with the script

See More: Batch Scripting to read XML file

Report •

November 20, 2012 at 18:27:59
This seemed to work, but very slow:
::===== begin batchscript bhags
@echo off & setlocal
for /f "tokens=*" %%a in (myfiles.txt) do (
for /f "tokens=*" %%b in (strings.txt) do (
for /f %%c in ('find /c "%%b"^<%%a') do >>results echo file: %%a, string: %%b, count: %%c
::==== end batchscript
and IMPORTANT NOTE: only counts lines with match, so if there's more than one match on a line, count will be WRONG. If that is an issue, it might be corrected with more code, but I'll wait for the other shoe to drop... Leaving output format up to you, this just a demo.

Report •

November 21, 2012 at 06:58:25
Thanks for the reply..!!! Your code worked perfectly for my requirement except the count..
I need count of the string, like if it repeats twice in the same line count should be two instead of one...

Report •

November 21, 2012 at 21:30:40
✔ Best Answer
If you can use a vbscript "helper", this might do it. Let me know. Batch SUCKS at handling xml or any "special" characters in files: ()<>!%, among others. It is good for some things, but not that.
::===== begin revised bathscript:
@echo off>results & setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (strings.txt) do set c="%%a" !c!
for /f "tokens=*" %%a in (myfiles.txt) do cscript bhags.vbs %%a %c% >> results
::====== end batch

'=======begin vbscript "bhags.vbs"
'count ttl no. of multiple strings in given file. file is first argument, followed by multiple strings each to be counted.
'output is: file, quoted-string, final-count
set fso=createobject("scripting.filesystemobject")
set b=fso.opentextfile(wscript.arguments(0))
for i=1 to wscript.arguments.count-1
wscript.echo wscript.arguments(0)+" """+wscript.arguments(i)+""" "+cstr(ubound(c))
'====== end vbscript

Report •

Related Solutions

November 22, 2012 at 08:05:34
Thanks for the code..But its not giving results if the strings.txt contains " != " and I need count If it appears as a whole string in the file like string ***, The code is counting it as 2 if the xml file contains " ****** ". But I dont want that count at all..

Report •

Ask Question