Solved

Find and Replace .cmd file

Posted on 2011-09-08
12
443 Views
Last Modified: 2012-05-12
Hello,

I would like to find and replace text within an XML file. I can simply do this with notepad's built in find and replace feature, but I have several finds to do on a daily basis.

I need to find a tag <g:id> and replace it with
<g:id xmlns:g="http://base.google.com/ns/1.0">
and the same for <g:brand>
<g:brand xmlns:g="http://base.google.com/ns/1.0">

I have a folder called myFolder and inside I would like to have the .xml file along with the .cmd so that when I double click the .cmd it searches the .xml file and replaces the tags.

Thanks, shout if you need any more information.
0
Comment
Question by:vacpartswarehouse
  • 6
  • 6
12 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36506423
Doing this in a CMD file isn't going to be the best approach, since it's ability to search and replace is pretty limited.

I'd suggest either doing it with a VBS script, where it could handle this well, or use a free utility like chgstr to do it.

http://www.paulslore.com/index.php?option=com_content&task=view&id=49&Itemid=46

~bp
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36506448
If a VBS approach interests you let me know and I'll post an example, but don't want to do that if it has to be BAT/CMD.

~bp
0
 

Author Comment

by:vacpartswarehouse
ID: 36506481
I have downloaded the CHGSTR and unzipped it to my downloads folder, but when I run the .exe the command prompt window flashes and nothing else happens. What do I need to do to utilize this program?
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 53

Expert Comment

by:Bill Prew
ID: 36506507
It's a command line utility, so you will need to create a BAT or CMD script that runs that utility, supplying the desired command line parms to it.  Here's an example in another question this is fairly similar to what you are trying to do.  Let me know if that doesn't help, I'll provide more info here.

http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_26824267.html

~bp
0
 

Author Comment

by:vacpartswarehouse
ID: 36506625
Maybe a VB macro would be best. In this situation the column headers are the <tags> and look like g:id and g:brand. Thanks
0
 

Author Comment

by:vacpartswarehouse
ID: 36506691
Here are all the XML tags that need changing...

<ns1:description>
<ns1:id>
<ns1:brand>
<ns1:expiration_date>
<ns1:price>
<ns1:upc>
<ns1:weight>      
<ns1:mpn>
<ns1:product_type>
<ns1:condition>

.. to:

<g:description xmlns:g="http://base.google.com/ns/1.0">
<g:id xmlns:g="http://base.google.com/ns/1.0">
<g:brand xmlns:g="http://base.google.com/ns/1.0">
<g:expiration_date xmlns:g="http://base.google.com/ns/1.0">
<g:price xmlns:g="http://base.google.com/ns/1.0">
<g:upc xmlns:g="http://base.google.com/ns/1.0">
<g:weight xmlns:g="http://base.google.com/ns/1.0">
<g:mpn xmlns:g="http://base.google.com/ns/1.0">
<g:product_type xmlns:g="http://base.google.com/ns/1.0">
<g:condition xmlns:g="http://base.google.com/ns/1.0">
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 36507015
Okay, give this a try.  Save as a VBS and edit the correct location of the XML file in the sInfile variable. If you want to overwrite that file you can, or if you want the output to a new file put a different name in the sOutfile variable;

Option Explicit

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Define variable
Dim sInFile, sOutFile, oFSO, oInFile, sData, oOutFile

' Set input file name, and output filename (if same file will be overwritten)
sInfile = "EE27298706.xml"
sOutfile = sInFile

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
sData = oInfile.ReadAll
oInfile.Close
Set oInfile = Nothing

sData = Replace(sData, "<ns1:description>",     "<g:description xmlns:g=""http://base.google.com/ns/1.0"">")
sData = Replace(sData, "<ns1:id>",              "<g:id xmlns:g=""http://base.google.com/ns/1.0"">")
sData = Replace(sData, "<ns1:brand>",           "<g:brand xmlns:g=""http://base.google.com/ns/1.0"">")
sData = Replace(sData, "<ns1:expiration_date>", "<g:expiration_date xmlns:g=""http://base.google.com/ns/1.0"">")
sData = Replace(sData, "<ns1:price>",           "<g:price xmlns:g=""http://base.google.com/ns/1.0"">")
sData = Replace(sData, "<ns1:upc>",             "<g:upc xmlns:g=""http://base.google.com/ns/1.0"">")
sData = Replace(sData, "<ns1:weight>",          "<g:weight xmlns:g=""http://base.google.com/ns/1.0"">")
sData = Replace(sData, "<ns1:mpn>",             "<g:mpn xmlns:g=""http://base.google.com/ns/1.0"">")
sData = Replace(sData, "<ns1:product_type>",    "<g:product_type xmlns:g=""http://base.google.com/ns/1.0"">")
sData = Replace(sData, "<ns1:condition>",       "<g:condition xmlns:g=""http://base.google.com/ns/1.0"">")

' Write file with any changes made
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
oOutfile.Write(sData)
oOutfile.Close
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing

Open in new window

~bp
0
 

Author Comment

by:vacpartswarehouse
ID: 36510497
Looks like that will do the trick.

Will the macro find file named EE27298706 anywhere on my computer? In this situation the file was on my desktop and it successfully made the changes.

I see how the macro does the find and replace so I'll add in the part to take care of the closing tags.

Thanks for all your help!
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36510528
No, if you only specify the filename and extension as I did, the file would have to exist in the current directory when the script is run.  If you want to be more specific then specify the full path to the file in that variable, like:

sInfile = "C:\Dir1\Dir2\EE27298706.xml"

~bp
0
 

Author Comment

by:vacpartswarehouse
ID: 36510570
Ah, thanks for clearing that up!
0
 

Author Closing Comment

by:vacpartswarehouse
ID: 36510582
Thank you for your time and patience!
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36510968
No problem, glad you got something useful, thanks for the comments.

~bp
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 79
Regular Expression Calculator Tester 2 72
how to run this deletion request 4 21
Query group by data in SQL Server - cursor? 3 31
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

770 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