Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find & Replace XML using DOS Command Script

Posted on 2007-03-29
9
Medium Priority
?
1,072 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

609 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