タイトル | ファイルの作成・更新・参照日付を取得する |
対象言語 | VB4.0, Access95以降 |
動作確認OS | Windows95,98,NT4.0 |
使用関数 | GetFileTime FileTimeToLocalFileTime FileTimeToSystemTime |
改築日 | 1998/11/27 |
Source Download |
ファイルの作成・更新・参照日付を取得します。 FileDateTime 関数は、ファイルの更新日しか取得できません。 作成日や参照日を取得するには、GetFileTime API関数 を使います。 GetFileTime API関数を使うと、ファイルの各時刻を取得出来ます。 但し、取得できる値は世界標準時刻なので、 FileTimeToLocalFileTimeAPI関数を使ってローカル時刻に 変換してやります。 ローカル時刻はコントロールパネルの設定に依存されます。 ファイルリストボックスで選択したファイルの作成日・更新日・参照日 を取得します。 |
![]() |
1. フォームを作成し、テキストボックス3つ(Text1,Text2,Text3
)とドライブリストボックス(Drive1)、ディレクトリボックス(Dir1) ファイルリストボックス(File1)を貼り付けてください。 Option Explicit Private Sub Dir1_Change() Me!File1.Path = Dir1 End Sub Private Sub Drive1_Change() Me!Dir1 = Drive1 End Sub Private Sub File1_Click() Dim finfo As Y_FILETIME Dim FilePath As String Me!Text1 = vbNullString Me!Text2 = vbNullString Me!Text3 = vbNullString If Right$(Me!File1.Path, 1) = "\" Then FilePath = Me!File1.Path & Me!File1 Else FilePath = Me!File1.Path & "\" & Me!File1 End If If Y_GetFileTime(FilePath, finfo) Then With finfo Me!Text1 = Format$(.CreateTime, "yyyy/mm/dd hh:mm:ss") Me!Text2 = Format$(.WriteTime, "yyyy/mm/dd hh:mm:ss") Me!Text3 = Format$(.AccessDate, "yyyy/mm/dd") End With End If End Sub |
3.モジュールウインドウを作成し、下のソースを入力してください。 Option Explicit 'ファイルを作成/開く Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, _ ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, _ ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Public Const GENERIC_READ = &H80000000 '読み取りアクセス Public Const OPEN_EXISTING = 3 'ファイルが存在する時のみオープン Public Const FILE_SHARE_READ = &H1 '他のオープン操作を実行できる Public Const INVALID_HANDLE_VALUE = -1 'オープン失敗 'オブジェクトハンドルを閉じる Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long 'ファイルの日付情報を取得する(UTC世界標準時) Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, _ lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long 'UTC世界標準時からローカル標準時(64ビット形式)に変換する Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long 'ローカ標準時(64ビット形式)からシステム時刻形式に変換 Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long '64ビット形式ファイル時刻 Type FILETIME dwLowDateTime As Long 'ファイル時刻の下位32ビット dwHighDateTime As Long 'ファイル時刻の上位32ビット End Type 'システム時刻形式ファイル時刻 Type SYSTEMTIME wYear As Integer '年 wMonth As Integer '月 wDayOfWeek As Integer '曜 0:日 1:月 2:火 3:水 4:木 5:金 6:土 wDay As Integer '日 wHour As Integer '時 wMinute As Integer '分 wSecond As Integer '秒 wMilliseconds As Integer 'ミリ秒 End Type Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End Type Type Y_FILETIME CreateTime As Date '作成日 WriteTime As Date '更新日 AccessDate As Date '参照日 End Type Public Function Y_GetFileTime(File As String, tagFileTime As Y_FILETIME) As Boolean '*********************************************************** '機能 : ファイルの作成・更新・参照日を取得する '引数 : File = ファイル名 ' tagFileTime = 日付を取得するバッファ '戻り値: True = 取得成功 Fase = 取得失敗 '*********************************************************** Dim longret As Long Dim i As Integer Dim flg As Boolean Dim Security As SECURITY_ATTRIBUTES Dim hFile As Long Dim LocalTime As FILETIME Dim LocalSystemTime As SYSTEMTIME Dim FTime(2) As FILETIME Dim RTime(2) As Date flg = False Security.nLength = Len(Security) 'ファイルをReadOnlyモードで開き、ファイルハンドルを取得する hFile = CreateFile(File, GENERIC_READ, FILE_SHARE_READ, Security, OPEN_EXISTING, 0, 0) If hFile <> INVALID_HANDLE_VALUE Then 'ファイルの日付情報を取得(UTC世界標準時) longret = GetFileTime(hFile, FTime(0), FTime(1), FTime(2)) 'ファイルを閉じる longret = CloseHandle(hFile) For i = 0 To 2 'UTC世界標準時からローカル標準時(64ビット形式)に変換 longret = FileTimeToLocalFileTime(FTime(i), LocalTime) 'ローカ標準時(64ビット形式)からシステム時刻形式に変換 longret = FileTimeToSystemTime(LocalTime, LocalSystemTime) With LocalSystemTime RTime(i) = CDate(.wYear & "/" & .wMonth & "/" & .wDay & " " & .wHour & ":" & .wMinute & ":" & .wSecond) End With flg = True Next End If If flg Then With tagFileTime .CreateTime = RTime(0) .WriteTime = RTime(2) .AccessDate = RTime(1) End With End If Y_GetFileTime = flg End Function |
Copyright (C)1997-2001 空耳工房 MY2Project All rights reserved. |