Batch file that allows XML choice selection
Moderator: DosItHelp
-
- Posts: 6
- Joined: 03 Aug 2016 16:23
Batch file that allows XML choice selection
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.
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.
Re: Batch file that allows XML choice selection
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.
Re: Batch file that allows XML choice selection
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
-
- Posts: 6
- Joined: 03 Aug 2016 16:23
Re: Batch file that allows XML choice selection
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
Many thanks.... Pat
-
- Posts: 6
- Joined: 03 Aug 2016 16:23
Re: Batch file that allows XML choice selection
A single batch file should be more efficient way i think.
Rgds... Pat
Rgds... Pat
Re: Batch file that allows XML choice selection
Similar task:
http://www.dostips.com/forum/viewtopic.php?f=3&t=7296
Try that code snippet:
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
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
Re: Batch file that allows XML choice selection
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) ....
-
- Posts: 6
- Joined: 03 Aug 2016 16:23
Re: Batch file that allows XML choice selection
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.
Re: Batch file that allows XML choice selection
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?
-
- Posts: 6
- Joined: 03 Aug 2016 16:23
Re: Batch file that allows XML choice selection
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
Re: Batch file that allows XML choice selection
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
Re: Batch file that allows XML choice selection
The default prefs.xml does not have this line.

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
-
- Posts: 6
- Joined: 03 Aug 2016 16:23
Re: Batch file that allows XML choice selection
aGerman wrote:The default prefs.xml does not have this line.![]()
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!
Re: Batch file that allows XML choice selection
That's not too complicated. I'll add it when I'm back home.
Re: Batch file that allows XML choice selection
Here you are ...
Regards
aGerman
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