Batch find and replace in XML

September 4, 2013 at 10:39:40
Specs: Windows 7
I'm trying to write a batch file that will look into a directory for XML files (example: 00000001.xml, 00000002.xml, 00000003.xml...) and need to replace all the Asset ID tags with new text. The asset id's are different in each xml but all start with NEWB (example: Asset_ID="NEWB0080000059305340", Asset_ID="NEWB0080000059305341", Asset_ID="NEWB0080000059305342"). So, I need to find the string NEWB, delete the next 8 characters and replace with current date (YYYYMMDD). Then, replace the Licensing_Window_Start" Value="2013-08-26T00:00:00" with "YYYY-MM-DDT00:00:00. Finally, save as same filename (i'm already backing up these files in a BACKUP directory - so saving over file is OK.)

See More: Batch find and replace in XML

Report •


#1
September 4, 2013 at 18:09:15
Are you restricted to "just batch", or can you use other resources? I highly recommend using some other platform to do the heavy lifting. I can do vbscript, but I'm always rusty on RegExp because I don't do it enough. Powershell is probably another alternative. Sample data is always appreciated in the forum (just a snippet of the xml, doesn't need to be the whole file) especially for batch.

Report •

#2
September 4, 2013 at 18:28:27
With VBScript or Powershell, you should probably load it as an XML document, and modify its DOM. Probably with a carefully crafted XPath.

How To Ask Questions The Smart Way


Report •

#3
September 5, 2013 at 08:20:57
Yes, I have to use batch scripting in this case. Trust me, I would prefer to use Python or Powershell, but can't with this one.

Report •

Related Solutions

#4
September 5, 2013 at 11:37:57
Honest question: Why? Windows has three native scripting languages. VBScript, Batch, and PowerShell. Arguably, VBScript is the "most native" to Windows.

How To Ask Questions The Smart Way


Report •

#5
September 5, 2013 at 19:13:32
@Razor: probably due to "management concerns and restrictions" (read: ignorance and stupiditiy). It seems that's usually the case when poster is "restricted to batch".
@Op: In that case, please post some sample material. Not much known of what to expect from the xml files as to format and "special characters" embedded (f/e: does the "Asset_id" always start on newline? Where does "licensensing_window_start" come in: following asset line or just "further down the pipe" somewhere.) Batch restriction sucks at best, so please give all the help you can.

Report •

#6
September 6, 2013 at 05:16:22
Yeah, but it's our job as the highly paid technical experts to offer better alternatives, and explain why they're better.

How To Ask Questions The Smart Way


Report •


Ask Question