Link to home
Start Free TrialLog in
Avatar of vacpartswarehouse
vacpartswarehouseFlag for United States of America

asked on

Find and Replace .cmd file

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.
Avatar of Bill Prew
Bill Prew

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
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
Avatar of vacpartswarehouse

ASKER

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?
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.

https://www.experts-exchange.com/questions/26824267/How-do-I-find-and-replace-text-strings-in-a-series-of-html-fies.html

~bp
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
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">
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
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
Ah, thanks for clearing that up!
Thank you for your time and patience!
No problem, glad you got something useful, thanks for the comments.

~bp