Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1033
  • Last Modified:

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)

Open in new window

1 Solution
Chris BottomleyCommented:
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

Bill PrewCommented:
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


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now