Page 1 of 1

For loop question

Posted: 08 Jun 2011 17:29
by renzlo
Hi guys,

i have 2 files:

sequence.xml with these contents:

Code: Select all

random1
random2


img.txt with these contents:

Code: Select all

random1.jpg
random2.jpg


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.

Re: For loop question

Posted: 08 Jun 2011 17:53
by dbenham
You are getting what you asked for!

change the files like so and it should become more obvious:

sequence.xml

Code: Select all

xml_A
xml_B

img.txt

Code: Select all

img_1
img_2

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

Re: For loop question

Posted: 08 Jun 2011 18:10
by renzlo
Thanks Dave but what if im not doing with associated file? I just want to echo them in column?

Re: For loop question

Posted: 08 Jun 2011 19:16
by dbenham
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

Re: For loop question

Posted: 08 Jun 2011 23:05
by renzlo
Thank you very much Dave.

Re: For loop question

Posted: 09 Jun 2011 00:54
by renzlo
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

Re: For loop question

Posted: 10 Jun 2011 08:43
by aGerman
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

Re: For loop question

Posted: 10 Jun 2011 20:13
by renzlo
as always, perfect aGerman.

Thanks a bunch.