P
pascalv
Does somebody know why, in some cases, these 2 programs return
different results (for finding the number of files of a folder)?
In both cases, the aim was to count *all* files
Thanks!
----- Idea 1: Use of Application.FileSearch
Public Sub test1()
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
With Application.FileSearch
.NewSearch
.RefreshScopes
.FileTypes.Add msoFileTypeAllFiles
.FileType = msoFileTypeAllFiles
.SearchSubFolders = True
.LookIn = "C:\users"
ActiveCell.Formula = .Execute(SortBy:=msoSortByLastModified, _
SortOrder:=msoSortOrderAscending, _
AlwaysAccurate:=True)
End With
Rem Of course Application.FileSearch.FoundFiles.Count
Rem does Not work either...
End Sub
----- End of idea 1
----- Idea 2: Use a recursive function
Public Sub test2()
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
ActiveCell.Formula = nbFiles("C:\users", fs)
End Sub
Private Function nbFiless(folderName As String, ByRef fs As Object)
Dim f As Object
nbFiles = fs.GetFolder(folderName).Files.Count
If Not (fs Is Nothing) Then
If fs.GetFolder(folderName).SubFolders.Count > 0 Then
For Each f In fs.GetFolder(folderName).SubFolders
nbFiles = nbFiles _
+ nbFiles(f.Path, fs)
Next
End If
End If
End Function
----- End of Idea 2
different results (for finding the number of files of a folder)?
In both cases, the aim was to count *all* files
Thanks!
----- Idea 1: Use of Application.FileSearch
Public Sub test1()
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
With Application.FileSearch
.NewSearch
.RefreshScopes
.FileTypes.Add msoFileTypeAllFiles
.FileType = msoFileTypeAllFiles
.SearchSubFolders = True
.LookIn = "C:\users"
ActiveCell.Formula = .Execute(SortBy:=msoSortByLastModified, _
SortOrder:=msoSortOrderAscending, _
AlwaysAccurate:=True)
End With
Rem Of course Application.FileSearch.FoundFiles.Count
Rem does Not work either...
End Sub
----- End of idea 1
----- Idea 2: Use a recursive function
Public Sub test2()
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
ActiveCell.Formula = nbFiles("C:\users", fs)
End Sub
Private Function nbFiless(folderName As String, ByRef fs As Object)
Dim f As Object
nbFiles = fs.GetFolder(folderName).Files.Count
If Not (fs Is Nothing) Then
If fs.GetFolder(folderName).SubFolders.Count > 0 Then
For Each f In fs.GetFolder(folderName).SubFolders
nbFiles = nbFiles _
+ nbFiles(f.Path, fs)
Next
End If
End If
End Function
----- End of Idea 2