Move a XML file based on content

May 6, 2009 at 12:31:16
Specs: Windows 2003
Is it possible to move a file based on content. I have a directory that each day is filled up with a few hundred XML files. I would like to move the files to certain directories based on what is in the file.

Part of the XML shows where the file should be sent
The "TO" element holds the Number and this is where the file that needs to be sent. The problem is Number is used else where in the file and it is only the Number in the TO element that should be read.

<TO>
<C1>18186BD4</C1>
<SERVICE>MAIL</SERVICE>
<NUMBER>1234</NUMBER>
</TO>

If NUMBER = 1234 move to c:\list\1234
If NUMBER = 1111 move to c:\list\1111
If NUMBER = 9999 move to c:\list\null\
Else move file to c:\list\notsure\

I dont code my self and was trying to do this with a few command lines tools but failed due to NUMBER appearing in more that one part of the file.

Any help on this would save me lots of work each day :-)


See More: Move a XML file based on content

Report •


#1
May 6, 2009 at 14:16:48
@echo off & setLocal EnableDelayedExpansion

for /f "tokens=* delims=" %%x in ('dir/b *.xml') do (

set TOlineNUM=
set /a N=0
set FOLD=

for /f "tokens=1-3 delims=<>" %%a in (%%x) do (
if not defined FOLD if "%%a" equ "NUMBER" set FOLD=%%b
)
echo move %%x c:\list\!FOLD!
)
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
May 6, 2009 at 18:44:58
Thank you Mechanix2Go the code looks good but the problem is there are two elements in the XML with the same name called

<NUMBER>

Your code picks up on the first element with NUMBER in it I need to get access to the second element called NUMBER
Here is more of what the XML file looks like.

<FROM>
<SERVICE>POSTBOX</SERVICE>
<NUMBER>1297563118</NUMBER>
</FROM>
<TO>
<C1>18186BD4</C1>
<SERVICE>MAIL</SERVICE>
<NUMBER>1234</NUMBER>
</TO>

If your code could work on the second bit then the code is just about there.


Report •

#3
May 19, 2009 at 19:00:12
Any one else got any ideas ?
Need to find this in a file

*<NUMBER>?????</NUMBER>
</TO>*

and then to extract the ???? part


Report •

Related Solutions

#4
May 20, 2009 at 01:50:17
@echo off > newfile & setLocal EnableDelayedExpansion

for /f "tokens=2 delims=<>" %%a in ('find "<NUMBER>" ^< my.xml') do (
set dest=%%a
)
echo move my.xml d:\some\!dest!


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#5
May 20, 2009 at 05:43:07
if you can afford to use other tools, here's an alternative solution using Python
import re,glob,shutil
choices = {
 "1234":os.path.join("c:\\","list","1234"),
 "1111":os.path.join("c:\\","list","1111"),
 "9999":os.path.join("c:\\","list","null")
}
pat=re.compile("<TO>.*<NUMBER>(.*?)</NUMBER>.*</TO>",re.M|re.DOTALL)
for files in glob.glob("*.xml"):    #get all xml files
    data=open(files).read()
    number=pat.findall(data)[0] #find the number
    if choices.has_key(number): 
        destination = choices[pat.findall(data)[0]]
        shutil.move(files, destination)
    else:
        shutil.move(files,os.path.join("c:\\","list","unsure"))

save as myscript.py and on command prompt
c:\test> python myscript.py


Report •

#6
May 20, 2009 at 21:02:16
Thanks Mechanix2Go the batch file worked great :-) From what I understand of the file it loops through the XML file and sets dest to the Number value each time it finds that Element.

This works great but is there any way for it to find the value in this part of the file *<NUMBER>?????</NUMBER>
</TO>*

or is this where a batch file can't go and its time to move to a programming language? like the Python example that ghostdog gave ( will try and install python to test)
As later on the XML file might change another another NUMBER element migth be added near the bottom and it would then take that value.


Report •

#7
May 21, 2009 at 01:57:04
You need to be specific about the file. It's no good making one script that works with the stated objective and then changing the XML.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#8
May 21, 2009 at 23:30:19
Hi Mechanix2Go sorry about not being clear. I was taking your example and using the same basic code idea and using it in another batch file.

This time I was looking to exact anther element but this time this the element appeared 4 times and I was looking to extract the 3rd instance of the element.

Now I am sure it would be easy to tell the code to extract the 4th instance but of this element but some times it might appear in the file only twice. that's why I wanted to some how patten match a certain part of the file that I know would aways hold that element.

eg the number element that i wanted to extract will alway have closing element of </TO>

*<NUMBER>?????</NUMBER>
</TO>*

This way it would not mater where in the file this element was located eg first or last it would always extract the data.


Report •

#9
May 24, 2009 at 01:09:15
Is that the actual line[s], or is it:

<NUMBER>?????</NUMBER></TO>


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#10
May 24, 2009 at 02:45:48
@OP, my last advice to you. If you seriously want to parse XML, batch is not the right tool to use. Learn to use a language with strong string manipulation capabilities and good XML parsing libraries.

Report •

#11
May 24, 2009 at 19:55:08
M2 it will look like this with the second line wraping on to a new line.

<NUMBER>?????</NUMBER>
</TO>

GD I think I asked the question before and said that I also through that maybe a batch file was being stretched to phase XML files. But M2 has produced some great batch files so far they are quick and dirty but they work very well.


Report •

#12
May 24, 2009 at 20:40:13
>>But M2 has produced some great batch files so far they are quick and dirty but they work very well.

i know he has. BUT you must consider your problem domain. some tasks are very simple, some are not, as in this case such as XML parsing, it becomes slow (script development) and messy. ( more code )


Report •

#13
May 25, 2009 at 04:49:56
I
'd like to see all the relevant lines. If it's really large, zip it up and use http://www.sendthisfile.com/


=====================================
If at first you don't succeed, you're about average.

M2


Report •


Ask Question