Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
renzlo
- Posts: 116
- Joined: 03 May 2011 19:06
#1
Post
by renzlo » 08 Jun 2011 17:29
Hi guys,
i have 2 files:
sequence.xml with these contents:
img.txt with these contents:
here's my script:
Code: Select all
echo Number Image
for /f "tokens=1 delims=" %%a in (sequence.xml) do (for /f "tokens=1 delims=" %%b in (img.txt) do (
echo %%a %%b
)
)
why is the output double?
Code: Select all
Number Image
random1 random1.jpg
random2 random2.jpg
random1 random1.jpg
random2 random2.jpg
this should be the output:
Code: Select all
Number Image
random1 random1.jpg
random2 random2.jpg
by the way, if you noticed, i am echoing them in column.
what will be the fix? Am i doing wrong?
Thanks in advance.
-
dbenham
- Expert
- Posts: 2461
- Joined: 12 Feb 2011 21:02
- Location: United States (east coast)
#2
Post
by dbenham » 08 Jun 2011 17:53
You are getting what you asked for!
change the files like so and it should become more obvious:
sequence.xml
img.txt
Your code performs the 2nd loop for each value in the 1st loop (2x2=4 rows). You only want to keep the row in img.txt that matches the current row in sequence.xml, but you are not providing instruction to do that.
Going back to your original output, the following should give you what you want (untested). This will get slow with large files because img.txt will be read in its entirety once for every row in sequence.xml.
Code: Select all
echo Number Image
for /f "tokens=1 delims=" %%a in (sequence.xml) do (
for /f "tokens=1 delims=" %%b in ('type img.txt | findstr %%a') do (
echo %%a %%b
)
)
Dave Benham
Last edited by
dbenham on 08 Jun 2011 23:17, edited 1 time in total.
-
renzlo
- Posts: 116
- Joined: 03 May 2011 19:06
#3
Post
by renzlo » 08 Jun 2011 18:10
Thanks Dave but what if im not doing with associated file? I just want to echo them in column?
-
dbenham
- Expert
- Posts: 2461
- Joined: 12 Feb 2011 21:02
- Location: United States (east coast)
#4
Post
by dbenham » 08 Jun 2011 19:16
renzlo wrote:Thanks Dave but what if im not doing with associated file? I just want to echo them in column?
I'm not sure I understand what you mean. A clear example showing what you want might help (and maybe what you don't want so we see the difference). I saw the reference to columns in your 1st post but it wasn't clear.
There is a
dostips.com :format function that might be useful, depending on your requirements.
Dave Benham
-
renzlo
- Posts: 116
- Joined: 03 May 2011 19:06
#5
Post
by renzlo » 08 Jun 2011 23:05
Thank you very much Dave.
-
renzlo
- Posts: 116
- Joined: 03 May 2011 19:06
#6
Post
by renzlo » 09 Jun 2011 00:54
I just don't get it, it always output double
sequence.xml
Code: Select all
First Line of XML
Second line of XML
3rd line of XML
img.txt
Code: Select all
First Line of IMG.tif
SEcond line of IMG.tif
3rd line of IMG.tif
my code:
Code: Select all
@echo off
echo Number Image
for /f "tokens=1 delims=" %%a in (sequence.xml) do (
for /f "tokens=1 delims=" %%b in ('type img.txt^|findstr .') do (
echo %%a %%b
)
)
pause
the output:
Code: Select all
Number Image
First Line of XML First Line of IMG.tif
First Line of XML SEcond line of IMG.tif
First Line of XML 3rd line of IMG.tif
SEcond line of XML First Line of IMG.tif
SEcond line of XML SEcond line of IMG.tif
SEcond line of XML 3rd line of IMG.tif
3rd line of XML First Line of IMG.tif
3rd line of XML SEcond line of IMG.tif
3rd line of XML 3rd line of IMG.tif
Press any key to continue . . .
this should be the output:
Code: Select all
Number Image
First Line of XML First Line of IMG.tif
SEcond line of XML SEcond line of IMG.tif
3rd line of XML 3rd line of IMG.tif
Press any key to continue . . .
how can i fix it?
thanks in advance
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#7
Post
by aGerman » 10 Jun 2011 08:43
I guess you have to use your RAM for saving the files contents.
Because xml files are normally encoded in UTF8 you have to use the type command to process them.
Code: Select all
@echo off &setlocal
set "file1=sequence.xml"
set "file2=img.txt"
set /a n1=0, n2=0
for /f "tokens=1* delims=:" %%a in ('type %file1%^|findstr /n .') do (
set /a n1=%%a
set "f1.%%a=%%b"
)
for /f "tokens=1* delims=:" %%a in ('type %file2%^|findstr /n .') do (
set /a n2=%%a
set "f2.%%a=%%b"
)
set /a n=n1
if %n2% gtr %n1% set /a n=n2
setlocal enabledelayedexpansion
for /l %%i in (1,1,%n%) do (
echo !f1.%%i! !f2.%%i!
)
endlocal
pause
Regards
aGerman
-
renzlo
- Posts: 116
- Joined: 03 May 2011 19:06
#8
Post
by renzlo » 10 Jun 2011 20:13
as always, perfect aGerman.
Thanks a bunch.