Script Center > Gallery > Storage > Rename All the Files in a Folder
TechNet Script Center logo

Welcome to the TechNet Script Center Gallery!

Each contribution is licensed to you under a License Agreement by its owner, not Microsoft. Microsoft does not guarantee the contribution or purport to grant rights to it.

Rename All the Files in a Folder

(Community)
Rate it:
 
 
 
 
 
Script Code
VBScript
'  RenameFiles.vbs
'    Renames all files in a specified folder (P1) to a specified naming prefix (P2)
'    In addition, it appends _(number) to each filename prefix where (number) is a
'    sequentially increasing number for each file. Filetypes are not modified.
'          RenameFiles.vbs "c:\images\" Mickey
'    will rename all files in the C:\images folder to Mickey_01.jpg, Mickey_02.jpg, etc.
'
'   This is very handy for renaming a batch of files obtained from a digital camera!
'
Option Explicit
Dim oCmd, oFolder, oFSO, oFileList, oFile
Dim sRenamePath, sRenamePrefix, sFileExtension, sConfirmRename
Dim iFileCount, iFileIndex
Dim bConfirmEach

Set oCmd = Wscript.Arguments

Select Case (oCmd.Count)
    Case 2
      sRenamePath = oCmd.item(0)
      sRenamePrefix = oCmd.item(1)
      bConfirmEach = False
    Case 3
      sRenamePath = oCmd.item(0)
      sRenamePrefix = oCmd.item(1)
      bConfirmEach = oCmd.item(2)
    Case Else
      WScript.Echo "RenameFiles.vbs requires 2 parameters:" &_
          vbcrlf & "1) Folder Path (or . for current folder)" &_
          vbcrlf & "2) File Prefix" &_
          vbcrlf & "3) Confirm each file? True/*False* (optional)"
      WScript.Quit
End Select
'
' Get a list of all files in the specified folder
'
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sRenamePath)

Set oFileList = oFolder.Files
For Each oFile in oFileList
    iFileCount = iFileCount + 1
Next

For Each oFile in oFileList
    sFileExtension = Right(oFile.Name,Len(oFile.Name)-InStr(oFile.Name,"."))
    If (LCase(sFileExtension) <> "db" AND LCase(sFileExtension) <> "jbf") Then        
       'Skip thumbs.db and PSP.jbf files
        iFileIndex = iFileIndex + 1
        If (bConfirmEach) Then
            sConfirmRename = LCase(InputBox("Rename " & oFile.Name & "?"))
        Else
            sConfirmRename = "y"
        End If
        If (sConfirmRename = "y") Then
            oFile.Move (sRenamePath & "\" & sRenamePrefix & "_" & _
                ZeroFill(iFileIndex,Len(iFileCount)) & "." & sFileExtension)
        End If
    End If
Next
Set oFolder = Nothing
Set oFSO = Nothing
WScript.Echo "Done"
WScript.Quit
'--------------------------------------------------------
Function ZeroFill (sString, iFieldLength)
    ZeroFill = Right(String(iFieldLength, "0") & Ltrim(sString), iFieldLength)
End Function
Platforms
Windows Server 2008 R2 No
Windows Server 2008 No
Windows Server 2003 No
Windows 7 No
Windows Vista No
Windows XP No
Windows 2000 No
For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script.
Disclaimer The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
Be the first to create a discussion.