drhenning Posted December 27, 2007 Posted December 27, 2007 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....file1file2file3should turn into file120071227141820.seqfile220071227141821.seqfile320071227141821.seqwhere the datetime stamp is yymmddhhmmssThe 3 files above would be in a directory and the filenames could varyI guess vb script would be the best and or easiest but anything that works....Thanks in advance for any guidence.....
Yzöwl Posted December 27, 2007 Posted December 27, 2007 Are you sure you want yymmddhhmmss, because the example you gave is yyyymmddhhmmss
drhenning Posted December 27, 2007 Author Posted December 27, 2007 Are you sure you want yymmddhhmmss, because the example you gave is yyyymmddhhmmssyes..... two more y's... yyyymmddhhmmss in the filename....Thanks...
Yzöwl Posted December 27, 2007 Posted December 27, 2007 Try something like this:strFolder = "c:\mypath\mydir"Set objFSO = CreateObject("Scripting.FileSystemObject")N = NowstrStamp = (((( 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.FilesFor Each objFile In objFiles objFSO.MoveFile objFile.path , objFile.parentfolder & "\" &_ objFile.name & strStampNextChange the strFolder data accordingly!
gunsmokingman Posted December 27, 2007 Posted December 27, 2007 Here is another script that will do what you want.All you have to do is drag and drop a folder onto 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
jdoe Posted December 28, 2007 Posted December 28, 2007 (edited) Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject") Dim F1, F2, F3, F4, StrSgunsmokingman,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 December 28, 2007 by jdoe
gunsmokingman Posted December 28, 2007 Posted December 28, 2007 Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject") Dim F1, F2, F3, F4, StrSgunsmokingman,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.
drhenning Posted December 30, 2007 Author Posted December 30, 2007 Try something like this:strFolder = "c:\mypath\mydir"Set objFSO = CreateObject("Scripting.FileSystemObject")N = NowstrStamp = (((( 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.FilesFor Each objFile In objFiles objFSO.MoveFile objFile.path , objFile.parentfolder & "\" &_ objFile.name & strStampNextChange the strFolder data accordingly!Thank you very much for this code... it did the trick for me.... Happy New Year.....
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now