BOOL FindClose(hFind);
Declare Function FindClose Lib "kernel32" (ByVal hFind As Long) As Long
引数の意味は、以下の通り。
| hFind | (IN) | FindFirstFileの戻り値の検索ハンドル。 |
戻り値は、関数の実行に成功したか否かを表す真偽値である。
/* My Documents配下のファイルを全て検索する */
void DoFind()
{
HANDLE hFind;
WIN32_FIND_DATA fd;
FILETIME ft;
SYSTEMTIME st;
/* 最初のファイル検索 */
hFind = FindFirstFile("\\my documents\\*.*", &fd);
/* 検索失敗? */
if(hFind == INVALID_HANDLE_VALUE) {
printf("検索失敗\n");
return; /******** エラー終了 ********/
}
do {
/* 更新時間取得 */
FileTimeToLocalFileTime(&fd.ftLastWriteTime, &ft);
FileTimeToSystemTime(&ft, &st);
/* 結果の表示 */
printf("ファイル名: %s", fd.cFileName);
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
printf("(ディレクトリ)");
}
printf("\n短いファイル名: %s\n", fd.cAlternateFileName);
printf("ファイルサイズ: %d\n", fd.nFileSizeLow);
printf("更新日: %04d/%02d/%02d %02d:%02d:%02d\n\n",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
} while(FindNextFile(hFind, &fd)); '次のファイルを検索
/* 検索終了 */
FindClose(hFind);
}
'主な宣言
Public Type SYSTEMTIME
Year As Integer
Month As Integer
DayOfWeek As Integer
Day As Integer
Hour As Integer
Minute As Integer
Second As Integer
Milliseconds As Integer
End Type
Public Type FILETIME
DateTimeLow As Long
DateTimeHigh As Long
End Type
Public Type WIN32_FIND_DATA
Attributes As Long
CreationTime As FILETIME
AccessTime As FILETIME
WriteTime As FILETIME
FileSizeHigh As Long
FileSizeLow As Long
Reserved0 As Long
Reserved1 As Long
FileName As String * MAX_PATH
ShortFileName As String * 14
End Type
'My Documents配下のファイルを全て検索する
Private Sub DoFind()
Dim hFind As Long
Dim FindData As WIN32_FIND_DATA
Dim FTime As FILETIME
Dim STime As SYSTEMTIME
Dim Msg As String
Dim bResult As Long
'最初のファイル検索
hFind = FindFirstFile("\my documents\*.*", FindData)
'検索失敗か?
If hFind = INVALID_HANDLE_VALUE Then
MsgBox "検索失敗"
Exit Sub '******** エラー終了 ********
End If
Do
'ファイル更新日を時分秒形式に変換
FileTimeToLocalFileTime FindData.WriteTime, FTime
FileTimeToSystemTime FTime, STime
'検索結果のメッセージ作成
With FindData
Msg = "検索結果:" & vbCr
'NULL文字を取り除く
.FileName = Left(.FileName, InStr(.FileName, vbNullChar) - 1)
.ShortFileName = Left(.ShortFileName, InStr(.ShortFileName, vbNullChar) - 1)
'検索結果のファイル名
Msg = Msg & "ファイル名:" & Trim(.FileName)
'ディレクトリかどうか
If (.Attributes And FILE_ATTRIBUTE_DIRECTORY) <> 0 Then
Msg = Msg & "(ディレクトリ)"
End If
'短いファイル名
Msg = Msg & vbCr & "短いファイル名:" & Trim(.ShortFileName) & vbCr
'ファイルサイズ(下位32bit)
Msg = Msg & "サイズ:" & .FileSizeLow & vbCr
'更新日
With STime
Msg = Msg & "更新日:" & .Year & "/" & .Month & "/" & .Day
Msg = Msg & " " & .Hour & ":" & .Minute & ":" & .Second
End With
End With
'検索結果表示
MsgBox Msg
'次を検索
bResult = FindNextFile(hFind, FindData)
Loop While bResult <> 0
'検索の終了
FindClose hFind
End Sub