dir /ad /s /b /-C
Function Q_29065834(ByVal parmTopDir As String)
Dim strData As String
Dim oWSH As Object
Dim oFS, oTS
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Static oRE As Object
Set oWSH = CreateObject("wscript.shell")
oWSH.Run "cmd /c cd " & parmTopDir & " & dir /w /s | findstr ""File(s) Directory"" > Q_29065834.txt", 0, True
Set oFS = CreateObject("scripting.filesystemobject")
Set oTS = oFS.OpenTextFile(parmTopDir & "\Q_29065834.txt", ForReading, True, TristateFalse)
strData = oTS.readall
oTS.Close
strData = Replace(strData, " Directory of ", "")
'remove file count summary lines
strData = Left(strData, InStrRev(strData, "File(s)") - 1)
strData = Left(strData, InStrRev(strData, vbCrLf) - 1)
Set oRE = CreateObject("vbscript.regexp")
oRE.Global = True
oRE.Pattern = "\r\n +"
If oRE.test(strData) Then
strData = oRE.Replace(strData, vbTab)
End If
oRE.Pattern = "(File\(s\))( +)"
strData = oRE.Replace(strData, "$1" & vbTab)
' Set oTS = oFS.OpenTextFile(parmTopDir & "\Q_29065834.txt", ForWriting, True, TristateFalse)
' oTS.write strData
' oTS.Close
Q_29065834 = Split(strData, vbCrLf)
End Function
Example invocation:x = Q_29065834(environ("homepath") & "\downloads")
?ubound(x)
4885
?x(0)
C:\Users\Mark\Downloads 1628 File(s) 9,687,821,627 bytes
?x(4885)
C:\Users\Mark\Downloads\XmlPad302azip 1 File(s) 10,989,479 bytes
Function Q_29065834(ByVal parmTopDir As String)
Dim strData As String
Dim oWSH As Object
Dim oFS, oTS
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim oRE As Object
Dim oMatches As Object
Dim lngMatch As Long
Dim vResult As Variant
Set oWSH = CreateObject("wscript.shell")
oWSH.Run "cmd /c cd " & parmTopDir & " & dir /-C /w /s > Q_29065834.txt", 0, True
Set oFS = CreateObject("scripting.filesystemobject")
Set oTS = oFS.OpenTextFile(parmTopDir & "\Q_29065834.txt", ForReading, True, TristateFalse)
strData = oTS.readall
oTS.Close
Set oRE = CreateObject("vbscript.regexp")
oRE.Global = True
oRE.Pattern = "(?:\n|^) Directory of (\S[^\r]+\S)\r\n(?:.|\n)+?(?= +\d+ File\(s\)) +(\d+) File\(s\) +(\d[^ ]*) bytes"
If oRE.test(strData) Then
Set oMatches = oRE.Execute(strData)
ReDim vResult(0 To oMatches.Count - 1)
For lngMatch = 0 To oMatches.Count - 1
With oMatches(lngMatch)
vResult(lngMatch) = .submatches(0) & vbTab & .submatches(1) & vbTab & .submatches(2)
End With
Next
End If
Q_29065834 = vResult
End Function
Function Q_29065834a(ByVal parmTopDir As String)
Dim strData As String
Dim oWSH As Object
Dim oFS, oTS
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim oRE As Object
Dim oMatches As Object
Dim lngMatch As Long
Dim lngSM As Long
Dim vResult As Variant
Set oWSH = CreateObject("wscript.shell")
oWSH.Run "cmd /c cd " & parmTopDir & " & dir /-C /w /s | findstr /R ""^ Directory of | File\(s\) "" > Q_29065834.txt", 0, True
Set oFS = CreateObject("scripting.filesystemobject")
Set oTS = oFS.OpenTextFile(parmTopDir & "\Q_29065834.txt", ForReading, True, TristateFalse)
strData = oTS.readall
oTS.Close
oFS.deletefile parmTopDir & "\Q_29065834.txt"
Set oRE = CreateObject("vbscript.regexp")
oRE.Global = True
'remove totals at the end
oRE.Pattern = "( +(\d+) File\(s\) +(\d[^ ]*) bytes)\r\n +(\d+) File\(s\) +(\d[^ ]*) bytes\s+$"
If oRE.test(strData) Then
strData = oRE.Replace(strData, "$1")
End If
oRE.Pattern = "(?:\n|^) Directory of (\S[^\r]+\S)\r\n(?:.|\n)+?(?= +\d+ File\(s\)) +(\d+) File\(s\) +(\d[^ ]*) bytes"
If oRE.test(strData) Then
Set oMatches = oRE.Execute(strData)
strData = vbNullString
ReDim vResult(0 To oMatches.Count - 1, 0 To oMatches(0).submatches.Count - 1)
For lngMatch = 0 To oMatches.Count - 1
With oMatches(lngMatch)
For lngSM = 0 To .submatches.Count - 1
vResult(lngMatch, lngSM) = .submatches(lngSM)
Next
End With
Next
End If
Q_29065834a = vResult
End Function
if you add a line of codeWhat I've posted is a function where you pass the top level directory (string). Prompting the user is what you do OUTSIDE of this function, using the Application.Dialogs().Show
but the actual DIR output exceeded the excel row limit
Open in new window
/gustav