MSFN Forum: RENAME FILES WITH DATES/TIMES - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

RENAME FILES WITH DATES/TIMES Rate Topic: -----

#1 User is offline   drhenning 

  • Group: Members
  • Posts: 3
  • Joined: 27-December 07

Posted 27 December 2007 - 01:22 PM

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


#2 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,119
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 27 December 2007 - 01:47 PM

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

#3 User is offline   drhenning 

  • Group: Members
  • Posts: 3
  • Joined: 27-December 07

Posted 27 December 2007 - 02:07 PM

View PostYzöwl, on Dec 27 2007, 02:47 PM, said:

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


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

Thanks...

#4 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,119
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 27 December 2007 - 03:07 PM

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!

#5 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 27 December 2007 - 05:22 PM

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

Quote

 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


#6 User is offline   jdoe 

  • Advanced Member
  • PipPipPip
  • Group: Banned
  • Posts: 314
  • Joined: 02-May 04

Posted 27 December 2007 - 10:02 PM

View Postgunsmokingman, on Dec 27 2007, 06:22 PM, said:

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 ?

This post has been edited by jdoe: 27 December 2007 - 10:05 PM


#7 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 28 December 2007 - 01:22 AM

View Postjdoe, on Dec 28 2007, 06:02 AM, said:

View Postgunsmokingman, on Dec 27 2007, 06:22 PM, said:

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.

#8 User is offline   drhenning 

  • Group: Members
  • Posts: 3
  • Joined: 27-December 07

Posted 29 December 2007 - 11:37 PM

View PostYzöwl, on Dec 27 2007, 04:07 PM, said:

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy