Solved

Find & Replace XML using DOS Command Script

Posted on 2007-03-29
9
1,059 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
  • 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now