Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating an XML file from a batch script

7,040 views
Skip to first unread message

Anton Shepelev

unread,
Feb 10, 2014, 6:23:12 AM2/10/14
to
Hello all,

I am trying to create a bat-script for an MSBuild
Post-build event that would process the built assem-
bly with Dotfuscator. Unfortunately, some of
Dotfuscator's parameters cannot be supplied on the
command line and must be passed via a config file.
Not wanting to hard-code the path to that file, I
want dynamically to create it in the current direc-
tory, use it with Dotfuscator, and delete it after-
wards.

How can I output strings into a file without the
surrounding quotes. The following works incosis-
tently:

echo "<open>Hello<close>" > config.xml

outputting the quotes on Windows Server 2003 R2 and
omitting them on Windows XP Pro. Or does it depend
on some OS setting?

What is the best way to create an XML file from a
batch script?

--
() ascii ribbon campaign - against html e-mail
/\ www.asciiribbon.org - against proprietary attachments

foxidrive

unread,
Feb 10, 2014, 7:19:28 AM2/10/14
to
On 10/02/2014 22:23, Anton Shepelev wrote:

>
> echo "<open>Hello<close>" > config.xml

This format is good. Redirection at the start to avoid trailing whitespace, and escaping the
characters with ^ that need it. % is escaped like this %% and ^& and ^^ and ^|

>>config.xml echo(^<open^>Hello^<close^>

The open bracket protects the echo in certain circumstances.


> outputting the quotes on Windows Server 2003 R2 and
> omitting them on Windows XP Pro. Or does it depend
> on some OS setting?

Should have been the same IMO.

> What is the best way to create an XML file from a
> batch script?

This is another option but you need escape closing brackets as well in this format.

(

echo(^<open^>Hello1^<close^>
echo(^<open^>Hello2^<close^>
echo(^<open^>Hello3^<close^>
echo(^<open^>Hello4^<close^>

)>config.xml


Frank P. Westlake

unread,
Feb 10, 2014, 9:59:11 AM2/10/14
to
On 02/10/2014 03:23 AM, Anton Shepelev wrote:
> I am trying to create a bat-script for an MSBuild
> Post-build event that would process the built assem-
> bly with Dotfuscator. Unfortunately, some of
> Dotfuscator's parameters cannot be supplied on the
> command line and must be passed via a config file.
> Not wanting to hard-code the path to that file, I
> want dynamically to create it in the current direc-
> tory, use it with Dotfuscator, and delete it after-
> wards.

Instead of messing with properly escaped strings it would be much easier
to create and maintain if you use something which behaves sort of like a
HERE doc. A CMD script may contain any kind of data -- even binary data
-- as long as the interpreter does not try to read and interpret parts
of the script which it won't understand. To ensure that the interpreter
does not go where you don't want it to be, precede that portion with a
'GOTO :EOF'.

To create an XML file from the script you could include it in the script
and precede each line with some string which will get filtered out. Then
in the script you use FINDSTR to search your script for lines beginning
with that string, process each line to remove the string, then write it
to the XML file.

:main
CALL :writeXML C:\dir\file.xml
GOTO :EOF


:writeXML <filename>
SET "filename=%~f1"
:: Zero the file in case it exists:
TYPE NUL:>"%filename%"
SET "docChar=`"
For /F "delims=" %%c in (
'TYPE %~f0^|FindStr /B /I /C:"%docChar%"'
) Do (
Set "line=%%c"
(Echo;!line:*%docChar%=!)>>"%filename%"
)
GOTO :EOF
:: BEGIN XML FILE
`<tagA>
` <tagB>
` </tagB>
`</tagA>
:: END XML FILE

I don't have Windows available so the above might need some work -- it
was pulled out of an old script and modified without being tested.
"%docChar%" might have to be hard-coded.

Frank

frank.w...@gmail.com

unread,
Feb 10, 2014, 3:44:27 PM2/10/14
to
From "Frank P. Westlake" :
> :writeXML <filename>
> SET "filename=%~f1"
> :: Zero the file in case it exists:
> TYPE NUL:>"%filename%"
> SET "docChar=`"
> For /F "delims=" %%c in (
> 'TYPE %~f0^|FindStr /B /I /C:"%docChar%"'

That '%~f0' is in a subroutine and I can't recall if it
is '%/0' with or without the ornaments that will always
result in the name of the script instead of the name of
the subroutine. If it fails set "ME=%~f0" up in :main
and use "%ME% in the subroutine.

Frank

Frank P. Westlake

unread,
Feb 10, 2014, 6:56:08 PM2/10/14
to
On 02/10/2014 12:44 PM, frank.w...@gmail.com wrote:
> That '%~f0' is in a subroutine and I can't recall if it is '%/0' with or
> without the ornaments that will always result in the name of the script
> instead of the name of the subroutine. If it fails set "ME=%~f0" up in
> :main and use "%ME% in the subroutine.

I'll rewrite that; I wrote (tapped) it on a five-inch tablet. The
variable '%~f0' is normally the full path and name of the script, but in
a subroutine it might be the name of that subroutine; I don't recall
which and I don't have access to Windows to check. The problem might
actually be with '%0' instead of with '%~f0'. To avoid the problem just
set the full path and name of the script into a variable prior to
calling the subroutine.

:main
SetLocal EnableExtensions EnableDelayedExpansion
SET "ME=%~f0"
CALL :writeXML C:\dir\file.xml
GOTO :EOF


:writeXML <filename>
SET "filename=%~f1"
:: Zero the file in case it exists:
TYPE NUL:>"%filename%"
SET "docChar=`"
For /F "delims=" %%c in (
'TYPE "%ME%"^|FindStr /B /I /C:"%docChar%"'
) Do (
Set "line=%%c"
(Echo;!line:*%docChar%=!)>>"%filename%"
)
GOTO :EOF

:: BEGIN XML FILE
`<tagA>
` <tagB>
` </tagB>
`</tagA>
:: END XML FILE

:: More script may follow here.

Frank

Anton Shepelev

unread,
Feb 12, 2014, 4:00:11 AM2/12/14
to
Frank P. Westlake:

> The variable '%~f0' is normally the full path and
> name of the script, but in a subroutine it might
> be the name of that subroutine; I don't recall
> which and I don't have access to Windows to check.
> The problem might actually be with '%0' instead of
> with '%~f0'. To avoid the problem just set the
> full path and name of the script into a variable
> prior to calling the subroutine.
>
> :main
> SetLocal EnableExtensions EnableDelayedExpansion
> SET "ME=%~f0"
> CALL :writeXML C:irle.xml
> GOTO :EOF
>
>
> :writeXML <filename>
> SET "filename=%~f1"
> :: Zero the file in case it exists:
> TYPE NUL:>"%filename%"
> SET "docChar=`"
> For /F "delims=" %%c in (
> 'TYPE "%ME%"^|FindStr /B /I /C:"%docChar%"'
> ) Do (
> Set "line=%%c"
> (Echo;!line:*%docChar%=!)>>"%filename%"
> )
> GOTO :EOF
>
> :: BEGIN XML FILE
> `<tagA>
> ` <tagB>
> ` </tagB>
> `</tagA>
> :: END XML FILE
>
> :: More script may follow here.

Thank you very much, Frank. Your script works as
expected.

Frank P. Westlake

unread,
Feb 12, 2014, 8:45:11 AM2/12/14
to
On 02/12/2014 01:00 AM, Anton Shepelev wrote:
> Your script works as expected.

I don't know whose expectations -- I didn't expect it to work.

Frank

Anton Shepelev

unread,
Feb 12, 2014, 9:46:24 AM2/12/14
to
Frank P. Westlake to Anton Shepelev:

> > Your script works as expected.
>
> I don't know whose expectations -- I didn't expect
> it to work.

Mine of course, but I never would have written such
a script from the first attempt without testing.

kumarsu...@gmail.com

unread,
Dec 8, 2016, 1:32:51 AM12/8/16
to

golem

unread,
Dec 10, 2016, 5:52:12 AM12/10/16
to
kumarsu...@gmail.com wrote:

>> What is the best way to create an XML file from a
>> batch script?

The following link is to Run Basic, a form of Basic which provides Html
output. Any use?

http://www.libertybasic.com/RunBASICBreakthrough.pdf

Todd Vargo

unread,
Dec 10, 2016, 11:57:00 AM12/10/16
to
On 12/8/2016 1:32 AM, kumarsu...@gmail.com wrote:
> On Monday, February 10, 2014 at 4:53:12 PM UTC+5:30, Anton Shepelev wrote:
>> Hello all,
>>
>> I am trying to create a bat-script for an MSBuild
>> Post-build event that would process the built assem-
>> bly with Dotfuscator. Unfortunately, some of
>> Dotfuscator's parameters cannot be supplied on the
>> command line and must be passed via a config file.
>> Not wanting to hard-code the path to that file, I
>> want dynamically to create it in the current direc-
>> tory, use it with Dotfuscator, and delete it after-
>> wards.
>>
>> How can I output strings into a file without the
>> surrounding quotes. The following works incosis-
>> tently:
>>
>> echo "<open>Hello<close>" > config.xml

echo ^<open^>Hello^<close^> > config.xml

>>
>> outputting the quotes on Windows Server 2003 R2 and
>> omitting them on Windows XP Pro. Or does it depend
>> on some OS setting?
>>
>> What is the best way to create an XML file from a
>> batch script?

The best way is to not use batch for creating XML files.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

pro...@berkeley.edu

unread,
Dec 10, 2016, 2:29:40 PM12/10/16
to
Yeah, but here's another option....

==========begin "C:\Cmd\demo\mytest22.cmd" ==========
1. @echo off
2. echo/3C 6F 70 65 6E 3E 48 65 6C 6C 6F 3C 63 6C 6F 73 65 3E 0D 0A >%temp%\hexxml
3. certutil -f -decodehex %temp%\hexxml %temp%\txtxml 1>nul:
4. type %temp%\txtxml >> config.xml
5. type config.xml
==========end "C:\Cmd\demo\mytest22.cmd" ==========

Line 2 may or may not be wrapped. It looks wrapped as I am composing
my post; it may not come out that way.

And the output in config.xml:

<open>Hello<close>

--
Phil Robyn

Todd Vargo

unread,
Dec 13, 2016, 12:13:47 AM12/13/16
to
> Yeah, but here's another option....
>
> ==========begin "C:\Cmd\demo\mytest22.cmd" ==========
> 1. @echo off
> 2. echo/3C 6F 70 65 6E 3E 48 65 6C 6C 6F 3C 63 6C 6F 73 65 3E 0D 0A >%temp%\hexxml
> 3. certutil -f -decodehex %temp%\hexxml %temp%\txtxml 1>nul:
> 4. type %temp%\txtxml >> config.xml
> 5. type config.xml
> ==========end "C:\Cmd\demo\mytest22.cmd" ==========
>
> Line 2 may or may not be wrapped. It looks wrapped as I am composing
> my post; it may not come out that way.
>
> And the output in config.xml:
>
> <open>Hello<close>

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Temp>certutil
'certutil' is not recognized as an internal or external command,
operable program or batch file.

pro...@berkeley.edu

unread,
Dec 13, 2016, 12:49:42 AM12/13/16
to
Hi, Todd,

Sorry, my bad ... I haven't used WinXP for quite some time. I should have
specified Win7. There supposedly is a version of certutil that will work
on XP but I never bothered to track it down and install it.

Speaking of certutil: does anybody know the magic switch to make certutil
do 'encodehex' with plain hex string output instead of two-column output
with hex chars in the left column and orig content in the right column???
It's possible to reformat the 'encodehex' output of certutil into a
hex string, but for large-scale to-hex conversions I'm still using a tiny
REXX exec to retain the line-by-line format of the original file....

--
Phil Robyn

0 new messages