Pages

Friday, July 31, 2015

Deleting Revit Backup Files Recursively using VB Script

Revit Back-up files are useful. However, it can be quite annoying when they fill up a folder and one accidentally starts working off of a back up rather than the latest file.

As a quick fix, I cobbled together a vbs script (learning a bit of vbs in the process) that would delete .rvt and .rfa back files (anything with the pattern <filename>.<####>.rvt or rfa.
You can either download the script and put it in a folder (that could also contain sub-folders) with Revit backup files and run it either by double-clicking or right-click>'Run as Administrator'

Alternatively, you can copy/paste the code below to new text file and then rename the extension to a '.vbs'

  
 
Set oShell = CreateObject("WScript.Shell")
Set ofso = CreateObject("Scripting.FileSystemObject")
oShell.CurrentDirectory = ofso.GetParentFolderName(WScript.ScriptFullName)
'WScript.Echo oShell.CurrentDirectory
intConfirm = MsgBox("Do you want to delete all Revit rvt and rfa files in the folder: " & oShell.CurrentDirectory, vbYesNo, "Confirm Deletion")
if intConfirm = vbNo then WScript.Quit
dim dir: Set dir = ofso.GetFolder(oShell.CurrentDirectory)

Set re = New RegExp
re.Pattern = "^\d{4}$"
numFilesDeleted = 0
DeleteRevitBackupsRecursively(dir)
Wscript.Echo "Number of Files Deleted: " & numFilesDeleted

Sub DeleteRevitBackupsRecursively(Folder)
 intAnswer = MsgBox("Delete files within sub folders?", vbYesNo, "Delete Files In SubFolder")
 DeleteFiles(Folder)
 if intAnswer = vbYes then
  For Each Subfolder in Folder.SubFolders
   Set objFolder = ofso.GetFolder(Subfolder.Path)
   'Wscript.Echo objFolder.Name
   DeleteFiles(objFolder)
  Next
 end if
End Sub

Sub DeleteFiles(Folder)
 dim file: for each file in Folder.Files
  dim name: name = file.name
  dim extension: extension = ofso.GetExtensionName(name)
  dim parts: parts = split(name, ".")
  if UBound(parts) = 2 then

   if re.Test(parts(1)) and (extension = "rvt" or extension = "rfa") then
    Dim objFile
    Set objFile = ofso.GetFile(file)
    objFile.Delete
    Set objFile = Nothing
    numFilesDeleted = numFilesDeleted+1
   end if

  ElseIf extension = "bak" then
   Set objFile = ofso.GetFile(file)
   objFile.Delete
   Set objFile = Nothing
   numFilesDeleted = numFilesDeleted+1
  end if
 next
End Sub
* Updated Code to include AutoCAD back up(*.bak) files and extension checks for Revit files

No comments :

Post a Comment