Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


renaming a *.txt file with currentdate

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> renaming a *.txt file with currentdate
  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 >>
 renaming a *.txt file with currentdate - 8/16/2001 5:44:39 AM   
  edmondsng

 

Posts: 1
Score: 0
Joined: 8/16/2001
From:
Status: offline
Hi All,
I am having problem writing a vbscript to rename a text file to currentdate (as the name of the file) everytime I moved it to an archive folder. This is to stop the older files from being overwritten everytime the new file is moved. Please help !!!
 
 
Post #: 1
 
 Re: renaming a *.txt file with currentdate - 8/17/2001 2:43:29 PM   
  AndrewZ@dzimmer.com

 

Posts: 4
Score: 0
Joined: 8/16/2001
From: USA
Status: offline
It might be more than you need but I created a script that would move a file from one folder to another. It checked to see if the file already existed. If it does, it added a letter to it. I use it in a form that is passed the location of the existing file.

'Splits the file path into an array
FILEARRAY = split(Request.Form("filename"), "\")

'Sets the root location.
FOLDERPATH = "d:\inetpub\wwwroot\_private\forms\" & FILEARRAY(0)

'Checks if the folder exists where it will write the file.
if FSOFILE.FolderExists(FOLDERPATH & "\archive") = FALSE then

'Creates the folder if it is not there.
set FORMFOLDER = FSOFILE.CreateFolder(FOLDERPATH & "\archive")
end if

'Gets the current Date.
ARCHIVEDATE = Year(Now) & "-" & Day(Now) & "-" & Month(Now)

'Creates the file i.e. 'TestFile<ARCHIVEDATE>.txt'
ARCHIVEFILENAME = Left(FILEARRAY(1), Len(FILEARRAY(1)) - 4) & ARCHIVEDATE & Right(FILEARRAY(1), 4)

'Sets the ANSI Character code for 'a'
COUNTER = 97
BACKUPOK = 0

'Starts looping through the files in the folder checking to see if they exist.
do while BACKUPOK = 0

'Checks if the First File Exists
if FSOFILE.FileExists(FOLDERPATH & "\archive\" & ARCHIVEFILENAME) then

'If the file exists, it appends a letter to the end of the file name. It looks like I set it up to append 2 characters if one set it used up. x, y, z, za, zb,
if COUNTER > 97 then
ARCHIVEFILENAME = Left(ARCHIVEFILENAME, Len(ARCHIVEFILENAME) - 5) & Chr(COUNTER) & Right(ARCHIVEFILENAME, 4)
else
ARCHIVEFILENAME = Left(ARCHIVEFILENAME, Len(ARCHIVEFILENAME) - 4) & Chr(COUNTER) & Right(ARCHIVEFILENAME, 4)
end if
COUNTER = COUNTER + 1
else

'Copies the file to the new folder with the new name.
set BACKUPFILE = FSOFILE.CopyFile(FOLDERPATH & "\" & FILEARRAY(1), FOLDERPATH & "\archive\" & ARCHIVEFILENAME, TRUE)

'Deletes the File
set BACKUPFILE = FSOFILE.DeleteFile(FOLDERPATH & "\" & FILEARRAY(1))
end if
loop

(in reply to edmondsng)
 
 
Post #: 2
 
 Re: renaming a *.txt file with currentdate - 8/17/2001 5:53:37 PM   
  imav8n

 

Posts: 95
Score: 0
Joined: 7/3/2001
From: USA
Status: offline
This is what I use for renaming and moving the log files on some of my servers to an archive folder. I'm actually using some other scripts that take the text files, and zip them up into a file called logfiles.zip, then moving that file to the archive. It's actually a combination of batch files and vbs files:

StartHere.bat:
-------------------------------------
del *.dt
date /t > date.dt
call dt.vbs
FOR /F "TOKENS=1" %%A IN (dt.dt) DO CALL rn.bat %%A
------------------------------------------
- The first line deletes the previous *.dt files.
- The 2nd line outputs the date into a new date.dt file in the "Thu 05/03/2001" format
- The 3rd line calls dt.vbs code for which is below. This file creates a dt.dt file which has the date converted to 05-03-2001 format.
- The 4th line takes the reformatted date from the dt.dt file, and puts it through a renaming batch file called rn.bat. Code is below.

dt.vbs
------------------------------------------
Set objFileSystem = wscript.CreateObject( "Scripting.FileSystemObject" )
Set objFilez = objFileSystem.createTextFile("e:\logs\sec\pcs\dt.dt", true)
Set objFilea = objFileSystem.OpenTextFile("e:\logs\sec\pcs\date.dt")


Do While Not objfilea.AtEndOfStream

scantext = ObjFilea.ReadLine
st = right(scantext, 11)
dt = trim(st)
mo = left(dt, 2)
da = mid(dt, 4,2)
yr = right(dt,4)
objfilez.writeline mo &"-"& da &"-"& yr
loop


------------------------------------------
------------------------------------------
rn.bat
--------------------------------------
ren logfiles.zip %1.zip
move %1.zip f:\archives\logs\sec\
--------------------------------------

Hopefully this helps, or at least gives you some ideas for how to do something similar.



~imav8n

(in reply to edmondsng)
 
 
Post #: 3
 
 
 
  

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 >> renaming a *.txt file with currentdate 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