|
FSO使ってこんなでは
Option Explicit
Public Sub Sample_1()
Dim i As Long
Dim vntFileNames As Variant
Dim strPath As String
Dim strCompe As String
Dim strProm As String
'フォルダ名を指定
strPath = "\\10.10.10.*\01便利機能"
'指定形式のファイル名を取得
strCompe = "^★."
If Not GetFilesList(vntFileNames, strPath, strCompe, "xls") Then
GoTo Wayout
End If
For i = 1 To UBound(vntFileNames, 1)
MsgBox vntFileNames(i), vbInformation
Next i
strProm = "処理が完了しました"
Wayout:
MsgBox strProm, vbInformation
End Sub
Private Function GetFilesList(vntFileNames As Variant, _
strFilePath As String, _
Optional strNamePattan As String = ".*", _
Optional strExtePattan As String = ".*") As Boolean
Dim i As Long
Dim objFiles As Object
Dim objFile As Object
Dim regExten As Object
Dim regName As Object
Dim vntRead() As Variant
Dim strName As String
Dim objFSO As Object
'FSOのオブジェクトを取得
Set objFSO = CreateObject("Scripting.FileSystemObject")
'フォルダの存在確認
If Not objFSO.FolderExists(strFilePath) Then
GoTo Wayout
End If
'regExtenpのオブジェクトを取得(正規表現を作成)
Set regExten = CreateObject("VBScript.RegExp")
With regExten
'パターンを設定
.Pattern = strExtePattan
'大文字と小文字を区別しないように設定
.IgnoreCase = True
End With
Set regName = CreateObject("VBScript.RegExp")
With regName
'パターンを設定
.Pattern = strNamePattan
'大文字と小文字を区別しないように設定
.IgnoreCase = True
End With
'フォルダオブジェクトを取得
Set objFiles = objFSO.GetFolder(strFilePath).Files
'ファイルの数が0でなければ
If objFiles.Count <> 0 Then
For Each objFile In objFiles
With objFile
strName = .Path
'検索をテスト
If regExten.test(objFSO.GetExtensionName(strName)) Then
If regName.test(objFSO.GetBaseName(strName)) Then
i = i + 1
ReDim Preserve vntRead(1 To i)
vntRead(i) = strName
End If
End If
End With
Next objFile
End If
Set regExten = Nothing
Set regName = Nothing
If i <> 0 Then
vntFileNames = vntRead
GetFilesList = True
End If
Wayout:
'フォルダオブジェクトを破棄
Set objFiles = Nothing
Set objFile = Nothing
Set objFSO = Nothing
End Function
|
|