how do you code this?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
renzlo
Posts: 116
Joined: 03 May 2011 19:06

how do you code this?

#1 Post by renzlo » 03 Jul 2011 03:10

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.
Last edited by renzlo on 03 Jul 2011 06:18, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do you code this?

#2 Post by aGerman » 03 Jul 2011 05:39

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

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: how do you code this?

#3 Post by renzlo » 03 Jul 2011 06:12

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
)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do you code this?

#4 Post by aGerman » 03 Jul 2011 06:58

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

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: how do you code this?

#5 Post by renzlo » 03 Jul 2011 07:06

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.

Post Reply