Sub test_aFFs()
Dim x() As Variant
x() = aFFs("x:\t\")
MsgBox Join(x(), vbLf)
MsgBox x(0), vbInformation, "First File"
MsgBox x(1), vbInformation, "Second File"
x() = aFFs("x:\t*", "/ad") 'Search for folders in x:\ that start with the letter "t".
MsgBox Join(x(), vbLf)
x() = aFFs("x:\t*", "/ad", True) 'Search for subfolders in x:\ that start with the letter "t".
MsgBox Join(x(), vbLf)
End Sub
Sub test2_aFFs()
Dim x() As Variant
x() = aFFs("x:\t\")
MsgBox Join(x(), vbLf)
MsgBox x(0), vbInformation, "First File"
MsgBox x(1), vbInformation, "Second File"
x() = aFFs("x:\t*", "/ad") 'Search for folders in x:\ that start with the letter "t".
MsgBox Join(x(), vbLf)
x() = aFFs("x:\t*", "/ad", True) 'Search for subfolders in x:\ that start with the letter "t".
MsgBox Join(x(), vbLf)
End Sub
Sub aFFs_Test()
Dim x() As Variant, s() As String, i As Long
x() = aFFs("c:\myfiles\excel\msword\*.doc")
MsgBox Join(x, vbLf)
For i = LBound(x) To UBound(x)
'Do your thing here, e.g.
'MsgBox x(i)
Next i
End Sub
Sub MyFoldersAndDatesCreated()
Dim a() As Variant, b() As Variant, i As Long
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
a() = aFFs("x:\", "/ad", True)
'MsgBox Join(a(), vbLf)
Range("A1").Resize(UBound(a) + 1, 1).Value2 = WorksheetFunction.Transpose(a)
b() = a() 'Set array to holder folder creation dates the same size
For i = LBound(a) To UBound(a)
b(i) = fso.GetFolder(b(i)).DateCreated
Next i
Range("B1").Resize(UBound(a) + 1, 1).Value2 = WorksheetFunction.Transpose(b)
Range("A:B").EntireColumn.AutoFit
End Sub
'Set extraSwitches, e.g. "/ad", to search folders only.
'MyDir should end in a "\" character unless searching by wildcards, e.g. "x:\test\t*
'Command line switches for the shell's Dir, http://ss64.com/nt/dir.html
Function aFFs(myDir As String, Optional extraSwitches = "", _
Optional tfSubFolders As Boolean = False) As Variant
Dim s As String, a() As String, v As Variant
Dim b() As Variant, i As Long
If tfSubFolders Then
s = CreateObject("Wscript.Shell").Exec("cmd /c dir " & _
"""" & myDir & """" & " /b /s " & extraSwitches).StdOut.ReadAll
Else
s = CreateObject("Wscript.Shell").Exec("cmd /c dir " & _
"""" & myDir & """" & " /b " & extraSwitches).StdOut.ReadAll
End If
a() = Split(s, vbCrLf)
If UBound(a) = -1 Then
MsgBox myDir & " not found.", vbCritical, "Macro Ending"
Exit Function
End If
ReDim Preserve a(0 To UBound(a) - 1) As String 'Trim trailing vblfcr
For i = 0 To UBound(a)
If Not tfSubFolders Then
s = Left$(myDir, InStrRev(myDir, "\"))
'add the folder name
a(i) = s & a(i)
End If
Next i
aFFs = sA1dtovA1d(a)
End Function
Function sA1dtovA1d(strArray() As String) As Variant
Dim varArray() As Variant, i As Long
ReDim varArray(LBound(strArray) To UBound(strArray))
For i = LBound(strArray) To UBound(strArray)
varArray(i) = CVar(strArray(i))
Next i
sA1dtovA1d = varArray()
End Function