Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 :-)

@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

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.

Any one else got any ideas ?
Need to find this in a file*<NUMBER>?????</NUMBER>
</TO>*and then to extract the ???? part

@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

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 promptc:\test> python myscript.py

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.

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

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.

Is that the actual line[s], or is it:
<NUMBER>?????</NUMBER></TO>
=====================================
If at first you don't succeed, you're about average.M2

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

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.

>>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 )

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

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |