Sponsored Links

Login Form






Lost Password?

Syndicate

Home arrow RSS Feeds
File Rename Using Creation Date PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Cheyenne Harden   
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.

Comments
Add New
Gav   |165.228.158.200 |04-12-2008 21:00:53
Any chance the script could be modified so that instead of renaming the file
it:

1. Checks the creation date
2. Creates a folder named the same as the
creation year (eg, "2007" (After checking that the year folder doesn't
already exist)
3. Moves the file into that folder

That way we could move all of
our old files into years for archiving. That'd be a huge help!
Write comment
Name:
Email:
 
Title:
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

 
Tag it:
Delicious
Furl it!
Spurl
digg
YahooMyWeb
< Prev   Next >
 

Google Search

Google