Top Index
| 01 | Imports System.IO | |
| 02 | Imports System.Text.RegularExpressions | |
| 03 | ||
| 04 | Public Class IndexDirectory | |
| 05 | Private _IndexFilePath As String | |
| 06 | ||
| 07 | 'インデックスファイル指定なし | |
| 08 | Public Sub New() | |
| 09 | Me.IndexFilePath = Path.Combine(Path.GetTempPath, Path.GetTempFileName) | |
| 10 | End Sub | |
| 11 | ||
| 12 | 'インデックスファイル指定あり | |
| 13 | Public Sub New(ByVal IndexFilePath As String) | |
| 14 | Me.IndexFilePath = IndexFilePath | |
| 15 | End Sub | |
| 16 | ||
| 17 | 'インデックスファイルパス | |
| 18 | Public Property IndexFilePath() As String | |
| 19 | Get | |
| 20 | Return _IndexFilePath | |
| 21 | End Get | |
| 22 | Set(ByVal Value As String) | |
| 23 | _IndexFilePath = Value | |
| 24 | End Set | |
| 25 | End Property | |
| 26 | ||
| 27 | 'インデックスファイルの作成(複数ディレクトリ) | |
| 28 | Public Sub CreateIndexFile(ByVal ParamArray Args() As Object) | |
| 29 | 'ファイルを作り直す | |
| 30 | Try | |
| 31 | If File.Exists(Me.IndexFilePath) Then File.Delete(Me.IndexFilePath) | |
| 32 | File.CreateText(Me.IndexFilePath).Close() | |
| 33 | Catch ex As Exception | |
| 34 | Throw New Exception("インデックスファイルの作成に失敗しました。", ex) | |
| 35 | End Try | |
| 36 | ||
| 37 | Dim em As IEnumerator = Args.GetEnumerator | |
| 38 | While em.MoveNext | |
| 39 | Dim D As String = DirectCast(em.Current, String) | |
| 40 | If Not Directory.Exists(D) Then D = Path.GetDirectoryName(D) | |
| 41 | ||
| 42 | CreateIndexFile(D) | |
| 43 | End While | |
| 44 | End Sub | |
| 45 | ||
| 46 | 'インデックスファイルの作成(単一ディレクトリ) | |
| 47 | Private Sub CreateIndexFile(ByVal DirectoryName As String) | |
| 48 | Try | |
| 49 | Dim em As IEnumerator = Directory.GetDirectories(DirectoryName).GetEnumerator | |
| 50 | While em.MoveNext | |
| 51 | Dim D As String = DirectCast(em.Current, String) | |
| 52 | Me.CreateIndexFile(D) | |
| 53 | ||
| 54 | Dim Files As String() = Directory.GetFiles(D) | |
| 55 | Me.WriteIndexFile(Files) | |
| 56 | End While | |
| 57 | Catch ex As Exception | |
| 58 | 'フォルダへのアクセス権がない場合、ここでCatchだけしておく。 | |
| 59 | End Try | |
| 60 | End Sub | |
| 61 | ||
| 62 | 'インデックスファイルへの書き込み | |
| 63 | Private Sub WriteIndexFile(ByVal Files As String()) | |
| 64 | Dim sw As IO.StreamWriter | |
| 65 | Dim em As IEnumerator = Files.GetEnumerator | |
| 66 | ||
| 67 | Try | |
| 68 | sw = New IO.StreamWriter(Me.IndexFilePath, True) | |
| 69 | While em.MoveNext | |
| 70 | sw.WriteLine(DirectCast(em.Current, String)) | |
| 71 | End While | |
| 72 | Catch ex As Exception | |
| 73 | Throw New Exception("インデックスファイルの書き込みに失敗しました。", ex) | |
| 74 | Finally | |
| 75 | sw.Close() | |
| 76 | End Try | |
| 77 | End Sub | |
| 78 | ||
| 79 | 'インデックスファイルから、正規表現を用いてファイル名を探す | |
| 80 | Public Function GetFiles(ByVal pattern As String) As ArrayList | |
| 81 | Dim sr As IO.StreamReader | |
| 82 | Dim Files As ArrayList = New ArrayList | |
| 83 | ||
| 84 | Try | |
| 85 | sr = New IO.StreamReader(Me.IndexFilePath) | |
| 86 | Dim s As String = sr.ReadLine | |
| 87 | Do Until (s = Nothing) | |
| 88 | If Regex.IsMatch(Path.GetFileName(s), pattern) Then Files.Add(s) | |
| 89 | s = sr.ReadLine | |
| 90 | Loop | |
| 91 | Catch ex As Exception | |
| 92 | Throw New Exception("インデックスファイルの読込に失敗しました。", ex) | |
| 93 | Finally | |
| 94 | sr.Close() | |
| 95 | End Try | |
| 96 | ||
| 97 | Return Files | |
| 98 | End Function | |
| 99 | End Class |