File Rename Using Creation Date

By | July 27, 2006

I have a call accounting package that creates call reports. The only problem is that the report is always the same name.
Well, as you guessed I'm lazy! So, I needed a way to rename a file and use the creation date in the files name.
Here is a script that will rename a file based on its creation date and then move it to an archive folder.

———-COPY EVERYTHING BELOW THIS LINE———-

'Created April 21 2006 By C.E. Harden

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='I:\Call Reports'} Where "& "ResultClass = CIM_DataFile")
'"I:\Call Reports" should be changed to where the file currently resides.

For Each objFile In FileList
    strDate = Left(objFile.CreationDate, 8)
    strNewName = objFile.Drive & objFile.Path  & "Daily Call Report " & strDate  & "." & "DOC"
    strNameCheck = Replace(strNewName, "\", "\\")

    i = 1
    Do While True
        Set colFiles = objWMIService.ExecQuery _
            ("Select * from Cim_Datafile Where Name = '" & strNameCheck & "'")
        If colFiles.Count = 0 Then
            errResult = objFile.Rename(strNewName)
            Exit Do
        Else
            i = i + 1
     strNewName = objFile.Drive & objFile.Path  & "Daily Call Report " & strDate  & "." & "DOC"
'Use the line below if you need to append a number to the end of a file if multiple files have the 'same creation date andMake sure you comment out the line above
            'strNewName = objFile.Drive & objFile.Path & "Daily Call Report " & strDate & "_" & i & "." & "DOC"
            strNameCheck = Replace(strNewName, "\", "\\")
        End If
    Loop
Next

 

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "I:\Call Reports\*.DOC" , "I:\Call Reports\2006 call reports"

 

———-COPY EVERYTHING ABOVE THIS LINE———-

You can schedule this to run as a task.

PLEASE MAKE SURE NO WORD WRAPPING IS HAPPENING IN YOUR NOTEPAD!!!

*Make sure that all users have write access to the path you have chosen!

Run this script as a scheduled task to rename your file regularly.

This information is provided "AS IS" with no warranties expressed or implied.

Leave a Reply

Your email address will not be published. Required fields are marked *