1.

Solve : Rename multiple files whose names are contained in an XML file.?

Answer»

Hi Guys,

I have a bunch of 'm4a' files that i need to RENAME, literally thousands, they're voice memos from my iPhone.

I have them stored on my hard disk in the directory 'C:\Memos'.

I have an xml file which contains the name of the file and the tag that the file should be called

The xml file contains all the file names, and all the tags that the files should be named.

I've included a sample of the xml file below to give a better idea of how it's structured.

 
 
-
-
  20090816 104438.m4a
  Autosleep
  20090816 124708.m4a
  Head pillow
 
 

So the file '20090816 104438.m4a' needs to be renamed 'Autosleep.m4a', the file '20090816 124708.m4a' needs to be renamed 'Head pillow.m4a' , etc.

Is there a way of doing this automatically with python or vbscript perhaps?

Any help or advice would be much appreciated as I am new to this and have been doing the renaming manually so far, but i've thousands of files to get through.

Thanks again,

JP
I'm sure you'll get a Python solution but I went with VBScript because it comes installed with Windows and there is no download required for those that won't or can't download third party products.

Code: [Select]Dim arrKey()
Dim arrString()

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
Set fso = CreateObject("Scripting.FileSystemObject")

xmlDoc.async = False
xmlDoc.load("c:\temp\ch.xml")    'change path\filename as required

Set KeyList = xmlDoc.documentElement.selectNodes("dict/key")
i = -1
For Each key in KeyList
  i = i + 1
  ReDim Preserve arrKey(i)
  arrKey(i) = key.text
Next

Set StringList = xmlDoc.documentElement.selectNodes("dict/string")
i = -1
For Each str in StringList
  i = i + 1
  ReDim Preserve arrString(i)
  arrString(i) = str.text
Next

For i = 0 To UBound(arrKey)
Set f = fso.GetFile(arrKey(i))
newName = arrString(i)
f.Name = newName & "." & fso.GetExtensionName(arrKey(i))
Next

There is probably a simpler way to do this with the XMLDOM object, but I chose to go with brute force. Save the snippet with a vbs extension and run from the command line as cscript scriptname.vbs Be sure to change the path\file name as required.

Good luck.  Here's a Python solution. Note you should really use an XML parser like lxml, but since your sample XML is simple enough.....
Code: [Select]import os
import sys
inputfile=sys.argv[1]
keys=[]
strings=[]
for line in open(inputfile):
    s=line.rstrip()
    if "<key&GT;" in s:
        keys.append(s[s.index(">")+1:].replace("</key>",""))
    if "<string>" in s:
        strings.append(s[s.index(">")+1:].replace("</string>",""))
for k,v in zip(keys,strings):
    try:
        print "Renaming %s to %s"%(k, v)
        os.rename(k,v)
    except OSError,e:
        print e
    else:
        print "Renamed %s to %s." %(k,v)


on command line
Code: [Select]c:\test> python myscript.py myxml.xml
Thanks a million for your help.

Having a slight problem here

The vb script is called 'memos.vbs', the xml file is 'C:\memo\l.xml'

When i run the script from command prompt using 'cscript memos.vbs' i get the error:

C:\memo.vbs(27,2) Microsoft VBScript runtime error: File not found'

Any advice on RESOLVING the above would be much appreciated! Thanks again for your help.

My script is:

Dim arrKey()
Dim arrString()

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
Set fso = CreateObject("Scripting.FileSystemObject")

xmlDoc.async = False
xmlDoc.load("C:\memo\l.xml")    'change path\filename as required

Set KeyList = xmlDoc.documentElement.selectNodes("dict/key")
i = -1
For Each key in KeyList
  i = i + 1
  ReDim Preserve arrKey(i)
  arrKey(i) = key.text
Next

Set StringList = xmlDoc.documentElement.selectNodes("dict/string")
i = -1
For Each str in StringList
  i = i + 1
  ReDim Preserve arrString(i)
  arrString(i) = str.text
Next

For i = 0 To UBound(arrKey)
   Set f = fso.GetFile(arrKey(i))
   newName = arrString(i)
   f.Name = newName & "." & fso.GetExtensionName(arrKey(i))
Next Thanks ghostdog!

The python scrit doesn't give the files the 'm4a' extension, but does change all the file names to the correct tag.

Is there a way to change the extension of multiple files, or perhaps include it in the python script?

Thanks again,

Jp Quote from: jpm999 on September 11, 2010, 07:43:03 PM

Thanks ghostdog!

The python scrit doesn't give the files the 'm4a' extension, but does change all the file names to the correct tag.

Is there a way to change the extension of multiple files, or perhaps include it in the python script?

Thanks again,

Jp

then add the string ".m4a"
Code: [Select]os.rename(k,v+".m4a")
Its not that difficult. Now, go read up Python docs.


Discussion

No Comment Found