I usually have the need to rename several files located on a particular folder. The files can have a particular string pattern which I would like to remove and/or an extension I want to change. I usually come across these scenarios when I script out the objects from a database to text files. When I extract tables and stored procedures, I usually create many files which could have a particular pattern and extension that needs to be changed. For example, I like to use the extension TAB instead of SQL for table scripts.
I wrote this script to allow me to facilitate the batch renaming of files.
This scripts uses the
Scripting.FileSystemObject COM object. This object is used to access a computer's file system. This allows you to create and alter existing files.
Using the code
This script iterates through all the files found in a particular folder. It searches for string and extension patterns in each file. If the pattern is found, it is replaced by the new string and extension that was provided.
The command line syntax to run the script is as follows:
cscript rename.vbs [folder path] [string to replace]
[new string] [old extension] [new extension]
|[folder path]||folder location|
|[string to replace]||pattern to find|
|[new string]||new string to replace pattern with|
|[old extension]||extension to find (optional)|
|[new extension]||new extension (optional)|
The usage is:
cscript rename.vbs . test ozkar txt tab
If one only needs to replace the extension for all the files, enter the extension pattern to replace as the [string to replace] parameter. The script is written to allow you to replace two patterns at once, only when the first pattern is found.
The code is divided into three main areas. The entry point
Main handles the parameter validation. A reference to the
FileSystemObject is assigned to the
fso variable. The arguments are referenced by the
objArgs variable which holds an array of values. The first three parameters are required. The last two parameters are optional. Once the parameters are validated,
fso.GetFolder is called to get a handle for that folder. A warning is issued to allow the user to confirm the task. The
ProcessFolder function is then called to rename the files.
Dim StdIn: Set StdIn = WScript.StdIn
Dim StdOut: Set StdOut = WScript
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim FilesRenamed: FilesRenamed = 0
Dim FilesSkipped: FilesSkipped = 0
set fso = nothing
dim objArgs: Set objArgs = WScript.Arguments
if objArgs.Count > 2 then
dim path: path = objArgs(0) dim olds: olds = objArgs(1) dim news: news = objArgs(2)
dim ext1: ext1 = ""
dim ext2: ext2 = ""
if objArgs.Count > 3 then ext1 = objArgs(3) if objArgs.Count > 4 then ext2 = objArgs(4)
dim CurrentFolder: Set CurrentFolder = fso.GetFolder(path)
StdOut.Echo "Warning: All files within the directory """ & _
CurrentFolder.Path & """ will be renamed."
If Not Confirm("Continue?") Then Exit Sub
ProcessSubFolders CurrentFolder , olds, news, ext1,ext2
StdOut.Echo "Files renamed :" & FilesRenamed
StdOut.Echo "Files Skipped :" & FilesSkipped
StdOut.Echo "Usage: rename.vbs [folder path] [string to replace]" & _
" [new string] [old extension] [new extension] "
Confirm function echoes a message and waits for the standard input to return. The user must enter Y to continue.
Function Confirm (ByVal promptText)
StdOut.Echo promptText & " (y/n) "
Dim s: s = StdIn.ReadLine()
Select Case LCase(Trim(s))
Confirm = True
Confirm = False
ProcessSubFolder function iterates through all the sub-folders in the current directory, and it recursively calls the same function for each sub-folder found. This allows it to search down the tree of folders. For each folder found, it calls the
Sub ProcessSubFolders (ByVal crfolder, ByVal oldTag, ByVal newTag, ByVal extOld, ByVal extNew)
Dim Folders: Set Folders = crfolder.SubFolders
Dim Folder ProcessFolder crfolder , oldTag, newTag, extOld,extNew
For Each Folder in FoldersNext
ProcessSubFolders Folder , oldTag, newTag, extOld,extNew
ProcessFolder function does all the hard work. It iterates through all the files in the folder. If it finds the patterns in the file name, it checks to see if the extension pattern was also provided. It then replaces all the string patterns and calls
Sub ProcessFolder (ByVal folder, ByVal oldTag, ByVal newTag, ByVal extOld, ByVal extNew)
Dim Files: Set Files = folder.Files
For Each File In Files
If inStr(1,File.Name,oldTag) > 0 Then
if (extOld <> "" and extNew <> "") then
FilesRenamed = FilesRenamed + 1
FilesSkipped = FilesSkipped + 1
Points of interest
I hope this simple script can be useful to some of you as it has been to me.
- 0g10112006 - Initial version.
- 0g10232007 - Added process subfolders.