Trucos Obtener fecha de creación, de última escritura y de último acceso de un fichero

Declaramos en un formulario :

Private Const OF_READ = &H0
Private Const OF_SHARE_DENY_NONE = &H40
Private Const OFS_MAXPATHNAME = 128

Private Type OFSTRUCTREC
  cBytes As Byte
  fFixedDisk As Byte
  nErrCode As Integer
  Reserved1 As Integer
  Reserved2 As Integer
  szPathName(OFS_MAXPATHNAME) As Byte
End Type

Private Type FILETIMEREC
  dwLowDateTime As Long
  dwHighDateTime As Long
End Type

Private Type SYSTEMTIMEREC
  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 Declare Function FileTimeToSystemTime Lib "kernel32" _
  (lpFileTime As FILETIMEREC, lpSystemTime As SYSTEMTIMEREC) As Long

Private Declare Function GetFileTime Lib "kernel32" (ByVal _
  hFile As Long, lpCreationTime As FILETIMEREC, lpLastAccessTime _
  As FILETIMEREC, lpLastWriteTime As FILETIMEREC) As Long

Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As _
  String, lpReOpenBuff As OFSTRUCTREC, ByVal wStyle As Long) As Long

Private Declare Function hread Lib "kernel32" Alias "_hread" _
  (ByVal hFile As Long, lpBuffer As Any, ByVal lBytes As Long) As Long

Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal _
  hFile As Long) As Long




Y para obtener las distintas fechas :

Dim sInpFile As String
Dim hFile As Integer
Dim FileStruct As OFSTRUCTREC
Dim iRC As Integer
Dim CreationTime As FILETIMEREC
Dim LastAccessTime As FILETIMEREC
Dim LastWriteTime As FILETIMEREC
Dim SystemTime As SYSTEMTIMEREC

sInpFile = "c:\autoexec.bat"

' comprobar que existe el fichero
If Len(Dir(sInpFile)) = 0 Then
  MsgBox "No puedo encontrar el fichero", vbExclamation
  Exit Sub
End If

' Abrirlo para obtener un handle
hFile = OpenFile(sInpFile, FileStruct, OF_READ Or OF_SHARE_DENY_NONE)
If hFile = 0 Then
  MsgBox "No puedo abrir el fichero", vbExclamation
  Exit Sub
End If

If GetFileTime(hFile, CreationTime, LastAccessTime, LastWriteTime) Then
    ' convertir a un formato que podamos usar
    If Not FileTimeToSystemTime(CreationTime, SystemTime) Then Debug.Print "Creación: " & SystemTime.wDay & "/" & SystemTime.wMonth & "/" & SystemTime.wYear
    If Not FileTimeToSystemTime(LastAccessTime, SystemTime) Then Debug.Print "Ultimo acceso : " & SystemTime.wDay & "/" & SystemTime.wMonth & "/" & SystemTime.wYear
    If Not FileTimeToSystemTime(LastWriteTime, SystemTime) Then Debug.Print "Ultima grabación: " & SystemTime.wDay & "/" & SystemTime.wMonth & "/" & SystemTime.wYear
End If
iRC = lclose(hFile)

NOTA
Como abrimos el fichero para obtener un handle al mismo, cada vez que miramos las fechas modificamos la de último acceso (extrañamente pone un día menos al actual).



Trucos Trucos

Visual Basic Página de Visual Basic

Página principal Página principal

www.jrubi.com