Jump to content

Recommended Posts

Posted

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.....


Posted
Are you sure you want yymmddhhmmss, because the example you gave is yyyymmddhhmmss

yes..... two more y's... yyyymmddhhmmss in the filename....

Thanks...

Posted

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!

Posted

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

Posted (edited)
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
Posted
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.

Posted
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.....

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

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