Page 1 of 1

Help Reading 2 tokens / line from each outputting to 1 line

Posted: 07 Jun 2010 11:56
by JamesA
Hi I am trying to read a file in where each line has 3 numbers seperated by a space/tab. and I want to use the first two numbers as input and put some text between them (XML tags) and output to a separate file to a new single line (file.txt):

e.g. this file contains:

1 2 3
12345 45456 0
63432 72356 0
73432 92356 0

XML tag:
(Echo style with ^ escape characters:) ^<commercial start="%%G" end="%%H" /^>
<commercial start=%%1 end=%%2 /^>

The Batch File I tried to write is shown below. I tried a bunch of things but don't know what I did wrong (maybe an environment variable can't contain multiple lines, or a DO (command) can't process multiple lines.

--------------
Mybatch file.bat
--------------
echo OFF
rem count number of lines and add another for loop?
rem set beginning=^<^?xml version="1.0" encoding="utf-8"^?^>
set "LINE= empty"
rem FOR /F "tokens=1,3" %%G IN (weather.txt) DO (set "LINE=%LINE%%MIDDLE%" && set "MIDDLE=%%H %%G" && echo "%MIDDLE% %LINE%")
rem echo Middle = %MIDDLE% Line = %line%
rem set LINE= set MIDDLE= @echo.%middle%
del file.txt
@echo %LINE%>file.txt
ECHO *************************
FOR /F "tokens=1,3" %%G IN (weather.txt) DO (
SET "LINE=%LINE% %%G %%H"
ECHO Test LINE=====%LINE%
)
@ECHO %LINE%>>file.txt

Any help you could give would be appreciated.

Re: Help Reading 2 tokens / line from each outputting to 1 l

Posted: 07 Jun 2010 12:52
by aGerman
I'm not sure what you realy need, but I think you have to write your file line by line. Try this

Code: Select all

@echo off &setlocal

for /f "tokens=1,2" %%G in (weather.txt) do (
  >>file.txt echo ^<commercial start=%%G end=%%H /^>
)



Regards
aGerman

Re: Help Reading 2 tokens / line from each outputting to 1 l

Posted: 08 Jun 2010 22:31
by JamesA
Your post gave me all the help I needed. Thanks a ton. That was a really fast response too!
Here was what my batch file did when it was done, took 2 arguments per line from a file via the for loop, and output an xml file in a different directory, for someone else's program to remove TV Commercials from TV shows I recorded on my computer.

rem * This batch file will take as an argument a movie file (movie.mpg, movie.dvr-ms, etc) and requires a decision list to be located in the same directory
rem * and will output a Commercial XML file in the location C:\Users\Public\DvrmsToolbox\CommercialsXml
rem usebackq = long filename support
@echo OFF &setlocal EnableDelayedExpansion
set "INTRO=^<?xml version="1.0" encoding="utf-8"?^>^<root^>"
@echo %INTRO%>temp.xml
for /f "tokens=1,2 usebackq" %%G in ("%~dpn1.edl") do (
>>temp.xml echo ^<commercial start="%%G" end="%%H" /^>
)
@echo ^</root^>>>temp.xml
move /Y temp.xml "C:\Users\Public\DvrmsToolbox\CommercialsXml\%~n1.xml"