Charles Yeung
Charles Yeung

Reputation: 38815

VBS - batch prefix files with date

Suppose I have the files as below:

c:\tmp\
       |_ tony.txt
       |_ peter.txt
       |_ mary.txt
       |_ may.txt

How can I write the .vbs file to batch append TODAY date to the front of the file? The result should look like:

c:\tmp\
       |_ 20110630_tony.txt
       |_ 20110630_peter.txt
       |_ 20110630_mary.txt
       |_ 20110630_may.txt

Thanks

Upvotes: 1

Views: 1753

Answers (1)

rd1966
rd1966

Reputation: 185

Would this get you started? You'll probably want to add some error checking...

Option Explicit

Const FOLDER_PATH = "C:\TMP"
Dim fso, folder, file, newFileName

Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(FOLDER_PATH)
For Each file In folder.Files
    newFileName = Year(Now) & Right("0" & Month(Now),2) & Right("0" & Day(Now),2) & "_" & file.Name
    fso.MoveFile file.Path,file.ParentFolder.Path & "\" & newFileName
Next

Upvotes: 4

Related Questions