VBScript recursive rename file type

Posted on 2011-10-07
Last Modified: 2012-06-27
I am attempting to rename thousands of files from .imap to .eml this script will do it but only for 1 folder How can I tell it to do this to all sub folders as well.

Thank you
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Test'} Where " _
        & "ResultClass = CIM_DataFile")

For Each objFile In colFiles
    strExtension = objFile.Extension 
    strExtension = Replace(strExtension, "imap", "eml")
    strNewName = objFile.Drive & objFile.Path & objFile.FileName & "." & strExtension
    errResult = objFile.Rename(strNewName)

Open in new window

Question by:ChicagoMikeW
    LVL 59

    Accepted Solution

    Try the following, it's not your base code ... but should help with the sub folders

    Dim fldr
    Dim fso
    Dim filecount
    Dim strExtension
        Set fso = CreateObject("scripting.filesystemobject")
        ROOTFOLDER = "c:\deleteme"
        processfolder ROOTFOLDER
    Sub processfolder(fldr)
    Dim fil
    Dim subFolder
    Dim oFolder
        Set oFolder = fso.GetFolder(fldr)
        For Each fil In oFolder.files
    '       filecount = filecount + 1
    '       wscript.echo right("000" & filecount, 4) & " : " & fil
            strExtension = fso.GetExtensionName(fil)
            If lcase(strExtension) = "imap" Then
                fso.movefile fil, left(fil, len(fil) - len(strextension)) & "eml"
            End If
        If oFolder.SubFolders.Count > 0 Then
                For Each subFolder In oFolder.SubFolders
                    processfolder subFolder
        End If
    End Sub

    Open in new window

    LVL 51

    Expert Comment

    by:Bill Prew
    Just as an alternate approach, you could do this with a fairly simple DOS command.   Right from a DOS command prompt you could just do:

    for /R "C:\Test" %A in (*.imap) do ren "%~A" "%~n.eml"

    Open in new window

    or create a simple BAT script to run like:

    @echo off
    for /R "C:\Test" %%A in (*.imap) do ren "%%~A" "%%~n.eml"

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
    This is an addendum to the following article: Acitve Directory based Outlook Signature ( The script is fine, and works in normal client-server domains…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now