Batch file that allows XML choice selection

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cyclist_sg
Posts: 6
Joined: 03 Aug 2016 16:23

Batch file that allows XML choice selection

#1 Post by cyclist_sg » 03 Aug 2016 16:34

Hi,

I have a program called ZWIFT that runs on XML preferences and there are 3 map options which I hope can select each time I run the program.


I wish to be able to select the mapping each time I run:
To ride Watopia, add: <WORLD>1</WORLD>
To ride Richmond, add: <WORLD>2</WORLD>
To ride London, add: <WORLD>3</WORLD>

Here is a sample prefs.xml file :
<ZWIFT>
<WORLD>1</WORLD>
<DEVICES>
<LASTCADENCEDEVICE>720996</LASTCADENCEDEVICE>
<LASTPOWERDEVICE>720996</LASTPOWERDEVICE>
</DEVICES>
<CONFIG>
<RICHMOND_BRANCH_PREFERENCE>0</RICHMOND_BRANCH_PREFERENCE>
<BRANCH_PREFERENCE>4</BRANCH_PREFERENCE>
</CONFIG>
<WORKOUTS>
<USE_ERG>1</USE_ERG>
</WORKOUTS>
</ZWIFT>

Hope to have a batch file so that I can key press map option when running the program file called ZWIFTLAUNCHER.EXE everytime. Thanks ahead.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch file that allows XML choice selection

#2 Post by Squashman » 03 Aug 2016 19:05

Easier to do with vbscript or jscript. We have discussed this a few times over the past few months on the forums. Search the forums and you should find some good examples.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file that allows XML choice selection

#3 Post by foxidrive » 04 Aug 2016 00:38

cyclist_sg wrote:Hope to have a batch file so that I can key press map option when running the program file called ZWIFTLAUNCHER.EXE everytime.


This style of task is a very frequent request, but how do you wish to select the 1,2,3 ?

Do you want to type 1 or 2 or 3 after the batch file name at the CMD prompt, like this?

startzwift.bat 1
startzwift.bat 2
startzwift.bat 3

cyclist_sg
Posts: 6
Joined: 03 Aug 2016 16:23

Re: Batch file that allows XML choice selection

#4 Post by cyclist_sg » 04 Aug 2016 00:56

This program is subjected to ongoing updates so best avoid cloning xml file. Preferably an edit on the moment selection is made to the xml file will be more dynamic and up to date.

Many thanks.... Pat

cyclist_sg
Posts: 6
Joined: 03 Aug 2016 16:23

Re: Batch file that allows XML choice selection

#5 Post by cyclist_sg » 04 Aug 2016 01:06

A single batch file should be more efficient way i think.

Rgds... Pat

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

Re: Batch file that allows XML choice selection

#6 Post by aGerman » 04 Aug 2016 11:20

Similar task:
http://www.dostips.com/forum/viewtopic.php?f=3&t=7296

Try that code snippet:

Code: Select all

@if (@a)==(@b) @end /* Batch portion:

@echo off &setlocal

set "xmlfile=test.xml"
choice /c wrl /n /m "Choose [W]atopia, [R]ichmond, or [L]ondon: "
cscript //nologo //e:jscript "%~fs0" "%xmlfile%" "%errorlevel%"

exit /b


JScript portion: */

var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('WORLD');
oNode.text = WScript.Arguments(1);
oXmlDoc.save(WScript.Arguments(0));

I assumed that your example is correct (with ZWIFT as root node of the XML file and WORLD as child node of ZWIFT).
Customize the (path +) name of your XML file.

Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file that allows XML choice selection

#7 Post by foxidrive » 04 Aug 2016 17:39

cyclist_sg wrote:This program is subjected to ongoing updates so best avoid cloning xml file. Preferably an edit on the moment selection is made to the xml file will be more dynamic and up to date.

Many thanks.... Pat

You misunderstood my question.

I don't know what you mean by cloning, and my post was only referring to one batch file.

Batch files can be launched in different ways:
1) clicked on
2) type the name at the cmd prompt, and with a parameter after the name
3) Scheduled in task scheduler
4) in the Windows startup group
5) ....

cyclist_sg
Posts: 6
Joined: 03 Aug 2016 16:23

Re: Batch file that allows XML choice selection

#8 Post by cyclist_sg » 04 Aug 2016 17:52

foxidrive wrote:
cyclist_sg wrote:This program is subjected to ongoing updates so best avoid cloning xml file. Preferably an edit on the moment selection is made to the xml file will be more dynamic and up to date.

Many thanks.... Pat

You misunderstood my question.

I don't know what you mean by cloning, and my post was only referring to one batch file.

Batch files can be launched in different ways:
1) clicked on
2) type the name at the cmd prompt, and with a parameter after the name
3) Scheduled in task scheduler
4) in the Windows startup group
5) ....


My apologies. What is required ia a batch file that will allow map choosing when executed. The maps are identified by nos. 1, 2 and 3. So when any one of these are keypressed, the script shld have added <WORLD>1/2/3</WORLD>line. The default prefs.xml does not have this line.

Sorry for the troubles.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file that allows XML choice selection

#9 Post by foxidrive » 04 Aug 2016 21:52

cyclist_sg wrote:What is required ia a batch file that will allow map choosing when executed. The maps are identified by nos. 1, 2 and 3. So when any one of these are keypressed, the script shld have added <WORLD>1/2/3</WORLD>line. The default prefs.xml does not have this line.


Do you require this added to the prefs.xml file
or copy prefs.xml to another filename and the WORLD section placed in the copied file?

cyclist_sg
Posts: 6
Joined: 03 Aug 2016 16:23

Re: Batch file that allows XML choice selection

#10 Post by cyclist_sg » 04 Aug 2016 21:56

foxidrive wrote:
cyclist_sg wrote:What is required ia a batch file that will allow map choosing when executed. The maps are identified by nos. 1, 2 and 3. So when any one of these are keypressed, the script shld have added <WORLD>1/2/3</WORLD>line. The default prefs.xml does not have this line.


Do you require this added to the prefs.xml file
or copy prefs.xml to another filename and the WORLD section placed in the copied file?


Adding is a better way because this program constantly updates.

Rgds... Pat

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file that allows XML choice selection

#11 Post by foxidrive » 04 Aug 2016 22:45

cyclist_sg wrote:Adding is a better way because this program constantly updates.


Ok. Add the line to your prefs.xml and then use aGerman's code in the post above.

All that needs to be added is a command to launch your program and add the path to the prefs.xml file.
Add the launching command above the exit /b line as shown.

Code: Select all

set "xmlfile=c:\program files\zwiftfolder\prefs.xml"



Code: Select all

start "" /D "c:\program files\zwiftfolder" "ZWIFTLAUNCHER.EXE"
exit /b

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

Re: Batch file that allows XML choice selection

#12 Post by aGerman » 05 Aug 2016 03:17

The default prefs.xml does not have this line.

:shock:

Code: Select all

@if (@a)==(@b) @end /* Batch portion:

@echo off &setlocal

set "xmlfile=test.xml"
choice /c wrl /n /m "Choose [W]atopia, [R]ichmond, or [L]ondon: "
cscript //nologo //e:jscript "%~fs0" "%xmlfile%" "%errorlevel%"

exit /b


JScript portion: */

var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('WORLD');
if (oNode == null) {
  var oNode2 = oXmlDoc.documentElement.selectSingleNode('DEVICES');
  oNode = oXmlDoc.createElement('WORLD');
  oXmlDoc.documentElement.insertBefore(oNode, oNode2);
  oNode.text = WScript.Arguments(1);
  var oXsltDoc = new ActiveXObject('Microsoft.XMLDOM');
  oXsltDoc.loadXML(
    '<?xml version="1.0" encoding="windows-1252"?>\n' +
    '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">\n' +
    '   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />\n' +
    '   <xsl:template match="node()|@*">\n' +
    '      <xsl:copy>\n' +
    '         <xsl:apply-templates select="node()|@*" />\n' +
    '      </xsl:copy>\n' +
    '   </xsl:template>\n' +
    '</xsl:stylesheet>');
  oXmlDoc.transformNodeToObject(oXsltDoc, oXmlDoc);
  oXmlDoc.save(WScript.Arguments(0));
  WScript.Quit();
}
oNode.text = WScript.Arguments(1);
oXmlDoc.save(WScript.Arguments(0));

Regards
aGerman

cyclist_sg
Posts: 6
Joined: 03 Aug 2016 16:23

Re: Batch file that allows XML choice selection

#13 Post by cyclist_sg » 05 Aug 2016 08:04

aGerman wrote:
The default prefs.xml does not have this line.

:shock:

Code: Select all

@if (@a)==(@b) @end /* Batch portion:

@echo off &setlocal

set "xmlfile=test.xml"
choice /c wrl /n /m "Choose [W]atopia, [R]ichmond, or [L]ondon: "
cscript //nologo //e:jscript "%~fs0" "%xmlfile%" "%errorlevel%"

exit /b


JScript portion: */

var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('WORLD');
if (oNode == null) {
  var oNode2 = oXmlDoc.documentElement.selectSingleNode('DEVICES');
  oNode = oXmlDoc.createElement('WORLD');
  oXmlDoc.documentElement.insertBefore(oNode, oNode2);
  oNode.text = WScript.Arguments(1);
  var oXsltDoc = new ActiveXObject('Microsoft.XMLDOM');
  oXsltDoc.loadXML(
    '<?xml version="1.0" encoding="windows-1252"?>\n' +
    '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">\n' +
    '   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />\n' +
    '   <xsl:template match="node()|@*">\n' +
    '      <xsl:copy>\n' +
    '         <xsl:apply-templates select="node()|@*" />\n' +
    '      </xsl:copy>\n' +
    '   </xsl:template>\n' +
    '</xsl:stylesheet>');
  oXmlDoc.transformNodeToObject(oXsltDoc, oXmlDoc);
  oXmlDoc.save(WScript.Arguments(0));
  WScript.Quit();
}
oNode.text = WScript.Arguments(1);
oXmlDoc.save(WScript.Arguments(0));

Regards
aGerman



Gentlemen, many thanks and I have tested the scripts with the file locations and all sorted. It works so well until I realised... hey no reset button to remove the line. LOL!

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

Re: Batch file that allows XML choice selection

#14 Post by aGerman » 05 Aug 2016 08:27

That's not too complicated. I'll add it when I'm back home.

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

Re: Batch file that allows XML choice selection

#15 Post by aGerman » 05 Aug 2016 11:02

Here you are ...

Code: Select all

@if (@a)==(@b) @end /* Batch portion:

@echo off &setlocal

set "xmlfile=test.xml"
choice /c wrle /n /m "Choose [W]atopia, [R]ichmond, [L]ondon, or r[E]set: "
cscript //nologo //e:jscript "%~fs0" "%xmlfile%" "%errorlevel%"

exit /b


JScript portion: */

var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('WORLD');
if (WScript.Arguments(1) == '4') {
  if (oNode != null) {
    oXmlDoc.documentElement.removeChild(oNode);
    oXmlDoc.save(WScript.Arguments(0));
  }
  WScript.Quit();
}
if (oNode == null) {
  var oNode2 = oXmlDoc.documentElement.selectSingleNode('DEVICES');
  oNode = oXmlDoc.createElement('WORLD');
  oXmlDoc.documentElement.insertBefore(oNode, oNode2);
  oNode.text = WScript.Arguments(1);
  var oXsltDoc = new ActiveXObject('Microsoft.XMLDOM');
  oXsltDoc.loadXML(
    '<?xml version="1.0" encoding="windows-1252"?>\n' +
    '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">\n' +
    '   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />\n' +
    '   <xsl:template match="node()|@*">\n' +
    '      <xsl:copy>\n' +
    '         <xsl:apply-templates select="node()|@*" />\n' +
    '      </xsl:copy>\n' +
    '   </xsl:template>\n' +
    '</xsl:stylesheet>');
  oXmlDoc.transformNodeToObject(oXsltDoc, oXmlDoc);
  oXmlDoc.save(WScript.Arguments(0));
  WScript.Quit();
}
oNode.text = WScript.Arguments(1);
oXmlDoc.save(WScript.Arguments(0));

Regards
aGerman

Post Reply