Solved

Find & Replace XML using DOS Command Script

Posted on 2007-03-29
9
1,065 Views
Last Modified: 2010-04-16
Hi All,

Hope someone can help me.  I'm struggling with my lack of DOS script knowledge.

Summary:  I'm copying an XML file from one server to another.  Then I need to parse through the XML file and change any references to the old server.

A speedy response would be most appreciated.

Thanks

Mark
0
Comment
Question by:SuperLight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18816983
Try this. Run the batch processing without any parameters to get the usage:

@echo off

setlocal enabledelayedexpansion

if "%~3"=="" echo Usage: %0 oldserver newserver inputfile [outputfile]&goto :EOF

if not exist "%~3" echo %~1 does not exist&goto :EOF

set oldserver=%~1
set newserver=%~2

set input=%~3
set output=%~dpn1.out

if not "%~4"=="" set output=%~4

del "%output%" 2>NUL

for /f "tokens=*" %%a in ('type "%input%"') do call :PROCESS "%%a"

if exist "%output%" echo Output in %output%&goto :EOF

echo No output file generated, %input% is empty
goto :EOF

:PROCESS

set line=%1
set line=!_t:%oldserver%=%newserver%!

set line=%line:^=^^%
set line=%line:&=^&%
set line=%line:|=^|%
set line=%line:<=^<%
set line=%line:>=^>%

(echo %line:~1,-1%)>>"%output%"

goto :EOF

Good Luck,
Steve
0
 

Author Comment

by:SuperLight
ID: 18817064
Thanks, I'll give it a go

Mark
0
 

Author Comment

by:SuperLight
ID: 18817139
Hi Steve

Code runs ok, but the output isn't so good.  When I click on the .xml file it says the xml file cannot be displayed.  When I look at the file in a text editor I get this: PPSD0055=APPSL005.  Seems to lose the 1st character of the old server and the last character of the new server.

Here is the xml I'm changing (should help):

 <?xml version="1.0" encoding="UTF-8" ?>
- <!-- Sample XML file generated by XML Spy v4.4 U (http://www.xmlspy.com)
  -->
- <css>
- <hub location="http://APPSD0055:58080">
  <dirPort>58089</dirPort>
  </hub>
- <spi>
- <provider>
  <native name="Native Directory" />
  </provider>
  </spi>
- <searchOrder>
  <el>Native Directory</el>
  </searchOrder>
- <token>
  <timeout>480</timeout>
  </token>
- <logger>
  <priority>DEBUG</priority>
  </logger>
  </css>

Ta

Mark
0
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 30

Expert Comment

by:SteveGTR
ID: 18818379
The batch processing doesn't appear to care too much for this XML file. Maybe a VBS script code do the trick. Unfortunately, I'm not a VBS script person.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18818521
I stole this from Sirbounty. So if he comes along feel free to give him full credit:

Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
OldFile="C:\OldFile.txt"
NewFile="C:\NewFile.txt"
OldText="PPSD0055"
NewText="NEWSERVER"
Dim objFile: Set objFile = objFSO.OpenTextFile(OldFile)
strData = objFile.ReadAll
objFile.Close
Set objFile = objFSO.CreateTextFile(NewFile)
objFile.Write Replace(strData, OldText, NewText)
objFile.Close
Set objFile = Nothing
set objFSO=Nothing

Also, if it doesn't work, then please blame Sirbounty ;)
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18818530
Try this:
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
OldFile="C:\OldFile.xml"
NewFile="C:\NewFile.xml"
OldText="OldServer"
NewText="NewServer"
Dim objFile: Set objFile = objFSO.OpenTextFile(OldFile)
strData = objFile.ReadAll
objFile.Close
Set objFile = objFSO.CreateTextFile(NewFile)
objFile.Write Replace(strData, OldText, NewText)
objFile.Close
Set objFile = Nothing
set objFSO=Nothing
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18818540
Haha :^)
Either way...only difference was that I changed the .txt to .xml here...
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18818645
Excellent :)
0
 

Author Comment

by:SuperLight
ID: 18841943
Hi There

Sorry for the belated response.  Many Thanks for both of your contributions, but sirbounty got the nod!

This was my final working solution (with some additional help)

Sub Main()
Dim strCmd() As String

strCmd = Split(Command, ",")
If UBound(strCmd) < 3 Then
    Exit Sub
End If


Call Change(strCmd(0), strCmd(1), strCmd(2), strCmd(3))

End Sub
--------------------------
Sub Change(ByVal OldFile As String, ByVal NewFile As String, _
                ByVal OldText As String, ByVal NewText As String)

Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")


Dim objFile: Set objFile = objFSO.OpenTextFile(OldFile)

strDAta = objFile.ReadAll
objFile.Close
Set objFile = objFSO.CreateTextFile(NewFile)
objFile.Write Replace(strDAta, OldText, NewText)
objFile.Close

Set objFile = Nothing
Set objFSO = Nothing

End Sub
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question