Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
SIMMS7400
- Posts: 546
- Joined: 07 Jan 2016 07:47
#1
Post
by SIMMS7400 » 21 Jul 2016 12:46
Hello -
I have a need to find a certain string in an XML file and then replace with the the value set with a variable.
For instance, my batch script has to variables:
SET LCM_USER=admin
SET LCM_PSSWRD=welcome1
Here is my xml
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<Package>
<LOCALE>en_US</LOCALE>
<User name="" password=""/>
<Task>
<Source type="Application" product="HP" project="Default Application Group" application="GENJBAPP"/>
<Target type="FileSystem" filePath="/HP-GENJBAPP"/>
<Artifact recursive="true" parentPath="/Configuration" pattern="*"/>
<Artifact recursive="true" parentPath="/Essbase Data" pattern="*"/>
<Artifact recursive="true" parentPath="/Global Artifacts" pattern="*"/>
<Artifact recursive="true" parentPath="/Plan Type" pattern="*"/>
<Artifact recursive="true" parentPath="/Relational Data" pattern="*"/>
<Artifact recursive="true" parentPath="/Security" pattern="*"/>
</Task>
</Package>
So, upon transformation, I need those values set in the variables above propogated to my XML.
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<Package>
<LOCALE>en_US</LOCALE>
<User name="admin" password="welcome1"/>
<Task>
<Source type="Application" product="HP" project="Default Application Group" application="GENJBAPP"/>
<Target type="FileSystem" filePath="/HP-GENJBAPP"/>
<Artifact recursive="true" parentPath="/Configuration" pattern="*"/>
<Artifact recursive="true" parentPath="/Essbase Data" pattern="*"/>
<Artifact recursive="true" parentPath="/Global Artifacts" pattern="*"/>
<Artifact recursive="true" parentPath="/Plan Type" pattern="*"/>
<Artifact recursive="true" parentPath="/Relational Data" pattern="*"/>
<Artifact recursive="true" parentPath="/Security" pattern="*"/>
</Task>
</Package>
I'm having trouble with the leading spaces and can't seem to get anything to work correctly. Any ideas?
Thanks, folks!
Last edited by
Squashman on 21 Jul 2016 12:53, edited 1 time in total.
Reason: MOD EDIT: Use Code tags for formatting.
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#2
Post
by aGerman » 21 Jul 2016 13:21
I recomment to use the XML DOM rather than any kind of regex replacements. A JScript hybrid script may help out.
Code: Select all
if (@a)==(@b) @end /* Batch portion:
@echo off &setlocal
set "xmlfile=test.xml"
set "LCM_USER=admin"
set "LCM_PSSWRD=welcome1"
cscript //nologo //e:jscript "%~fs0" "%xmlfile%" "%LCM_USER%" "%LCM_PSSWRD%"
pause
exit /b
JScript portion: */
var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('User');
oNode.setAttribute('name', WScript.Arguments(1));
oNode.setAttribute('password', WScript.Arguments(2));
oXmlDoc.save(WScript.Arguments(0));
Regards
aGerman
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#3
Post
by penpen » 21 Jul 2016 13:34
This may help (section 3.):
http://www.dostips.com/forum/viewtopic.php?p=34194#p34194You probably only need to replace the xslt part in it, with something like this (untested: sorry actually i've not much time):
Code: Select all
<xslt id="default">
<!-- <?xml version="1.0" encoding="utf-8"?> -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="iso-8859-1"/>
<xsl:preserve-space elements="*"/>
<xsl:template match="priority|clntId">
<xsl:value-of select="."/>
<xsl:value-of select="'
'" disable-output-escaping="no"/>
</xsl:template>
</xsl:stylesheet>
</xslt>
penpen
-
SIMMS7400
- Posts: 546
- Joined: 07 Jan 2016 07:47
#4
Post
by SIMMS7400 » 21 Jul 2016 13:43
Thank you, both!
Germin - I'm getting an error saying "Conditional compilation turned off".
Is there a special way to set up that ode for it to work in my batch file?
Thanks!
-
Squashman
- Expert
- Posts: 4487
- Joined: 23 Dec 2011 13:59
#5
Post
by Squashman » 21 Jul 2016 13:46
SIMMS7400 wrote:Is there a special way to set up that ode for it to work in my batch file?
You put all your batch code inside the JSCRIPT comment.
-
SIMMS7400
- Posts: 546
- Joined: 07 Jan 2016 07:47
#6
Post
by SIMMS7400 » 21 Jul 2016 13:58
This is what I did but it gives me that error:
Code: Select all
echo ********************************************************>>%logfile%
echo PREPARE LCM EXPORT.XML FILE AT %TIME% >>%logfile%
echo ********************************************************>>%logfile%
echo PREPARE LCM EXPORT.XML FILE AT %TIME%
if (@a)==(@b) @end /* Batch portion:
@echo off &setlocal
set "xmlfile=Export.xml"
set "LCM_USER=admin"
set "LCM_PSSWRD=welcome1"
CALL C:\Windows\System32\cscript.exe //nologo //e:jscript "%~fs0" "%xmlfile%" "%LCM_USER%" "%LCM_PSSWRD%"
pause
exit /b
JScript portion: */
var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('//User');
oNode.setAttribute('name', WScript.Arguments(1));
oNode.setAttribute('password', WScript.Arguments(2));
oXmlDoc.save(WScript.Arguments(0))
Thanks, I"m probably doing it wrong!
-
Squashman
- Expert
- Posts: 4487
- Joined: 23 Dec 2011 13:59
#7
Post
by Squashman » 21 Jul 2016 14:15
This works for me.
Code: Select all
@if (@a)==(@b) @end /* Batch portion:
@echo off &setlocal
echo ********************************************************
echo PREPARE LCM EXPORT.XML FILE AT %TIME%
echo ********************************************************
echo PREPARE LCM EXPORT.XML FILE AT %TIME%
set "xmlfile=Export.xml"
set "LCM_USER=admin"
set "LCM_PSSWRD=welcome1"
cscript.exe //nologo //e:jscript "%~fs0" "%xmlfile%" "%LCM_USER%" "%LCM_PSSWRD%"
pause
exit /b
JScript portion: */
var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('//User');
oNode.setAttribute('name', WScript.Arguments(1));
oNode.setAttribute('password', WScript.Arguments(2));
oXmlDoc.save(WScript.Arguments(0));
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#8
Post
by aGerman » 21 Jul 2016 14:19
Code: Select all
@if (@a)==(@b) @end /* Batch portion:
... is the only valid line in both Batch and JScript. It exisits in order to introduce a JScript comment and must be the first line in your code.
Code: Select all
@if (@a)==(@b) @end /* Batch portion
@echo off &setlocal
set "logfile=mylog.txt"
echo ********************************************************>>%logfile%
echo PREPARE LCM EXPORT.XML FILE AT %TIME% >>%logfile%
echo ********************************************************>>%logfile%
echo PREPARE LCM EXPORT.XML FILE AT %TIME%
set "xmlfile=Export.xml"
set "LCM_USER=admin"
set "LCM_PSSWRD=welcome1"
cscript //nologo //e:jscript "%~fs0" "%xmlfile%" "%LCM_USER%" "%LCM_PSSWRD%"
pause
exit /b
JScript portion */
var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('User');
oNode.setAttribute('name', WScript.Arguments(1));
oNode.setAttribute('password', WScript.Arguments(2));
oXmlDoc.save(WScript.Arguments(0));
Regards
aGerman
-
SIMMS7400
- Posts: 546
- Joined: 07 Jan 2016 07:47
#9
Post
by SIMMS7400 » 21 Jul 2016 14:40
Ah, so this cant reside in the middle portion of my script? ah i see. Let me test a blank .cmd.
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#10
Post
by aGerman » 21 Jul 2016 15:06
Very first line:
Code: Select all
@if (@a)==(@b) @end /* Batch portion:
Now include your whole Batch code that also contains the cscript call.
Finally append:
Code: Select all
exit /b
JScript portion */
var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('User');
oNode.setAttribute('name', WScript.Arguments(1));
oNode.setAttribute('password', WScript.Arguments(2));
oXmlDoc.save(WScript.Arguments(0));
Regards
aGerman
-
Squashman
- Expert
- Posts: 4487
- Joined: 23 Dec 2011 13:59
#11
Post
by Squashman » 21 Jul 2016 15:21
SIMMS7400 wrote:Ah, so this cant reside in the middle portion of my script?
That is what I meant by my first comment.
-
SIMMS7400
- Posts: 546
- Joined: 07 Jan 2016 07:47
#12
Post
by SIMMS7400 » 21 Jul 2016 17:17
aGerman -
Thank you so much! I've been able to use a blank .cmd and get this work perfectly!!! However, when I try to incorporate this into my batch file I'm in the middle of building, it still fails. Can you help me place your portion in the correct areas?
Here is what I have right now:
Code: Select all
@IF (@a)==(@b) @end /* Batch portion
@ECHO off &setlocal
REM ------------------------------------------------------------------------
REM -- SCRIPT NAME: CREATE_APP_LCM_MIGRATE.cmd
REM ------------------------------------------------------------------------
REM --Call Environment Script--
call C:\Hyperion_Batch\Scripts\batch\_env.cmd
REM --Set working directory as script path--
cd /d %~dp0
REM ------------------------------------------------------------------------
REM SET LOG & ERROR PATHS
REM ------------------------------------------------------------------------
SET intrapath=%MAINPATH%%LOGPATH%%GENJBAPPLOGS%
SET errorintrapath=%MAINPATH%%ERRORPATH%%GENJBAPPERRORS%
FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set timestamp=%%a%%b)
FOR /f "tokens=* delims= " %%c in ("%timestamp%") do (set timestamp=%%c)
REM ------------------------------------------------------------------------
REM SET & DELETE LOG FILES
REM ------------------------------------------------------------------------
SET logfile=%intrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0.log
SET errorfile=%errorintrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0.log
if exist %logfile% del %logfile%
if exist %errorfile% del %errorfile%
REM -- Invoke Batch Client
echo ********************************************************>>%logfile%
echo %~n0 at %TIME% >>%logfile%
echo ********************************************************>>%logfile%
echo ********************************************************>>%logfile%
echo PREPARE LCM EXPORT.XML FILE AT %TIME% >>%logfile%
echo ********************************************************>>%logfile%
echo PREPARE LCM EXPORT.XML FILE AT %TIME%
set "xmlfile=Export.xml"
set "LCM_USER=admin"
set "LCM_PSSWRD=welcome1"
cscript //nologo //e:jscript "%~fs0" "%xmlfile%" "%LCM_USER%" "%LCM_PSSWRD%"
PAUSE
SET myError1=%errorlevel%
IF %myError1%==0 goto EXECUTE_LCM_EXPORT
echo ********************************************************>>%logfile%
echo Error Encountered in PREPARE LCM EXPORT.XML FILE >>%logfile%
echo ********************************************************>>%logfile%
goto AbnormalExit
:EXECUTE_LCM_EXPORT
echo ********************************************************>>%logfile%
echo EXECUTE LCM EXPORT FOR %APP% >>%logfile%
echo ********************************************************>>%logfile%
CALL %LCM_UTIL_PATH%Utility.bat" %LCM_IE_PATH%%LCM_APP_DIR%Export.xml
SET myError1=%errorlevel%
IF %myError1%==0 goto NormalExit
echo ********************************************************>>%logfile%
echo Error Encountered in EXECUTE LCM EXPORT FOR %APP% >>%logfile%
echo ********************************************************>>%logfile%
goto AbnormalExit
:NormalExit
echo ********************************************************>>%logfile%
echo %~n0 Success >>%logfile%
echo ********************************************************>>%logfile%
echo ********************************************************>>%logfile%
echo Normal Exit - %~nx0 >>%logfile%
echo ********************************************************>>%logfile%
date /t >>%logfile%
time /t >>%logfile%
EXIT /B 0
JScript portion: */
var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('//User');
oNode.setAttribute('name', WScript.Arguments(1));
oNode.setAttribute('password', WScript.Arguments(2));
oXmlDoc.save(WScript.Arguments(0))
:AbnormalExit
echo ********************************************************>>%errorfile%
echo %~n0 results in Abnormal Exit >>%errorfile%
echo ********************************************************>>%errorfile%
echo Please Check the log file for errors>>%errorfile%
echo ********************************************************>>%errorfile%
echo Abnormal Exit - %~nx0 >>%errorfile%
echo ********************************************************>>%errorfile%
date /t >>%errorfile%
time /t >>%errorfile%
EXIT 1
But unfortunately, still fails. Thanks guys.
-
SIMMS7400
- Posts: 546
- Joined: 07 Jan 2016 07:47
#13
Post
by SIMMS7400 » 21 Jul 2016 18:25
Nevermind, I got it folks!
Thank you again!! This is great!
-
SIMMS7400
- Posts: 546
- Joined: 07 Jan 2016 07:47
#14
Post
by SIMMS7400 » 22 Jul 2016 12:18
Hi Guys -
I just have another question. I have a need to modify two more strings, both with the same value.
Here is the file:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<Package>
<LOCALE>en_US</LOCALE>
<User name="" password=""/>
<Task>
<Source type="Application" product="HP" project="Default Application Group" application="GENJBAPP"/>
<Target type="FileSystem" filePath="/HP-GENJBAPP"/>
<Artifact recursive="true" parentPath="/Configuration" pattern="*"/>
<Artifact recursive="true" parentPath="/Essbase Data" pattern="*"/>
<Artifact recursive="true" parentPath="/Global Artifacts" pattern="*"/>
<Artifact recursive="true" parentPath="/Plan Type" pattern="*"/>
<Artifact recursive="true" parentPath="/Relational Data" pattern="*"/>
<Artifact recursive="true" parentPath="/Security" pattern="*"/>
</Task>
</Package>
I need to change GENJBAPP (they are located in lines 6 & 7) to a value I specify from a variable, like we did above. Can you help me modify the above solution?
Thank you!
-
Squashman
- Expert
- Posts: 4487
- Joined: 23 Dec 2011 13:59
#15
Post
by Squashman » 22 Jul 2016 12:21
SIMMS7400 wrote:I need to change GENJBAPP (they are located in lines 6 & 7) to a value I specify from a variable, like we did above. Can you help me modify the above solution?
Thank you!
I would think given the example you have already been given you could at least attempt to replicate that. Come back and show us the code you attempted.