Option Explicit
'by Randy Pack to collect files of certain types (or all) in folder
'v210503 subfolder
Public gvTypCode
Public gcolIgnorDirs As Collection
Public Const kCELLstartDir = "B1"
Public Const kCELLfileType = "B2"
Public Const kCELLuseSubDir = "B3"
Public gbUseSubDirs As Boolean
Public Sub ScanSubfolders(ByVal pvStartDir)
Dim FileSystem As Object
Range("A2").Select
Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(pvStartDir)
Set FileSystem = Nothing
End Sub
Sub DoFolder(Folder)
Dim SubFolder
Dim oFile
Dim fso
Dim i As Integer
Set fso = CreateObject("Scripting.FileSystemObject")
'Set Folder = fso.GetFolder(Folder)
For Each SubFolder In Folder.SubFolders
'Debug.Print SubFolder
'For i = 1 To gcolIgnorDirs.Count
' If InStr(vDir, gcolIgnorDirs(i)) > 0 Then GoTo skipDir
'Next
DoFolder SubFolder
skipDir:
Next
'list each file in folder
For Each oFile In Folder.Files
' Operate on each file
'Debug.Print Folder, oFile.Name, oFile.DateLastModified
If oFile.Name = "FAILURE ANALYSIS 2010_Backup.mdb" Then
Beep
End If
' Debug.Print oFile.Name
If IsCorrectFileType(oFile.Name) Then
ActiveCell.Offset(0, 0).Value = oFile.Name 'filename
ActiveCell.Offset(0, 1).Value = oFile 'filename & name
ActiveCell.Offset(0, 2).Value = Folder 'folder name
'ActiveCell.Offset(0, 3).Value = Folder 'same as above
ActiveCell.Offset(0, 4).Value = Mid(oFile.Name, InStrRev(oFile.Name, ".") + 1) 'folder
ActiveCell.Offset(0, 5).Value = oFile.datelastModified 'date last mod
ActiveCell.Offset(1, 0).Select 'next row
End If
skip1:
Next
Set oFile = Nothing
Set SubFolder = Nothing
End Sub
Public Sub ScanFilesIn1Folder(ByVal pvStartDir)
Dim FileSystem As Object
Dim Folder As Object
Dim oFile As Object
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Set Folder = FileSystem.GetFolder(pvStartDir)
Range("A2").Select
For Each oFile In Folder.Files
If InStr(oFile.Name, "backup") > 0 Then GoTo skip1
If IsCorrectFileType(oFile.Name) Then 'If InStr(oFile.Name, ".accdb") > 0 Or InStr(oFile.Name, ".mdb") > 0 Then
ActiveCell.Value = oFile
ActiveCell.Offset(1, 0).Select 'next row
End If
skip1:
Next
Set oFile = Nothing
Set Folder = Nothing
Set FileSystem = Nothing
End Sub
Public Function IsCorrectFileType(ByVal pvFile) As Boolean
'If InStr(LCase(pvFile), "backup") > 0 Then
Dim vWord
Select Case UCase(gvTypCode)
Case "A"
IsCorrectFileType = (InStr(pvFile, ".accdb") > 0) Or (InStr(pvFile, ".mdb") > 0)
Case "X"
IsCorrectFileType = InStr(pvFile, ".xls") > 0
Case "W"
IsCorrectFileType = InStr(pvFile, ".doc") > 0
Case "T"
IsCorrectFileType = InStr(pvFile, ".txt") > 0
Case "*", "" 'all files
IsCorrectFileType = True
End Select
End Function
Public Sub LoadIgnorDir()
Set gcolIgnorDirs = New Collection
Sheets("Ignore").Select
Range("A2").Select
While ActiveCell.Value <> ""
gcolIgnorDirs.Add ActiveCell.Value
ActiveCell.Offset(1, 0).Select 'next row
Wend
Sheets(1).Select
End Sub
Public Sub GetFileList()
Dim fso
Dim vStartDir
Dim wsTarg As Worksheet, wsMain As Worksheet
Set wsMain = ActiveSheet
vStartDir = Range(kCELLstartDir).Value
gvTypCode = Range(kCELLfileType).Value
gbUseSubDirs = UCase(Range(kCELLuseSubDir).Value) = "Y"
Set fso = CreateObject("Scripting.FileSystemObject")
'clear
'LoadIgnorDir
'Range("A2:B600").ClearContents
'set vals
Sheets.Add
Set wsTarg = ActiveSheet
wsTarg.Activate
Range("A1").Value = "Filename"
Range("b1").Value = "Filepath"
Range("c1").Value = "folder"
Range("d1").Value = "subfolder"
Range("e1").Value = "File ext"
Range("f1").Value = "last modified date"
Range("A2").Select
If gbUseSubDirs Then
DoFolder fso.GetFolder(vStartDir)
Else
ScanFilesIn1Folder vStartDir
End If
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
MsgBox "Done"
End Sub