
Sub CercaFiles()
Dim Fso As New FileSystemObject
Dim NomeFile As String
Dim strArr() As String
Dim I As Long
Dim Percorso As String
Dim Ricerca As String
Percorso = "C:documentiscansioniprova.pdf"
Ricerca = "*.*"
Columns("A:C").ClearContents
Range("A1") = Percorso
Range("B1") = Ricerca
NomeFile = Dir$(Percorso & "*." & Ricerca)
Do While NomeFile <> vbNullString
I = I + 1
ReDim Preserve strArr(1 To I)
strArr(I) = Percorso & "" & NomeFile
NomeFile = Dir$()
Loop
Call recurseSubFolders(Fso.GetFolder(Percorso), strArr(), I, Ricerca)
Set Fso = Nothing
If I > 0 Then
For I = 1 To UBound(strArr)
Cells(I + 2, 1) = strArr(I)
Next
End If
MsgBox UBound(strArr)
End Sub
Private Sub recurseSubFolders(ByRef Folder As Object, _
ByRef strArr() As String, _
ByRef I As Long, _
ByRef searchTerm As String)
Dim SubFolder As Folder
Dim strName As String
For Each SubFolder In Folder.SubFolders
strName = Dir$(SubFolder.Path & "*." & searchTerm)
Do While strName <> vbNullString
I = I + 1
ReDim Preserve strArr(1 To I)
strArr(I) = SubFolder.Path & "" & strName ' percorso e nome del file trovato
' strArr(I) = strName ' questa istruzione se si vuole il solo nome del file trovato
strName = Dir$()
Loop
Call recurseSubFolders(SubFolder, strArr(), I, searchTerm)
Next
End Sub |
.... If I > 0 Then For I = 1 To UBound(strArr) Cells(I + 2, 1) = Split(strArr(I), "") Next .... |
.... If I > 0 Then For I = 1 To UBound(strArr) Cells(I + 2, 1) = Split(strArr(I), "", 2) Next .... |
Sub CercaFiles()
Dim Fso As New FileSystemObject
Dim NomeFile As String
Dim strArr() As String
Dim I As Long
Dim y As Long
Dim colonna As Long
Dim Percorso As String
Dim Ricerca As String
Dim stringa() As String
Percorso = "I:ScansioniPaolaARCHIVIO ATTESTATI ALIMENTARISTI ! ! !"
Ricerca = "*.*"
Columns("A:C").ClearContents
Range("A1") = Percorso
Range("B1") = Ricerca
NomeFile = Dir$(Percorso & "*." & Ricerca)
Do While NomeFile <> vbNullString
I = I + 1
ReDim Preserve strArr(1 To I)
strArr(I) = Percorso & "" & NomeFile
NomeFile = Dir$()
Loop
Call recurseSubFolders(Fso.GetFolder(Percorso), strArr(), I, Ricerca)
Set Fso = Nothing
If I > 0 Then
For I = 1 To UBound(strArr)
stringa = Split(Right(strArr(I), Len(strArr(I)) - Len(Percorso)), "")
colonna = 1
For y = LBound(stringa) To UBound(stringa)
If stringa(y) <> "" Then
Cells(I + 2, colonna) = stringa(y)
colonna = colonna + 1
End If
Next
Next
End If
MsgBox UBound(strArr)
End Sub
Private Sub recurseSubFolders(ByRef Folder As Object, _
ByRef strArr() As String, _
ByRef I As Long, _
ByRef searchTerm As String)
Dim SubFolder As Folder
Dim strName As String
For Each SubFolder In Folder.SubFolders
strName = Dir$(SubFolder.Path & "*." & searchTerm)
Do While strName <> vbNullString
I = I + 1
ReDim Preserve strArr(1 To I)
strArr(I) = SubFolder.Path & "" & strName ' percorso e nome del file trovato
' strArr(I) = strName ' questa istruzione se si vuole il solo nome del file trovato
strName = Dir$()
Loop
Call recurseSubFolders(SubFolder, strArr(), I, searchTerm)
Next
End Sub |
Sub ListSubfoldersFiles()
Dim myDir As String, temp(), myList, myExtension As String
Dim Rtn As Integer, msg As String
myDir = "F:Documenti"
myExtension = "*.*"
myList = SearchFiles(myDir, myExtension, 0, temp())
If Not IsError(myList) Then
Cells(1).Resize(UBound(myList, 2), 2).Value = _
Application.Transpose(myList)
Else
MsgBox "No file found"
End If
End Sub
Private Function SearchFiles(myDir As String _
, myFileName As String, n As Long, myList()) As Variant
Dim fso As Object, myFolder As Object, myFile As Object
Set fso = CreateObject("Scripting.FileSystemObject")
For Each myFile In fso.getfolder(myDir).Files
Select Case myFile.Attributes
Case 2, 4, 6, 34
Case Else
If (Not myFile.Name Like "~$*") _
* (myFile.Path & "" & myFile.Name <> ThisWorkbook.FullName) _
* (UCase(myFile.Name) Like UCase("*" & myFileName)) Then
n = n + 1
ReDim Preserve myList(1 To 2, 1 To n)
myList(1, n) = myDir
myList(2, n) = myFile.Name
End If
End Select
Next
For Each myFolder In fso.getfolder(myDir).subfolders
SearchFiles = SearchFiles(myFolder.Path, myFileName, _
n, myList)
Next
SearchFiles = IIf(n > 0, myList, CVErr(xlErrRef))
End Function |
