Newsgroup: it.comp.lang.visual-basic
Titolo: Modifica data file

Ciao.
Qualcuno conosce una funzione,o una API per modificare da VB5 la data di
modifica di un file ??


L' API da usare e' SetFileTime, ma non e' cosi' facile come sembra perche'
Windows usa un valore a 64 bit che contiene il numero di intervalli di 100
ns trascorsi dal 1/1/1601 e tale valore e' riferito all'ora di Greenwich.
I passi da compiere sono (vedi sotto per i dettagli):
1.Ottenere un handle per il file tramite CreateFile
2.Leggere i dati relativi nelle tre strutture FILETIME con GetFileTime (per
poterle passare a SetFileTime se non vanno modificate)
3.Valorizzare i dati di una o piu' strutture (in base alle date che si
vogliono cambiare) SYSTEMTIME
4.convertire la struttura SYSTEMTIME in una struttura FILETIME con
SystemTimeToFileTime
5.Passare dall'ora locale all'ora di Greenwich con LocalFileTimeToFileTime
6.settare l'ora con SetFileTime
7.rilasciare l'handle del file con CloseHandle.
Codice di esempio:
Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As
FILETIME, lpLocalFileTime As FILETIME) As Long
Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As
FILETIME, lpFileTime As FILETIME) As Long
Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long,
lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As
FILETIME) As Long
Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long,
lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As
FILETIME) As Long
Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As
FILETIME, lpSystemTime As SYSTEMTIME) As Long
Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As
SYSTEMTIME, lpFileTime As FILETIME) As Long
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
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type
Public 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
Public Const FILE_SHARE_READ = &H1
Public Const OPEN_EXISTING = 3
Public Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000
Private Sub Command1_Click()
Dim FileHandle As Long
Dim newTime As SYSTEMTIME
Dim MyDateTime As FILETIME
Dim MyLocalDateTime As FILETIME
Dim CreationTime As FILETIME
Dim LastAccessTime As FILETIME
Dim LastWriteTime As FILETIME
Dim ret As Long
'Ottiene l'Handle del file
FileHandle = CreateFile("C:\Autoexec.bat", GENERIC_READ Or GENERIC_WRITE,
FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0)
'legge gli orari di creazione, ultimo accesso, ultima modifica
ret = GetFileTime(FileHandle, CreationTime, LastAccessTime, LastWriteTime)
'inserisce i nuovi dati
With newTime
    .wYear = 1998
    .wMonth = 2
    .wDay = 7
    .wHour = 13
    .wMinute = 20
    .wSecond = 0
    .wMilliseconds = 0
End With
'li converte in file time
ret = SystemTimeToFileTime(newTime, MyLocalDateTime)
'passa dall'ora locale all'ora di Greenwich
ret = LocalFileTimeToFileTime(MyLocalDateTime, MyDateTime)
'imposta l'orario che si vuole modificare
ret = SetFileTime(FileHandle, CreationTime, LastAccessTime, MyDateTime)
'chiude l'handle
CloseHandle FileHandle
End Sub
Saluti.