Solved

Create Date on File

Posted on 2008-06-24
4
732 Views
Last Modified: 2008-07-10
Hi,

I have a vbscript that searches log files for certain words.  On each run of the script information is written to a file called letter_print_extract.txt.  What I want to do is if the letter_print_extract.txt file is more than 5 days old I want to move it to a new file name and re-create it again.

To do this I'm using the code :-

'Rename existing file based on modified date of previous log file
        Set SecureLogFile = objFSOLog.GetFile(strFullLogPath)
        CreatedDate = SecureLogfile.DateCreated
        ModifiedDate = SecureLogfile.DateLastModified

        If CreatedDate <= SecureDate Then
          objFSOLog.MoveFile strFullLogPath, strFullBackupPath & "_" & FormatDateToString(ModifiedDate)  & ".txt"
        End If        

This works fine, so I then want to re-create a new file with the same name as the one just moved.
Again this works, but the DateCreated does not show as 24th June but as 2nd June which is when the original was created.

To create the file I use

        Set objFSOLog = CreateObject("Scripting.FileSystemObject")
        Set LogFile = objFSOLog.OpenTextFile(strFullLogPath, ForAppending, TRUE)

The create date of this file is still 2nd June 2008.

Any ideas what I'm doing wrong. Have attached the full script for completeness.
0
Comment
Question by:HRMorton
  • 2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
dosth earned 100 total points
ID: 21854244
yes, DateCreated is always when the file is created first time

instead of checking this

If CreatedDate <= SecureDate Then

can you check for ModifiedDate <= now()-5
0
 
LVL 24

Assisted Solution

by:purplepomegranite
purplepomegranite earned 25 total points
ID: 21854254
That is odd, and I wonder if it is to do with using OpenTextFile.

Try the attached modification and see if that does the same.
Set objFSOLog = CreateObject("Scripting.FileSystemObject")
if objFSOLog.FileExists(strFullLogPath) then
   Set LogFile = objFSOLog.OpenTextFile(strFullLogPath, ForAppending) 
else
   Set LogFile = objFSOLog.CreateTextFile(strFullLogPath, ForAppending, false) 
end if

Open in new window

0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 21854278
Actually, having just checked, that is right.  If I delete a file in Windows Explorer, then create one with the same name, it retains the created date of the original file.

You could modify the log so that the first line when you recreate it is the log start date.  Then you can read this line to determine when to archive it.  Using the ModifiedDate value won't work, because it will always be recently modified as your script updates it!
0
 
LVL 15

Expert Comment

by:dosth
ID: 21971110
thanks for the points
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question