Solved

Change file date and time stamp

Posted on 2002-05-27
14
709 Views
Last Modified: 2007-12-19
I have a vb 6.0 program that i need to change the date and time stamp on a file i created. Can anyone give me the code to do this.


thanks.
0
Comment
Question by:MICS
  • 4
  • 3
  • 3
  • +2
14 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7037425
Please maintain these open questions:
Printing in Javascript Date: 03/20/2002 08:43AM PST  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20279360
Printing Date: 01/31/2002 05:58AM PST  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20261587

Thanks,
Anthony
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 75 total points
ID: 7037708
'From www.allapi.net

'This project needs a Common Dialog box, named CDBox.
'  (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
'   and select Microsoft Common Dialog control)
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
    Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'KPDTeam@Allapi.net
    Dim m_Date As Date, lngHandle As Long
    Dim udtFileTime As FILETIME
    Dim udtLocalTime As FILETIME
    Dim udtSystemTime As SYSTEMTIME
    m_Date = Format(Now, "DD-MM-YY")

    'Set the dialog's title
    CDBox.DialogTitle = "Choose a file ..."
    'Set the dialog's filter
    CDBox.Filter = "All Files (*.*)|*.*"
    'Show the 'Open File'-dialog
    CDBox.ShowOpen

    udtSystemTime.wYear = Year(m_Date)
    udtSystemTime.wMonth = Month(m_Date)
    udtSystemTime.wDay = Day(m_Date)
    udtSystemTime.wDayOfWeek = WeekDay(m_Date) - 1
    udtSystemTime.wHour = Hour(m_Date)
    udtSystemTime.wSecond = Second(m_Date)
    udtSystemTime.wMilliseconds = 0

    ' convert system time to local time
    SystemTimeToFileTime udtSystemTime, udtLocalTime
    ' convert local time to GMT
    LocalFileTimeToFileTime udtLocalTime, udtFileTime
    ' open the file to get the filehandle
    lngHandle = CreateFile(CDBox.Filename, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
    ' change date/time property of the file
    SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
    ' close the handle
    CloseHandle lngHandle
    MsgBox "The date of the file '" + CDBox.Filename + "' has been changed to" + Str$(m_Date), vbInformation + vbOKOnly, App.Title
End Sub
0
 

Author Comment

by:MICS
ID: 7037730
umm... it must be easier than that.  I want to change a file that is hard coded. lets say "C:\MyFile.txt" and i want its created date to be Jan 9, 1996...

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7037744
Well, maybe there is another "simple" way but you did ask for the code, not the easiest one.
With Windows, the most simple thing could be harder to implement.
Cheers
0
 
LVL 17

Expert Comment

by:inthedark
ID: 7038375
Date and time handling is much more complicated than most people realise.  In fact it is so complicated that even Microsoft screwed it up.  In Windows NT if you create a file in January and then view the time of create of the same file in April, you will see that the time of creation changes.

The problem is that file times are meant to be stored in GMT and then translated into local time when they are viewed but somehow it all went wrong.  Recently I was working on a band new Windows 2000 server from Dell it was configured with US Date formats, I loaded a Novell driver which changed the way that filetime worked.  We changed the date format of the server to UK date format and my VB project forms (ABOUT 100 OF THEN) got redated back to the fifties and sixties.

In summary Richie_Simonetti, don't waste much time looking for a simpler way, I don't think there is one.....

0
 
LVL 17

Expert Comment

by:inthedark
ID: 7038405
The example posted had a bug which I have now fixed. I turned the example into an easy-to-use class.

Example of usage:

Private Sub Command1_Click()

Dim GF As New zGF         ' General Functions

Dim dt As New zFileDates
Dim d$
Dim FilePath$
Dim newdate As Date

' get the path to re-date
FilePath = "D:\CustData\Abacus\Utilities\PrintOrders\"
GF.CheckSlash FilePath ' make sure has a slash at the end


newdate = Now

' Read files from the path
d$ = Dir$(FilePath + "*.*")
Do While Len(d$) > 0
    ' change the file date/time
    dt.UpdateFileTime FilePath + d$, newdate
    d$ = Dir
Loop
MsgBox "Done"


End Sub

===============end of example

Here is the

1)Create a new class module
2)paste the following code
3)change the class name to zFileDates

====Code for zFileDates class=========

Option Explicit

' converted to a class form the code supplied here:
' the code from this location had a date/time bug
' which was fixed
' please report any errors to:
' nyoung@nyoung@vipintersoft.com

   'KPD-Team 1998
   'URL: http://www.allapi.net/
   'KPDTeam@Allapi.net

Private Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type

Private Type SYSTEMTIME
   wYear As Integer
   wMonth As Integer
   wDayOfWeek As Integer
   wDay As Integer
   wHour As Integer
   wMinute As Integer
   wSecond As Integer
   wMilliseconds As Integer
End Type

Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
         (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
   ByVal dwShareMode As Long, _
   ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, _
   ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, _
   lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" _
    (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long
Private Declare Function LocalFileTimeToFileTime _
    Lib "kernel32" (lpLocalFileTime As FILETIME, _
    lpFileTime As FILETIME) As Long

Public Sub UpdateFileTime(FileName As String, NewDateTime As Date)
 
   Dim lngHandle As Long
   Dim udtFileTime As FILETIME
   Dim udtLocalTime As FILETIME
   Dim udtSystemTime As SYSTEMTIME

   udtSystemTime.wYear = Year(NewDateTime)
   udtSystemTime.wMonth = Month(NewDateTime)
   udtSystemTime.wDay = Day(NewDateTime)
   udtSystemTime.wDayOfWeek = Weekday(NewDateTime) - 1
   udtSystemTime.wHour = Hour(NewDateTime)
   udtSystemTime.wMinute = Minute(NewDateTime) ' this was missing
   udtSystemTime.wSecond = Second(NewDateTime)
   udtSystemTime.wMilliseconds = 0

   ' convert system time to local time
   SystemTimeToFileTime udtSystemTime, udtLocalTime
   
   ' convert local time to GMT
   LocalFileTimeToFileTime udtLocalTime, udtFileTime
   
   ' open the file to get the filehandle
   lngHandle = CreateFile(FileName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
   
   ' change date/time property of the file
   SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
   
   ' close the handle
   CloseHandle lngHandle
   
End Sub





0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7038531
MICS,

Richie has given you the best solution.  The only other shorter solution that I know of is to use the File System Object.  Note that there are in fact three file dates involved, as you can see by going to the Properties window in Explorer.

In order to use the following code you need to make a reference to the Microsoft Scripting Runtime.  And uncomment the date(s) you want changed.

Sub ChangeFileDate(ByVal FileName As String, ByVal FileDate As Date)
Dim fs As Scripting.FileSystemObject
Dim fil As Scripting.File

Set fs = New Scripting.FileSystemObject
Set fil = fs.GetFile(FileName)
'fil.DateCreated = FileDate
'fil.DateLastModified = FileDate
fil.DateLastModified = FileDate
Set fil = Nothing
Set fs = Nothing

End Sub
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7038535
Sorry about the typo:
Sub ChangeFileDate(ByVal FileName As String, ByVal FileDate As Date)
Dim fs As Scripting.FileSystemObject
Dim fil As Scripting.File

Set fs = New Scripting.FileSystemObject
Set fil = fs.GetFile(FileName)
'fil.DateCreated = FileDate
'fil.DateLastAccessed = FileDate
fil.DateLastModified = FileDate

Set fil = Nothing
Set fs = Nothing

End Sub
0
 

Author Comment

by:MICS
ID: 7039100
acperkins,

the method that you posted will not work,

fil.DateCreated = FileDate
fil.DateLastAccessed = FileDate
fil.DateLastModified = FileDate

you cannot use these to assign values, they are read only.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7039249
All of this is nice but creating a class for this? Why load such object in memory and waste resources?
OOP is good 8 and i like it) but not apply it to everything always.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7039272
>>you cannot use these to assign values, they are read only<<
I stand corrected, my mistake.

Anthony

0
 

Author Comment

by:MICS
ID: 7039447
there were a few bugs that were fixed by inthedark.

inthedark, i have posted another question with 50points for you, for fixing the bugs.

thanks guys
0
 
LVL 17

Expert Comment

by:inthedark
ID: 7040601
Thanks Richie my app got redated nicely thanks....one day I'll post a question "form code vs class code" but right now I'm taking a bath with too many crocodiles.
0
 
LVL 20

Expert Comment

by:dsacker
ID: 7879317
Does anyone have a solution for changing the file dates via VB Script or WSH scripts?  I'm administering some NT scheduled tasks across multiple platforms, and have already used the FileSystemObject to successfully timestamp files for archival sake.  The API solutions above do not work in VBScript.  Any suggestions (other than writing a VB executable program *lol*)?
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now