0

I need a batch script that will read in another batch script (batch2) and:

  • look for the string "configout:" OR "configin:"
  • If it encounters one of these two strings, extract what's after it until the string ".xml"
  • copy paste it in a new text file.
  • and do this for each line of batch2.

For exemple: If this is my first line in the batch script

/configin:%faxml%fm_sellin_in.xml /configout:%faxml%transco_fm_sellin_out%col_transco%.xml /inputfile:

I should have this in my text file:

%faxml%fa_sellin_in.xml
%faxml%transco_fm_sellin_out%col_transco%.xml

I have seen a good code in Here:

for /f "tokens=1-2 delims=~" %%b in ("yourfile.txt") do (
  echo %%b >> newfile.txt
  echo removed %%a)

but i don't know how to adapt it to my specific case.

1 Answer 1

1

Why not replace all the /configin and /configout with newlines? -

(Replace string with a new line in Batch)

For example

setlocal EnableDelayedExpansion

set "str=/configin:%%faxml%%fm_sellin_in.xml /configout:%%faxml%%transco_fm_sellin_out%%col_transco%%.xml /inputfile:"

set str=!str:/configin^:=^

!
set str=!str:/configout^:=^

!

Now, !str! would contain

fm_sellin_in.xml
transco_fm_sellin_out.xml /inputfile:

Then, you could use the for loop to extract the strings

for /f "tokens=1,2 delims=. " %%a in ("!str!") do ()

this for loop iterates through each line and splits each line with the and . characters.

So %%a is your file name and %%b is the extension.

then

if [%%b]==[xml] (echo %%a.%%b>>mytextfile.txt)

We will do this for all the lines of batch2.

And the finished code is

setlocal EnableDelayedExpansion

for /f "delims=" %%c in (batch2.txt) do (
set str=%%c
set str=!str:/configin^:=^

!
set str=!str:/configout^:=^

!
for /f "tokens=1,2 delims=. " %%a in ("!str!") do (if [%%b]==[xml] (echo %%a.%%b>>mytextfile.txt))
)
Sign up to request clarification or add additional context in comments.

2 Comments

It works very well thank you, that's what i was looking for. Can you explain to me what is the meaning of ^ and why did you use [xml] instead of (xml) ?
@R.Omar the %% and ^ are escape characters to tell the interpreter to treat it as a character instead of part of the !var:find=replace! or %var% syntax. I used [xml] instead of (xml) because [%%b] is a command line parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.