|
指定フォルダ以下の全ての更新日付を取得するサンプル(VB6)

|
<このサンプルの概要>
このサンプルの仕様は、タイトルそのままです。このサンプルではc:\temp\以下の全ての
ファイル/フォルダの更新日付を取得しています。GetFileDate 関数は再帰的アルゴリズ
ムを採用していて、フォルダを発見するとGetFileDate を再度呼び出し、その下にあるフ
ァイル/フォルダの更新日付を取得しています。それを繰り返す事で指定フォルダ以下の
全てのファイル/フォルダの全ての更新日付を取得しています。
Private FileName() As String
'
' 関数名 : Command1_Click
' 返り値 : 無し
' 引き数 : 無し
' 機能説明 : ファイル日時取得ボタンクリック
' 備考 :
' 著作権 : Copyright(C) 2007 のん All right reserved
'
Private Sub Command1_Click()
On Error GoTo ErrHandler
Dim orgPath As String
ReDim FileName(0 To 0)
orgPath = orgPath & "c:\temp\"
' 指定フォルダ以下の全ての更新日付を取得する
GetFileDate orgPath, orgPath
Set fso = Nothing
Exit Sub
ErrHandler:
Set fso = Nothing
MsgBox Err.Description
Exit Sub
End Sub
'
' 関数名 : GetFileDate
' 返り値 : 無し
' 引き数 : orgPath(i) : 更新日付を取得する初期フォルダPATH
' : path (i) : orgPathと同じPATHを指定する
' 機能説明 : 指定フォルダ以下の全ての更新日付を取得する
' 備考 : RECYCLERフォルダは無視する
' 著作権 : Copyright(C) 2007 のん All right reserved
'
Public Sub GetFileDate(ByVal orgPath As String, ByVal path As String)
On Error GoTo ErrHandler
Dim fobj As Object
Dim fdate As Date
Dim fso As FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
'If fso.FolderExists(path) Then
' If fso.GetFolder(path).Attributes And System Then
' Exit Sub
' End If
'End If
'If fso.FileExists(path) Then
' If fso.GetFile(path).Attributes And System Then
' Exit Sub
' End If
'End If
If InStr(Format$(path, ">"), Format$(orgPath, ">") & "RECYCLER") > 0 Then
Exit Sub
End If
If fso.FolderExists(path) Then
' フォルダーの日時取得
If orgPath <> path Then
fdate = fso.GetFolder(path).DateLastModified
ReDim Preserve FileName(UBound(FileName) + 1)
FileName(UBound(FileName)) = Replace$(path & "\", orgPath, "") & _
vbTab & Format$(fdate, "yyyy/mm/dd hh:mm:ss")
End If
' フォルダー内のファイルの日時取得
For Each fobj In fso.GetFolder(path).Files
GetFileDate orgPath, fobj.path
Next
' フォルダー内のフォルダの日時取得
For Each fobj In fso.GetFolder(path).SubFolders
GetFileDate orgPath, fobj.path
Next
Set fobj = Nothing
ElseIf fso.FileExists(path) Then
' ファイルの日時取得
fdate = fso.GetFile(path).DateLastModified
ReDim Preserve FileName(UBound(FileName) + 1)
FileName(UBound(FileName)) = Replace$(path, orgPath, "") & _
vbTab & Format$(fdate, "yyyy/mm/dd hh:mm:ss")
End If
DoEvents
Set fso = Nothing
Exit Sub
ErrHandler:
Set fso = Nothing
MsgBox Err.Description
Exit Sub
End Sub