Jump to content

RENAME FILES WITH DATES/TIMES


Recommended Posts

I have mostly worked with scripts using Unix shells and am struggling to do what is a simple job in Unix scripts but somehow I can't get it in windows scripts....

What I basically need to do is execute a script that will rename all files in a directory and add a date-time stamp to the file name and add a file extension...

All files in the directory will not have a file extension....

In unix it would be an easy for each loop using variables...

an example I'd like would be something like the following....

file1

file2

file3

should turn into

file120071227141820.seq

file220071227141821.seq

file320071227141821.seq

where the datetime stamp is yymmddhhmmss

The 3 files above would be in a directory and the filenames could vary

I guess vb script would be the best and or easiest but anything that works....

Thanks in advance for any guidence.....

Link to comment
Share on other sites


Try something like this:

strFolder = "c:\mypath\mydir"
Set objFSO = CreateObject("Scripting.FileSystemObject")
N = Now
strStamp = (((( Year(N)*100 + Month(N))*100 + Day(N))*100 + _
Hour(N)) * 100 + Minute(N))*100 + Second(N)
Set objFolder = objFSO.GetFolder(strFolder)
Set objFiles = objFolder.Files
For Each objFile In objFiles
objFSO.MoveFile objFile.path , objFile.parentfolder & "\" &_
objFile.name & strStamp
Next

Change the strFolder data accordingly!

Link to comment
Share on other sites

Here is another script that will do what you want.

All you have to do is drag and drop a folder on

to the script.

Save As RenameFolder.vbs

 Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim F1, F2, F3, F4, StrS
'/-> Time Stamp
Function TimeStamp()
StrS = _
Year(Date) & _
Month(Date) & _
Day(Date) & _
Hour(Time) & _
Minute(Time) & _
Second(Time)
End Function
'/-> Checks To See If Some Thing Is Drag And Drop On To The Script
If WScript.Arguments.Count = 0 Then
Act.Popup "This script needs a folder to be drag " & vbCrLf &_
"and drop on this file to start.",30,"Error No Source", 4128
WScript.Quit()
Else
For Each F1 in Wscript.Arguments
'/-> Check To See If It A File Or Folder
If Right(InStr(F1,"."),4) Then
Act.Popup "This Is A File, Cannot Continue",7,"Not A Folder",4128
Exit For
Else
'/-> Start A Collection Of The Files Inside Of The Folder
Set F2 = Fso.GetFolder(F1)
For Each F3 in F2.Files
'/-> Break Up The File Name
F4 = Split(F3.Name,".")
TimeStamp()
Fso.MoveFile F3.Path, F2.Path & "\" & F4(0) & StrS & "." & F4(1)
Next
End If
Next
End If

Link to comment
Share on other sites

Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")

Dim F1, F2, F3, F4, StrS

gunsmokingman,

I just would like to know if there is a reason why you always use DIM when there is no data types in VBScripts. IMHO, they are useless but why you use them anyway ?

Edited by jdoe
Link to comment
Share on other sites

Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")

Dim F1, F2, F3, F4, StrS

gunsmokingman,

I just would like to know if there is a reason why you always use DIM when there is no data types in VBScripts. IMHO, they are useless but why you use them anyway ?

It consider good scripting, from all I have read.

Link to comment
Share on other sites

Try something like this:
strFolder = "c:\mypath\mydir"
Set objFSO = CreateObject("Scripting.FileSystemObject")
N = Now
strStamp = (((( Year(N)*100 + Month(N))*100 + Day(N))*100 + _
Hour(N)) * 100 + Minute(N))*100 + Second(N)
Set objFolder = objFSO.GetFolder(strFolder)
Set objFiles = objFolder.Files
For Each objFile In objFiles
objFSO.MoveFile objFile.path , objFile.parentfolder & "\" &_
objFile.name & strStamp
Next

Change the strFolder data accordingly!

Thank you very much for this code... it did the trick for me....

Happy New Year.....

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...