Solved

Rename Files by last modified date and extension with VBScript

Posted on 2009-07-02
2
1,671 Views
Last Modified: 2013-11-10
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

0
Comment
Question by:-Manlytrash-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 250 total points
ID: 24765790
You could test for the extension with a separate If statement.


Set objWMIService = GetObject("winmgmts:\\" & strComputername & "\root\cimv2")
Set FileList = objWMIService.ExecQuery ("ASSOCIATORS OF {Win32_Directory.Name='" & _
    FilePath & "'} where ResultClass = CIM_DataFile")
 
' Rename files with computer name and last date modified by extension
For Each objFile In FileList
    If LCase(objFile.Extension) = LCase(FileExt) Then
        strDate = Left(objFile.LastModified, 8)
        strNewName = objFile.Drive & objFile.Path & strComputername & "-" & strDate & "." & FileExt
        errResult = objFile.Rename(strNewName)
    End If
Next

Open in new window

0
 

Author Closing Comment

by:-Manlytrash-
ID: 31599323
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!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

729 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