Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Add Date To End of Text File Name

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,29679
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Add Date To End of Text File Name
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Add Date To End of Text File Name - 1/10/2006 12:59:44 PM   
  Navarre

 

Posts: 90
Score: 0
Joined: 10/19/2005
Status: offline
Hey guys,

I am wanting to add the current date to the end of all TXT files in a directory, ie bob.txt afterwards would be  - bob 20060106.txt.  I would not mind the space as to make it easier to read.

Any quick ideas, I have been having a look around but so far all I have found is something to tack the date on the beginning of the file or replace the name all together.
 
 
Post #: 1
 
 RE: Add Date To End of Text File Name - 1/10/2006 5:54:56 PM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
i use this,

MyDate = Replace(Date, "/", "-")
OutputFile = "./filecheck_Summary-" & mydate & ".txt"

thats quick and dirty, if you want to put it in a specific order, such as yyyymmdd it becomes more difficult

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to Navarre)
 
 
Post #: 2
 
 RE: Add Date To End of Text File Name - 1/11/2006 1:09:29 AM   
  DiGiTAL.SkReAM


Posts: 1184
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
quote:

ORIGINAL: Navarre

Hey guys,

I am wanting to add the current date to the end of all TXT files in a directory, ie bob.txt afterwards would be  - bob 20060106.txt.  I would not mind the space as to make it easier to read.

Any quick ideas, I have been having a look around but so far all I have found is something to tack the date on the beginning of the file or replace the name all together.


      

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to Navarre)
 
 
Post #: 3
 
 RE: Add Date To End of Text File Name - 1/11/2006 2:34:23 AM   
  ebgreen


Posts: 5069
Score: 31
Joined: 7/12/2005
Status: offline
All of this:

      

Could be:

sDate = Year(Now()) & Right(Month(Now()) & "0", 2) & Right(Day(Now()) & "00", 2)

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 4
 
 RE: Add Date To End of Text File Name - 1/11/2006 2:43:00 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
quote:

ORIGINAL: ebgreen

All of this:

      

Could be:

sDate = Year(Now()) & Right(Month(Now()) & "0", 2) & Right(Day(Now()) & "00", 2)


I like that one liner much better, i was using len like digital was.
that single line is a pain to read tho
gotta love parenthesis lol

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to ebgreen)
 
 
Post #: 5
 
 RE: Add Date To End of Text File Name - 1/11/2006 5:08:52 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Here is another way of doing this.  Essentially, enumerate the files in a folder, Use GetBaseName to get the name of the file, minus the extenstion the use getExtensionName to get the extension.  Once you have that, use move to rename the files.  Here is an example.


      

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to kirrilian)
 
 
Post #: 6
 
 RE: Add Date To End of Text File Name - 1/11/2006 5:12:56 AM   
  ziminski

 

Posts: 79
Score: 2
Joined: 1/8/2006
Status: offline
or you could use the FormatDateTime (Date, DateFormat) function
CONSTANT VALUE DESCRIPTION
VBGeneralDate 0 Display the date and time using system settings
VBLongDate 1 Display the date in long date Format Saturday, June 26, 1943
VBShortDate 2 Display the date in short date Format 6/26/43
VBLongTime 3 Display the time in long time Format 3:48:01 PM
VBShortTime 4 Display the time in short time format (24 hour clock)15:48


so that
Dim Today
Today = FormatDateTime(Date, 2)
msgbox today

(in reply to kirrilian)
 
 
Post #: 7
 
 RE: Add Date To End of Text File Name - 1/11/2006 7:43:05 AM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
How does that help?  Last time I checked "/" was not an expectable character for file names. If you mean to use it in conjunction with something else please say so, otherwise we will be seeing posts asking why they are getting an error when trying to rename their file.


Cybex

_____________________________

Common sense is not so common.

(in reply to ziminski)
 
 
Post #: 8
 
 RE: Add Date To End of Text File Name - 1/11/2006 7:47:19 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
agreed, hence the replace (from / to -) in my first reply

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to Cybex)
 
 
Post #: 9
 
 RE: Add Date To End of Text File Name - 1/11/2006 7:52:50 AM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
I saw that in yours Kirrilian.

Cybex

_____________________________

Common sense is not so common.

(in reply to kirrilian)
 
 
Post #: 10
 
 RE: Add Date To End of Text File Name - 1/11/2006 8:18:01 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
quote:

ORIGINAL: ebgreen

All of this:

      

Could be:

sDate = Year(Now()) & Right(Month(Now()) & "0", 2) & Right(Day(Now()) & "00", 2)


actually it should be:
sDate = Year(Now()) & Right("0" & Month(now()), 2) & Right("00" & Day(Now()), 2)

< Message edited by kirrilian -- 1/11/2006 8:21:40 AM >


_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to ebgreen)
 
 
Post #: 11
 
 RE: Add Date To End of Text File Name - 1/11/2006 8:20:59 AM   
  Navarre

 

Posts: 90
Score: 0
Joined: 10/19/2005
Status: offline
Thanks for that guys that was excellent.  Just another quick one, is it possible to change it so that it only does TXT files?

(in reply to Cybex)
 
 
Post #: 12
 
 RE: Add Date To End of Text File Name - 1/12/2006 2:52:01 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Try something like the bolded section below.  You will just need to change it to work in whichever example you decide to use.

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("c:\nt\test")
Set fc = f.Files

'Will format the date as an 8 digit number i.e. 112006 will become 01012006
theDate = Right(String(2,"0") & month(Now()),2) _
       & Right(String(2,"0") & Day(Now()),2) _
       & Year(Now())

For Each f1 in fc
   if right(lcase(f1.name),4) = ".txt" then
     theBaseName = fso.GetBaseName(f1.name)
     theExtension = fso.GetExtensionName(f1.Name)
     f1.Move(fso.GetParentFolderName(f1.path) & "\" & theBaseName & "_" & theDate & "." & theExtension)
     'wscript.Echo fso.GetParentFolderName(f1.path) & "\" & theBaseName & "_" & theDate & "." & theExtension
    End If
Next

< Message edited by mbouchard -- 1/12/2006 11:41:29 PM >


_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to Navarre)
 
 
Post #: 13
 
 RE: Add Date To End of Text File Name - 1/12/2006 2:55:56 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
i think a code block interprets everything literally so you would have to put that part separate to bold it

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to mbouchard)
 
 
Post #: 14
 
 RE: Add Date To End of Text File Name - 1/12/2006 8:45:06 AM   
  Navarre

 

Posts: 90
Score: 0
Joined: 10/19/2005
Status: offline
Thanks again guys that was really helpful.

(in reply to kirrilian)
 
 
Post #: 15
 
 RE: Add Date To End of Text File Name - 1/12/2006 11:40:32 PM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
quote:

ORIGINAL: kirrilian

i think a code block interprets everything literally so you would have to put that part separate to bold it

BAH, thanks.  Note to self, preview preview preview.

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to kirrilian)
 
 
Post #: 16
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Add Date To End of Text File Name Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts