Page 1 of 1

how do you code this?

Posted: 03 Jul 2011 03:10
by renzlo
Hi Everyone,

I have a text file with these contents:

Code: Select all

Name;       Description;    Remarks
Tom;          Wild;            ;
John;          ;              Absent;
;            Honest;          Present;


What I am doing is that I am extracting the contents into xml using for loop delimited by semi-colon (;),

my output:

Code: Select all

<Name>Tom</Name>
<Description>Wild</Description>
<Remarks></Remarks>
<Name>John</Name>
<Description></Description>
<Remarks>Absent</Remarks>
<Name></Name>
<Description>Honest</Description>
<Remarks>Present</Remarks>


My problem is that i want those blank tags to be an empty tag. For example:

from <Remarks></Remarks> to <Remarks />

i tried using if defined and compare-op but i can't get it.

to summarize, if the content is blank, the empty tag should be written in xml.

Thanks in advance.

Re: how do you code this?

Posted: 03 Jul 2011 05:39
by aGerman
Are you sure there are white spaces between the values in your text file? I've never seen such weird source before. How could you distinguish between a value consisting of spaces and an empty value?

Regards
aGerman

Re: how do you code this?

Posted: 03 Jul 2011 06:12
by renzlo
yes aGerman I am sure, i am also using your replace script, the one you've given me in vbs, i am using semi-colon as delimeter so i can distiguish the value.

Here's the code i am using:

Code: Select all

for /f "skip=1 tokens=1-3 delims=;" %%a in (source.txt) do (
echo ^<Name^>%%a^</Name^> >>file.xml
echo ^<Description^>%%b^</Description^> >>file.xml
echo ^<Remarks^>%%c^</Remarks^> >>file.xml
)


Here's the output before executing the replace script.

Code: Select all

<Name>Tom         </Name>
<Description>Wild      </Description>
<Remarks>               </Remarks>
<Name>John     </Name>
<Description>         </Description>
<Remarks>     Absent</Remarks>
<Name> </Name>
<Description>    Honest</Description>
<Remarks>    Present</Remarks>


and here's after:

Code: Select all

<Name>Tom</Name>
<Description>Wild</Description>
<Remarks></Remarks>
<Name>John</Name>
<Description></Description>
<Remarks>Absent</Remarks>
<Name></Name>
<Description>Honest</Description>
<Remarks>Present</Remarks>


I tried this code but the output always comes from the "if not defined condition"

Code: Select all

for /f "skip=1 tokens=1-3 delims=;" %%a in (source.txt) do (
if defined %%a echo ^<Name^>%%a^</Name^> >>file.xml
if not defined %%a echo ^<Name /^> >>file.xml
if defined %%b echo ^<Description^>%%b^</Description^> >>file.xml
if not defined %%b echo ^<Description /^> >>file.xml
if defined %%c echo ^<Remarks^>%%c^</Remarks^> >>file.xml
if not defined %%c echo ^<Remarks /^> >>file.xml
)

Re: how do you code this?

Posted: 03 Jul 2011 06:58
by aGerman
Try that code:

Code: Select all

@echo off &setlocal enabledelayedexpansion
set "src=incoming.txt"
set "dest=out.xml"

>"%dest%" type nul
for /f "usebackq skip=1 tokens=* eol=" %%a in ("%src%") do (
  set "line=%%a"
  for /l %%b in (1,1,50) do (set "line=!line:;  =; !")
  set "line= !line!"
  for /f "tokens=1-3 delims=;" %%c in ("!line!") do (
    set "name=%%c"
    if "%%c"==" " (set "name=<Name />") else set "name=<Name>!name:~1!</Name>"
    set "description=%%d"
    if "%%d"==" " (set "description=<Description />") else set "description=<Description>!description:~1!</Description>"
    set "remarks=%%e"
    if "%%e"==" " (set "remarks=<Remarks />") else set "remarks=<Remarks>!remarks:~1!</Remarks>"
    >>"%dest%" echo !name!
    >>"%dest%" echo !description!
    >>"%dest%" echo !remarks!
  )
)

Note that it would eliminate exclamation marks.

Regards
aGerman

Re: how do you code this?

Posted: 03 Jul 2011 07:06
by renzlo
aGerman wrote:Try that code:

Code: Select all

@echo off &setlocal enabledelayedexpansion
set "src=incoming.txt"
set "dest=out.xml"

>"%dest%" type nul
for /f "usebackq skip=1 tokens=* eol=" %%a in ("%src%") do (
  set "line=%%a"
  for /l %%b in (1,1,50) do (set "line=!line:;  =; !")
  set "line= !line!"
  for /f "tokens=1-3 delims=;" %%c in ("!line!") do (
    set "name=%%c"
    if "%%c"==" " (set "name=<Name />") else set "name=<Name>!name:~1!</Name>"
    set "description=%%d"
    if "%%d"==" " (set "description=<Description />") else set "description=<Description>!description:~1!</Description>"
    set "remarks=%%e"
    if "%%e"==" " (set "remarks=<Remarks />") else set "remarks=<Remarks>!remarks:~1!</Remarks>"
    >>"%dest%" echo !name!
    >>"%dest%" echo !description!
    >>"%dest%" echo !remarks!
  )
)

Note that it would eliminate exclamation marks.

Regards
aGerman


as always, perfect. thanks aGerman.