Link to home
Start Free TrialLog in
Avatar of -Manlytrash-
-Manlytrash-Flag for United States of America

asked on

Rename Files by last modified date and extension with VBScript

I have this VBscript that will rename all the files in a directory to the computer name and last date modified. What I need it to do is rename only files of my choosing by it's extension. I have tried several ways but have failed. Below is the code I have so far. Thanks!
' ************************************************************************************
' * Rename files by last modified date: File-Rename.vbs
' *
' * Comments:
' * Created from code snippets on the internet!
' * Will rename all files in folder to computer name and date
' * by the extension you choose
' ************************************************************************************
 
Option Explicit
 
' Defined Variables
DIM objWMIService, oWshNetwork, filelist, strnewname, objfile
Dim strComputerName, strdate, strnamecheck, errResult
 
' Set Variables, these are the only parts that need changed to suit your needs!
Const FilePath = "G:\Scripts\NT Backup Scripts\htm"  'Path to the files
Const FileExt = "html"    'File extension
 
' Set variable defaults
Set oWshNetwork = WScript.CreateObject("WScript.Network")
strComputerName = oWshNetwork.ComputerName
 
' Select File from last modified date and by extension
Set objWMIService = GetObject("winmgmts:\\" & strComputername & "\root\cimv2")
Set FileList = objWMIService.ExecQuery ("ASSOCIATORS OF {Win32_Directory.Name='" & FilePath & "'} Where extension = '" & FileExt & "' " & "ResultClass = CIM_DataFile")
 
' Rename files with computer name and last date modified by extension
For Each objFile In FileList
    strDate = Left(objFile.LastModified, 8)
    strNewName = objFile.Drive & objFile.Path & strComputername & "-" & strDate & "." & FileExt
    errResult = objFile.Rename(strNewName)
Next

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Shift-3
Shift-3
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of -Manlytrash-

ASKER

That worked perfectly! I'm not a programmer but can take others code and make it work for me most of the time. Thanks for the quick response!