FINDSTR passing file name dynamically using FOR LOOP

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sandeep_hi
Posts: 3
Joined: 21 Jun 2015 03:39

FINDSTR passing file name dynamically using FOR LOOP

#1 Post by sandeep_hi » 21 Jun 2015 03:44

I am stuck with below problem. I need to read multiple xml files in a directory & look for certain string. If the particular string is found certain lines from XML file needs to be copied and written to output file.

Below script is working fine for a single file :

Code: Select all

setlocal EnableDelayedExpansion
rem Assemble the list of line numbers
set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"PASS" 1.xml') do (
   set /A before=%%a-10, after=%%a+2
   set "numbers=!numbers!!before!: !after!: "
)
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" 1.xml ^| findstr /B "%numbers%"') do echo %%b) >> Out.txt


Out.txt for above script rightly displays the specific lines from 1.xml file But it doesn't work when I try to use FOR loop to pass all XML files(1.xml, 2.xml, 3.xml etc) read one by one from a text file.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
FOR /F %%x in (input.txt) DO (
set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"PASS" %%x') do (
   set /A before=%%a-10, after=%%a+2
   set "numbers=!numbers!!before!: !after!: "
)
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %%x ^| findstr /B "%numbers%"') do echo %%b) > Out.txt
)


The output on running above script is: FINDSTR: No search strings

A sample Input.txt would look something like this:
1.xml
2.xml
3.xml Here is a sample XML file which could be used as input for both the scripts: https://www.dropbox.com/s/h16fzdurvogrp9l/1.xml?dl=0 Please download & rename sample xml as 1.xml, 2.xml etc

All the XML files and batch script are located in same directory. In the last FOR Loop I have also tried using findstr /B "!numbers!", still the result was same.

Any suggestions whats going wrong here?

Thanks
San

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FINDSTR passing file name dynamically using FOR LOOP

#2 Post by foxidrive » 21 Jun 2015 08:35

Maybe this is a simple solution:

Code: Select all

@echo off
for %%a in (*.xml) do call batchone.bat "%%a"


Code: Select all

:: batchone.bat
@echo off
setlocal EnableDelayedExpansion
rem Assemble the list of line numbers
set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"PASS" "%~1" ') do (
   set /A before=%%a-10, after=%%a+2
   set "numbers=!numbers!!before!: !after!: "
)
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%~1" ^| findstr /B "%numbers%"') do echo %%b) >> Out.txt

sandeep_hi
Posts: 3
Joined: 21 Jun 2015 03:39

Re: FINDSTR passing file name dynamically using FOR LOOP

#3 Post by sandeep_hi » 21 Jun 2015 09:38

Thanks a lot Foxidrive for the solution..below code works well:

Code: Select all

@echo off
for %%a in (*.xml) do batchone.bat "%%a"


Just wondering what was going wrong in the first approach with one batch script?
Last edited by sandeep_hi on 21 Jun 2015 10:24, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FINDSTR passing file name dynamically using FOR LOOP

#4 Post by foxidrive » 21 Jun 2015 10:02

Note that my one liner and the quote in your reply post has been edited - the do and call keywords are required.

You may have long filenames and which is why it failed,
plus you are using "%numbers%" within a loop and it will require "!numbers!" with the delayed expansion.

sandeep_hi
Posts: 3
Joined: 21 Jun 2015 03:39

Re: FINDSTR passing file name dynamically using FOR LOOP

#5 Post by sandeep_hi » 21 Jun 2015 10:28

Thanks again... I will try debugging first approach further based on your inputs :)

Post Reply