?
Solved

Find & Replace XML using DOS Command Script

Posted on 2007-03-29
9
Medium Priority
?
1,069 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 1500 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month9 days, 22 hours left to enroll

762 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