Here is how to back up the whole darn project drive with Revit Python Shell in one shot. We were going through Revit 2014 upgrade and in order to take some extra precaution I thought to do an extra temporary backup of all the Revit 2013 documents from all the project directories on the server. You know – so should something goes wrong… But we don’t want the rest of stuff that project folders might contain, just .rvt files. So here is the question – how do you go and backup / copy only Revit files from about 50 project folders and also preserve the same folder – structure from their original location? Especially when these files are not only in the folder where they were meant to be but also all over the place. One option is manually go and look for all these files and recreate the folders in the new place – doesn’t sound like something anyone would want to do, kind of like the visit to the dentist. It is actually really easy to use .NET to parse and copy files around – all we need is a scripting environment and something to execute the script. If you have Revit Python Shell installed then you are in luck – who said you can only do Revit specific stuff with RPS? You can also move files around and do all sorts of other useful things without having to compile your code and not even touch Revit API! Without further ado here is the script to run in Revit Python Shell that will take a specified directory, will parse in all sub-directories for .rvt files and copy them to the new location while preserving the original folder structure.
Warning: You will have to plug in your own paths into fromPath and toPath variables. Make sure to test it on the temporary folders to see how it works before you attempt it on your project drives. And make sure you do double backwards slashes in the paths.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#dp Stuff | http://dp-stuff.org | copyRevitFiles #this Revit Python Script finds and copies all the Revit files from #a specified directory to a new location preserving the folder structure import clr import time clr.AddReference('System.IO') clr.AddReference('RevitAPI') clr.AddReference('RevitAPIUI') from Autodesk.Revit.DB import * from System.IO import * #replace with your specific paths fromPath = 'L:\\10005\\' toPath = 'O:\\Temp\\' extension = '*.rvt' def recSearchSub(myDirPath): #search in subs ct1 = 0 ct2 = 0 tStart = time.clock() print 'Time START :', localtime for p in Directory.GetDirectories(myDirPath): for f in Directory.GetFiles(p, extension, SearchOption.AllDirectories): try: recPath = f.replace(fromPath, toPath) print recPath fPath = Path.GetDirectoryName(recPath) if not(Directory.Exists(fPath)): Directory.CreateDirectory(fPath) File.Copy(f, recPath) ct1+=1 except: ct2+=1 pass tEnd = time.clock() print str(ct1) + ' files copied' print str(ct2) + ' files failed to copy' print 'Time Spent:', tEnd - tStart return recSearchSub(fromPath)