|
|||||
VBS(VB SCRIPT/WSH)やVBAで手軽にファイル一覧を出力する方法をご紹介します。 このサンプルではファイルPATHに加えて更新日時、サイズ、行数(ステップ数)を出力します。 ファイルの行数(ステップ数)を出力する関連のサンプルはこちらです。 ファイル一覧にステップ数(行数)を表示するエクセル(VBA) フォルダ一覧関係のその他のツールやサンプルソースはこちらです。 ファイル比較/フォルダ比較(ツール&サンプル) フォルダ内のファイル比較ツール ★ファイル一覧サンプルソース
const ForReading = 1
' --------------------------------------------------------------------
' フォルダ内のファイル一覧(更新日時、サイズ、行数付き)の取得
Redim path_list(0)
GetFDate "D:\Temp\test", path_list
' フォルダ内のファイル一覧(更新日時、サイズ、行数付き)の出力
FListWrite "D:\Temp\out.txt", path_list
' --------------------------------------------------------------------
' フォルダ内のファイル一覧(更新日時、サイズ、行数付き)の取得----------
Public Sub GetFDate(ByVal fol_path, ByRef path_list())
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder(fol_path).files
' ファイルのPATH、更新日時、サイズの設定
ReDim Preserve path_list(UBound(path_list) + 1)
path_list(UBound(path_list)) = _
f.path & vbTab & _
fso.GetFile(f.path).DateLastModified & vbTab & _
fso.GetFile(f.path).Size
' ファイルの行数の設定----------------------------------
' (注意)バイナリファイルの行数も設定してしまっています。
' 拡張子で判断した方が良いかもしれません
lcnt = ""
If fso.GetFile(f.path).Size > 0 Then
Set txf = fso.OpenTextFile(f.path, ForReading)
txf.ReadAll
lcnt = txf.Line
txf.Close
Set txf = Nothing
End If
path_list(UBound(path_list)) = _
path_list(UBound(path_list)) & vbTab & lcnt
' ------------------------------------------------------
Next
For Each f In fso.GetFolder(fol_path).SubFolders
GetFDate f.path, path_list
Next
Set fso = Nothing
End Sub
' --------------------------------------------------------------------
' フォルダ内のファイル一覧(更新日時、サイズ、行数付き)の出力----------
Private Sub FListWrite(ByVal out_path, ByRef path_list())
Set fso = CreateObject("Scripting.FileSystemObject")
Set txf = fso.CreateTextFile(out_path, True)
For i = 1 To UBound(path_list)
txf.WriteLine path_list(i)
Next
txf.Close
Set txf = Nothing
Set fso = Nothing
End Sub
' --------------------------------------------------------------------
|