Solved

Find & Replace XML using DOS Command Script

Posted on 2007-03-29
9
1,066 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
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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