VBScript recursive rename file type

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)
Next

Open in new window

LVL 2
ChicagoMikeWAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris BottomleySoftware Quality Lead EngineerCommented:
Try the following, it's not your base code ... but should help with the sub folders

Chris
Dim fldr
Dim fso
Dim ROOTFOLDER
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
    Next
    If oFolder.SubFolders.Count > 0 Then
            For Each subFolder In oFolder.SubFolders
                processfolder subFolder
        Next
    End If
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
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

~bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.