1.

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

Answer» <html><body><p>Hi Guys,<br/><br/>I have a bunch of 'm4a' files that i need to <a href="https://interviewquestions.tuteehub.com/tag/rename-613846" style="font-weight:bold;" target="_blank" title="Click to know more about RENAME">RENAME</a>, literally thousands, they're voice memos from my iPhone.<br/><br/>I have them stored on my hard disk in the directory 'C:\Memos'.<br/><br/>I have an xml file which contains the name of the file and the tag that the file should be called <br/><br/>The xml file contains all the file names, and all the tags that the files should be named.<br/><br/>I've included a sample of the xml file below to give a better idea of how it's structured.<br/><br/>  <br/>  <br/>- <br/>- <br/>  20090816 104438.m4a <br/>  Autosleep <br/>  20090816 124708.m4a <br/>  Head pillow <br/>  <br/>  <br/><br/>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.<br/><br/>Is there a way of doing this automatically with python or vbscript perhaps?<br/><br/>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. <br/><br/>Thanks again,<br/><br/>JP<br/>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.<br/><br/> Code: <a>[Select]</a>Dim arrKey()<br/>Dim arrString()<br/><br/>Set xmlDoc = CreateObject("Microsoft.XMLDOM")<br/>Set fso = CreateObject("Scripting.FileSystemObject")<br/><br/>xmlDoc.async = False<br/>xmlDoc.load("c:\temp\ch.xml")    'change path\filename as required<br/><br/>Set KeyList = xmlDoc.documentElement.selectNodes("dict/key")<br/>i = -<a href="https://interviewquestions.tuteehub.com/tag/1-236780" style="font-weight:bold;" target="_blank" title="Click to know more about 1">1</a><br/>For Each key in KeyList<br/>  i = i + 1<br/>  ReDim Preserve arrKey(i)<br/>  arrKey(i) = key.text<br/>Next<br/><br/>Set StringList = xmlDoc.documentElement.selectNodes("dict/string")<br/>i = -1<br/>For Each str in StringList<br/>  i = i + 1<br/>  ReDim Preserve arrString(i)<br/>  arrString(i) = str.text<br/>Next<br/><br/>For i = <a href="https://interviewquestions.tuteehub.com/tag/0-242464" style="font-weight:bold;" target="_blank" title="Click to know more about 0">0</a> To UBound(arrKey) <br/> Set f = fso.GetFile(arrKey(i))<br/> newName = arrString(i)<br/> f.Name = newName &amp; "." &amp; fso.GetExtensionName(arrKey(i))<br/>Next <br/><br/>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 <strong>vbs</strong> extension and run from the command line as <strong>cscript <em>scriptname.vbs</em></strong> Be sure to change the path\file name as required.<br/><br/>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.....<br/> Code: <a>[Select]</a>import os<br/>import sys<br/>inputfile=sys.argv[1]<br/>keys=[]<br/>strings=[]<br/>for line in open(inputfile):<br/>    s=line.rstrip()<br/>    if "&lt;key&amp;<a href="https://interviewquestions.tuteehub.com/tag/gt-249387" style="font-weight:bold;" target="_blank" title="Click to know more about GT">GT</a>;" in s:<br/>        keys.append(s[s.index("&gt;")+1:].replace("&lt;/key&gt;",""))<br/>    if "&lt;string&gt;" in s:<br/>        strings.append(s[s.index("&gt;")+1:].replace("&lt;/string&gt;",""))<br/>for k,v in zip(keys,strings):<br/>    try:<br/>        print "Renaming %s to %s"%(k, v)<br/>        os.rename(k,v)<br/>    except OSError,e:<br/>        print e<br/>    else:<br/>        print "Renamed %s to %s." %(k,v)<br/><br/><br/>on command line<br/> Code: <a>[Select]</a>c:\test&gt; python myscript.py myxml.xml<br/>Thanks a million for your help.<br/><br/>Having a slight problem here<br/><br/>The vb script is called 'memos.vbs', the xml file is 'C:\memo\l.xml'<br/><br/>When i run the script from command prompt using 'cscript memos.vbs' i get the error:<br/><br/>C:\memo.vbs(27,2) Microsoft VBScript runtime error: File not found'<br/><br/>Any advice on <a href="https://interviewquestions.tuteehub.com/tag/resolving-2248888" style="font-weight:bold;" target="_blank" title="Click to know more about RESOLVING">RESOLVING</a> the above would be much appreciated! Thanks again for your help. <br/><br/>My script is:<br/><br/>Dim arrKey()<br/>Dim arrString()<br/><br/>Set xmlDoc = CreateObject("Microsoft.XMLDOM")<br/>Set fso = CreateObject("Scripting.FileSystemObject")<br/><br/>xmlDoc.async = False<br/>xmlDoc.load("C:\memo\l.xml")    'change path\filename as required<br/><br/>Set KeyList = xmlDoc.documentElement.selectNodes("dict/key")<br/>i = -1<br/>For Each key in KeyList<br/>  i = i + 1<br/>  ReDim Preserve arrKey(i)<br/>  arrKey(i) = key.text<br/>Next<br/><br/>Set StringList = xmlDoc.documentElement.selectNodes("dict/string")<br/>i = -1<br/>For Each str in StringList<br/>  i = i + 1<br/>  ReDim Preserve arrString(i)<br/>  arrString(i) = str.text<br/>Next<br/><br/>For i = 0 To UBound(arrKey) <br/>   Set f = fso.GetFile(arrKey(i))<br/>   newName = arrString(i)<br/>   f.Name = newName &amp; "." &amp; fso.GetExtensionName(arrKey(i))<br/>Next Thanks ghostdog!<br/><br/>The python scrit doesn't give the files the 'm4a' extension, but does change all the file names to the correct tag.<br/><br/>Is there a way to change the extension of multiple files, or perhaps include it in the python script?<br/><br/>Thanks again,<br/><br/>Jp Quote from: jpm999 on September 11, 2010, 07:43:03 PM</p><blockquote>Thanks ghostdog!<br/><br/>The python scrit doesn't give the files the 'm4a' extension, but does change all the file names to the correct tag.<br/><br/>Is there a way to change the extension of multiple files, or perhaps include it in the python script?<br/><br/>Thanks again,<br/><br/>Jp<br/></blockquote> <br/>then add the string ".m4a" <br/> Code: <a>[Select]</a>os.rename(k,v+".m4a")<br/>Its not that difficult. Now, go read up Python docs.</body></html>


Discussion

No Comment Found