Search code examples
batch-filebatch-rename

Discard a particular set of .xml files while parsing through numerous .xml files in a particular folder in a batch script


I want to write a batch script wherein it will parse through .xml files in a particular location and output the total count of a particular tag in the xml files. But in doing so, I have quite a few .xml files in the folder but I want to parse through only those .xml files that does not have the string "DEL" in the filenames.

For example, Suppose I have the below list of xml files in a folder :

abc.xml     
pwrdt.xml     
terwyw.xml     
drDELyt.xml     
yrte.xml     
uyteDEL.xml     
DELytety.xml     
ahdDELwe.xml     

I want to write a batch script which parses through only those .xml files from the above list which does not contain the string DEL in the file name.

So I want to parse through only,

abc.xml     
pwrdt.xml     
terwyw.xml     
yrte.xml


@echo off    
findstr /ip /c:"/ORDNUM" C:\Users\mypath\Desktop\folder\*.xml >> log-it.txt 

In the above statement, I want to search /ORDNUM in only those .xml files that does NOT contain "DEL" in their file name

Below is the script I am using now :

@Echo off
(for /f "delims=" %%F in ('Dir /B "C:\Users\soumya.kanti.dey\Desktop\Splunk\*.xml" ^| Findstr /v "DEL" ') do (
    Echo Processing file %%F
    findstr /ip /c:"/ORDNUM" "%%F"
)) > log-it.txt

for /f "delims=: tokens=2" %%C in ('find /C "/ORDNUM" log-it.txt') Do Set /A "Count=%%C"
echo %count% > "C:\Users\soumya.kanti.dey\Desktop\total.txt"

Solution

  • Using a wildcard as input for findstr you can't exclude any files, you'll need a for /f to parse dir output filtered by another find/findstr /v to only process wanted xml files.

    @Echo off
    for /f "delims=" %%F in ('Dir /B "C:\Users\mypath\Desktop\folder\*.xml" ^| Findstr /v "DEL" ') do findstr /ip /c:"/ORDNUM" "%%F" >> log-it.txt
    

    As findstr doesn't output the file name when processing a single file you might use

    @Echo off
    (for /f "delims=" %%F in ('Dir /B "C:\Users\mypath\Desktop\folder\*.xml" ^| Findstr /v "DEL" ') do (
        Echo Processing file %%F
        findstr /ip /c:"/ORDNUM" "%%F"
    )) > log-it.txt
    

    Changed >> to > to recreate the file in each run.

    To save the count of matches in file log-it.txt use (append)

    for /f "delims=: tokens=2" %%C in ('find /C "/ORDNUM" log-it.txt') Do Set /A "Count=%%C"
    echo %count% > "C:\Users\mypath\Desktop\total.txt"