Page 1 of 1

loop works (almost) but I need "nested" output

Posted: 29 May 2008 14:48
by davep
I hope I phrased that right. This is my fist post; please be gentle.

I'm using a for loop to read one line at a time in my source text file, then using that data to create an xml file. I'm having trouble with blank spaces, and/or indenting, and the equals sign.

Here's an example of a few lines from the source text file:

Code: Select all

00:05:20.346
00:07:22:510
00:08:32.284


And I get those values with for /F %%T in (%file1%) do...

And here's What I'm trying to get for each line in the text file:

Code: Select all

 <Markers>
  <Marker
    Time="%%T"
    Value="%%T"
    GenerateKeyframe="True"
    GenerateThumbnail="False" />


Here's a bit of the batch:

Code: Select all

[snip]

:: start the "nest"

echo ^<Markers^> >> %file3%

:: do the rest

for /F %%T in (%file1%) do (
        for %%A in (
  ^<Marker
    Time="%%T"
    Value="%%T"
    GenerateKeyFrame="True"
    GenerateThumbnail="False" ^/^>
                    ) do echo %%A >> %file3%
)

echo ^<^/Markers^> >> %file3%

[snip]


This generates a line for Time, a line for "%%T", a line for Value, a line for "%%T", etc. Also, I'm losing the "=" and I can't seem to get the "nested" look where <Marker is indented two spaces, and Time, Value, etc are indented four spaces.

Here's a bit of what the target should look like:

Code: Select all

<Markers>  
  <Marker 
   Time="00:03:07.687" 
   Value="00:03:07.687" 
   GenerateKeyFrame="True" 
   GenerateThumbnail="False" />
  <Marker 
   Time="00:07:28.573" 
   Value="00:07:28.573" 
   GenerateKeyFrame="True" 
   GenerateThumbnail="False" />
[snip]
</Markers>


Thanks!

Re: loop works (almost) but I need "nested" output

Posted: 29 May 2008 20:24
by chcfrank
Davep,

Here is my prototype is the command prompt, you should be able to convert it into a script. To print initial spaces, use echo. instead just echo

Code: Select all

c:\>echo ^<Markers^> & for /f %i in (x.txt) do @(echo.  ^Marker & echo.    Time="%i" & echo.    Value="%i" & echo.   GenerateKeyframe="True" & echo.    GenerateThumbnail="False" /^> ) & echo ^</Markers^>


-Frank

davep wrote:I hope I phrased that right. This is my fist post; please be gentle.

I'm using a for loop to read one line at a time in my source text file, then using that data to create an xml file. I'm having trouble with blank spaces, and/or indenting, and the equals sign.

Here's an example of a few lines from the source text file:

Code: Select all

00:05:20.346
00:07:22:510
00:08:32.284


And I get those values with for /F %%T in (%file1%) do...

And here's What I'm trying to get for each line in the text file:

Code: Select all

 <Markers>
  <Marker
    Time="%%T"
    Value="%%T"
    GenerateKeyframe="True"
    GenerateThumbnail="False" />


Here's a bit of the batch:

Code: Select all

[snip]

:: start the "nest"

echo ^<Markers^> >> %file3%

:: do the rest

for /F %%T in (%file1%) do (
        for %%A in (
  ^<Marker
    Time="%%T"
    Value="%%T"
    GenerateKeyFrame="True"
    GenerateThumbnail="False" ^/^>
                    ) do echo %%A >> %file3%
)

echo ^<^/Markers^> >> %file3%

[snip]


This generates a line for Time, a line for "%%T", a line for Value, a line for "%%T", etc. Also, I'm losing the "=" and I can't seem to get the "nested" look where <Marker is indented two spaces, and Time, Value, etc are indented four spaces.

Here's a bit of what the target should look like:

Code: Select all

<Markers>  
  <Marker 
   Time="00:03:07.687" 
   Value="00:03:07.687" 
   GenerateKeyFrame="True" 
   GenerateThumbnail="False" />
  <Marker 
   Time="00:07:28.573" 
   Value="00:07:28.573" 
   GenerateKeyFrame="True" 
   GenerateThumbnail="False" />
[snip]
</Markers>


Thanks!

Posted: 29 May 2008 20:42
by davep
I'll give it a shot when I get back to the office. Thanks! If I may ask a follow up question, how could I use %%T's line number from the source txt instead of just repeating %%T again? Like so:

Code: Select all

Time="hh:mm:ss:msec"
Value="chapter 1"
...
Time="hh:mm:ss:msec"
Value="chapter 2"
...
Time="hh:mm:ss:msec"
Value="chapter 3"
...