Filename Cleaner Script.

Posted: January 13, 2012 in Windows Scripts
Tags: , , , , ,

Here’s another simple script that I used frequently. In it’s currently state it removes Fullstops, Underscores, hyphens from files names.

So a file call File.name-test_1.avi would become File name test.avi

The script is often helpful when managing media or importing files into a systems that requires a specific format.

'**------------------------------------------------------------------------**
'**        Mass Filename cleaner Script - used to rename files    **
'**----------                 By The Gamblor                   ---------**
'**----------                  Version 1.0                        ---------**
'**------------------------------------------------------------------------**

Dim oFSO
Dim sSourceFile
Dim sDestinationFile
 Dim FileName(), FileCounter, debugmode

debugmode=0

'On Error Resume Next
  Dim fso, folder, files, NewsFile

 Set fso = CreateObject("Scripting.FileSystemObject")
Set results = FSO.CreateTextFile("results.csv", TRUE)
  sFolder = "."
   Set folder = fso.GetFolder(sFolder)
  Set files = folder.Files

  FileCounter=1

  For each folderIdx In files
    Redim Preserve FileName(FileCounter)
    FileName(filecounter) = folderIdx.Name
    FileCounter=FileCounter+1
  Next

  For X = 1 to ubound(filename)

      rename(filename(x))

  next

  results.Close

Set oFSO= Nothing

msgbox("done " & ubound(FileName))

Function Rename(sourceName)

ext = right(sourceName,4)
NewName = Left(SourceName,len(sourcename)-4)
' Change the bit below if you need to remove or replace anything in the file name!
NewName = replace(NewName,"_"," ")
NewName = replace(NewName,"-"," ")
newName = replace(NewName,"."," ") & ext

 '  if debugmode =1 then msgbox("old name " & SourceName & " New Name " & newname)

  if sourceName = "FileCleaner.vbs" or sourceName = "results.csv" then
    ' do nothing
   else
    if debugmode =1 then msgbox("old name " & SourceName & " New Name " & newname)
    results.WriteLine(SourceName)

    FSO.MoveFile sourceName, newname
   end if

Set oFso = Nothing
End Function

Copy the above text into a file call FileCleaner.vbs, put it in the same directory as the files that need renaming and run it (double click on it). If you get an error halfway though, just run the file again.

Leave a comment