PDA

Click to See Complete Forum and Search --> : date and time file created


gray bale
Nov 10th, 1999, 07:09 PM
How do I extract the date and time from a file, via Visual Basic, and then set it to a new date and time.

smalig
Nov 10th, 1999, 07:39 PM
Use DateCreated and DateLastModified property to return the date and time that the specified file or folder was created and modified.

Sub ShowFileAccessInfo(filespec)
Dim fs, f, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(filespec)
s = UCase(filespec) & vbCrLf
s = s & "Created: " & f.DateCreated & vbCrLf
s = s & "Last Accessed: " & f.DateLastAccessed & vbCrLf
s = s & "Last Modified: " & f.DateLastModified
MsgBox s, 0, "File Access Info"
End Sub

------------------
smalig
smalig@hotmail.com
smalig.tripod.com (http://smalig.tripod.com)

Mark Sreeves
Nov 10th, 1999, 08:21 PM
Setting the date is a bit more complicated

I've cut this out of one of my own progs so let me know if something seems to be missing!

Option Explicit

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 SetFileTimeWrite Lib "kernel32" Alias "SetFileTime" (ByVal hFile As Long, ByVal MullP As Long, ByVal NullP2 As Long, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
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 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 Command1_Click()
Dim year As Integer, Month As Integer
Dim Day As Integer, Hour As Integer
Dim Minute As Integer, Second As Integer
Dim TimeStamp As Variant
Dim Filename As String
Dim X As Integer
Dim i As Integer

Me.MousePointer = vbHourglass

'this sets to current date
'you can change this to a diffent date
year = Format(Date, "yyyy")
Month = Format(Date, "mm")
Day = Format(Date, "dd")
Hour = Format(Time, "hh")
Minute = Format(Time, "nn")
Second = Format(Time, "ss")


TimeStamp = DateSerial(year, Month, Day) + TimeSerial(Hour, Minute, Second)

'filename is the file you want to alter

X = ModifyFileStamp(Filename, TimeStamp)


Me.MousePointer = vbDefault
End Sub

'Create a new function called ModifyFileStamp. Add the following code to this function:
Function ModifyFileStamp(Filename As String, TimeStamp As Variant) As Integer
Dim X As Long
Dim Handle As Long
Dim System_Time As SYSTEMTIME
Dim File_Time As FILETIME
Dim Local_Time As FILETIME

System_Time.wYear = year(TimeStamp)
System_Time.wMonth = Month(TimeStamp)
System_Time.wDay = Day(TimeStamp)
System_Time.wDayOfWeek = WeekDay(TimeStamp) - 1
System_Time.wHour = Hour(TimeStamp)
System_Time.wMinute = Minute(TimeStamp)
System_Time.wSecond = Second(TimeStamp)
System_Time.wMilliseconds = 0

'convert the system time to a file time
X = SystemTimeToFileTime(System_Time, Local_Time)

'convert local file time to file time based on UTC
X = LocalFileTimeToFileTime(Local_Time, File_Time)

'open the file so we can get a file handle to the file
Handle = CreateFile(Filename, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)

'now change the file time and date stamp
X = SetFileTimeWrite(Handle, ByVal 0&, ByVal 0&, File_Time)
CloseHandle Handle

End Function


------------------
Mark Sreeves
Analyst Programmer

Mark.Sreeves@Softlab.co.uk
A BMW Group Company

billwagnon
Nov 10th, 1999, 08:51 PM
I am trying to use Smallig's code in a VBA project.

I get error 429 "ActiveX component can't
create object" on the "CreateObject
("Scripting.FileSystemObject") line.

My guess is that the project needs a
reference to create the object, but I don't
know which one. Any guesses? Thanks.



[This message has been edited by billwagnon (edited 11-11-1999).]

billwagnon
Nov 10th, 1999, 09:27 PM
It looks like I need Scrrun.dll, but it isn't
on my machine. Can you email it to me and let me know where it goes?