-Manlytrash-
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER