Rename xml file based on the content inside using window bat

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rams1986
Posts: 1
Joined: 23 Jan 2015 01:30

Rename xml file based on the content inside using window bat

#1 Post by rams1986 » 23 Jan 2015 03:44

Hi Folks,
I have an requirement using windows batch scripting.Im very new to this so kindly help me out.

I have multiple xml files , ex APJ01.xml,APJ02.xml...APJ0N.xml. Now based on the content inside the file i need to rename these xmlfiles with corresponding value init. exmaple

<SalesOrder><SOM_SalesOrderID>976064101</SOM_SalesOrderID>

current xml file name : APJ01.xml

after renaming :: APJ_976064101_timestamp.xml.

Please help me doing this in windows batch. i have to do only in windows batch.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Rename xml file based on the content inside using window

#2 Post by Squashman » 23 Jan 2015 07:10

This question has been discussed several times over the years on this forum. Will try and dig up the threads for you.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Rename xml file based on the content inside using window

#3 Post by ShadowThief » 23 Jan 2015 07:42

CSV.bat will surely help with this.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Rename xml file based on the content inside using window

#4 Post by Squashman » 23 Jan 2015 08:02

ShadowThief wrote:CSV.bat will surely help with this.

IMHO I don't think it will.

Penpen wrote a XML parsing routine. Just need to find the thread.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename xml file based on the content inside using window

#5 Post by foxidrive » 23 Jan 2015 19:25

Test this in a folder with some files.

It will write tempren.bat with the rename commands inside it for you to check.
The helper batch file repl.bat is a plain batch file too.

Code: Select all

@echo off
(
   for %%a in (*.xml) do (
      type "%%a"|repl ".*<SalesOrder><SOM_SalesOrderID>(.*?)</SOM_SalesOrderID>.*" "ren \q%%a\q \q<%%a<_$1_timestamp%%~xa\q" xia|repl "(.*)<(...).*<(.*)" "$1$2$3" a
   )
)>tempren.bat



This uses a helper batch file called `repl.bat` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Rename xml file based on the content inside using window

#6 Post by penpen » 25 Jan 2015 18:21

Squashman wrote:Penpen wrote a XML parsing routine. Just need to find the thread.
I've found it: http://www.dostips.com/forum/viewtopic.php?p=32941#p32941
The first code block contains an example on how to extract data from a xml file using xslt.
The second code block contains the file "xslt.bat".

penpen

Post Reply