About

I had the need to rename a bunch of files in windows, each with similar names. The filenames of our wedding pictures misspelt our last names! So, I wrote 2 scripts to rename all 800 some file names. The second is more flexible and better coded.

Script Method

This method was modified from a online bulletin board. It is a piece of code in and of itself, not a function like the second method below. It uses very little object oriented code methods, just procedural coding techniques.

' This script will remove load_ from the filename of all files that include this string in the filename and replace it with another string
' I tested it to work successfully with 4 cases:
'  - uppercase filename (LOAD_test.txt)
'  - lowercase filename (load_test.txt)
'  - different file extension (load_test.exe)
'  - fake out filename (loading_test.txt)
' Nick Yeates http://www.nickyeates.com 9/28/08
 
dir = "c:\temp\" 
strCheck = "load_" ' Note: the replace using this below is case sensitive
 
set fsObject=Wscript.CreateObject ("Scripting.FileSystemObject")
Set oFolder = fsObject.GetFolder(dir)
set oFiles = oFolder.Files
For each item in oFiles
    strFileName=item.Name
    ' InStr function syntax: InStr([start,]string1,string2[,compare])
    LoadFile = InStr(strFileName, strCheck) 'this tells me if LOAD is in the string
    If LoadFile <> 0 Then
        ' Replace function syntax: Replace(expression, find, replacewith[, start[, count[, compare]]])
        strFileName = Replace(strFileName, strCheck, "")
        item.name = strFileName 'Set the file name to new name
    End If
Next
 
 
' Example for renaming files
' Three important lines from above are altered here to show an example of how to use the above code
'dir = "L:\Wedding Pictures\Photographer"
'strCheck = "YEATIS_GOVEL" 'leave this in uppercase
'
'        strFileName = Replace(strFileName, strCheck, "Yeates_Gouel_Wedding")
'
' Example for renaming files with numbers in the name you want to search for
' The only difference is the % symbols, which each refer to any digit
'dir = "c:\Temp"
'strCheck = "Yeates_Gouel_%%%%"
'
'        strFileName = Replace(strFileName, strCheck, "Yeates_Gouel_Wedding")

RegEx / Function Method

The below code implements the same functionality, except in sleeker code, and utilizing Regular Expressions. RegEx allows the user to define the MatchPattern string to be more flexible. For example, one could replace files names with words and numbers, and the numbers may change. Say for example ‘File_129’ ‘File_235’ ‘File_912’ will all fall under the RegEx ‘File_\d\d\d’.

' This function will rename files in a directory; Only those filenames that match the defined pattern will be renamed
' One string in the file name (MatchPattern) will be replaced with another string (ReplacementText) from the filename of all files that include the MatchPattern in the filename
' The user is able to define what string to remove (MatchPattern), and can include via regular expressions (RegEx)
' The user is able to define what string to put in its place (ReplacementText)
' All files in the given directory are searched and renamed
' Nick Yeates http://www.nickyeates.com 1/1/09
 
Sub RenameFilesByExpression(ByRef dir, ByVal MatchPattern, ByVal ReplacementText)
    Dim regEx, CurrentMatch, CurrentMatches
 
    Set regEx = New RegExp
    regEx.Pattern = MatchPattern
    regEx.IgnoreCase = False
    regEx.Global = True
    regEx.MultiLine = True
 
    set fsObject = Wscript.CreateObject ("Scripting.FileSystemObject")
    Set oFolder = fsObject.GetFolder(dir)
    set oFiles = oFolder.Files
    For each item in oFiles
        strFileName = item.Name
        strFileName = regEx.Replace(strFileName, ReplacementText)
        item.name = strFileName 'Set the file name to new name
    Next
 
    Set regEx = Nothing
 
End Sub
 
' Example call of the RenameFilesByExpression function
' To run the above code, you must call the function from another piece of 'driver' code; Below is an example
' The \d each represent a digit; So in this case, 4 digits
RenameFilesByExpression "L:\Wedding Pictures\Photographer", "Yeates_Gouel_Wedding_\d\d\d\d", "Yeates_Gouel_Wedding"
 
technology/windows/renamefiles.txt · Last modified: 01.19.2009 04:12 by 71.166.39.38
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki