gstylianou
Registered User.
- Local time
- Today, 09:26
- Joined
- Dec 16, 2013
- Messages
- 359
Hi,
I found the following code here in the our forum (special thanks to the writer thedbguy) buti'm trying to modify them in order to fill the Listbox only with file name without the path. Following is the code and i will be glad if someone can explain to me how can i do it.
Public Function ListFilesT(FolderPath As String, Optional FileSearch As String, Optional FileExt As String) As String
'3/25/2018
'thedbguy@gmail.com
'optional valid file extensions should include the dot and be separated by semicolons (e.g. .txt;.docx;.xlsx;.accdb)
On Error GoTo ErrHandler
Dim fso As Object
Dim fsoFolder As Object
Dim fsoFile As Object
Dim arrExtensions() As String
Dim var As Variant
Dim x As Long
Dim strFiles As String
'assign valid file extensions to an array
If Not IsMissing(FileExt) Then
arrExtensions = Split(FileExt, ";")
End If
Set fso = CreateObject("Scripting.FileSystemObject")
'check for a valid path
If fso.folderexists(FolderPath) Then
Set fsoFolder = fso.getfolder(FolderPath)
'process subfolders
If fsoFolder.subfolders.Count > 0 Then
For Each var In fsoFolder.subfolders
'recursion
strFiles = ";" & ListFilesT(var.Path, FileSearch, FileExt) & strFiles
Next var
End If
'list files
If fsoFolder.files.Count > 0 Then
For Each var In fsoFolder.files
If IsMissing(FileExt) Or FileExt = "" Then
'check for search keyword, if supplied
If FileSearch = "" Then
' strFiles = ";" & var.Path & strFiles
strFiles = ";" & fso.GetFileName(var.Path) & strFiles ' i tried with this without result..
ElseIf InStr(var.Name, FileSearch) > 0 Then
' strFiles = ";" & var.Path & strFiles
strFiles = ";" & fso.GetFileName(var.Path) & strFiles
End If
Else
'check for valid file extensions, if supplied
For x = LBound(arrExtensions) To UBound(arrExtensions)
'skip files without file extensions
If InStr(var.Name, ".") > 0 Then
If Mid$(var.Name, InStrRev(var.Name, ".")) = arrExtensions(x) Then
'check for search keyword, if supplied
If FileSearch = "" Then
' strFiles = ";" & var.Path & strFiles
strFiles = ";" & fso.GetFileName(var.Path) & strFiles
ElseIf InStr(var.Name, FileSearch) > 0 Then
' strFiles = ";" & var.Path & strFiles
strFiles = ";" & fso.GetFileName(var.Path) & strFiles
End If
End If
End If
Next x
End If
Next var
End If
Else
'bad folder name
MsgBox "Folder does not exist.", vbInformation, "Invalid"
End If
'cleanup list (try to remove extra semicolons
If Right$(strFiles, 1) = ";" Then strFiles = Left$(strFiles, Len(strFiles) - 1)
'return the result
ListFilesT = CleanList(Mid$(strFiles, 2))
errExit:
Set fsoFolder = Nothing
Set fso = Nothing
Exit Function
ErrHandler:
MsgBox Err.Number & ". " & Err.Description
Resume errExit
Resume
End Function
Please note:
The above code is working and giving the result e.g C:\Program Files (x86)\NutriSoft\Common\Templates\Nutrition\MyWordFile.docx but i want only to show each file name e.g MyWordFile.docx
I found the following code here in the our forum (special thanks to the writer thedbguy) buti'm trying to modify them in order to fill the Listbox only with file name without the path. Following is the code and i will be glad if someone can explain to me how can i do it.
Public Function ListFilesT(FolderPath As String, Optional FileSearch As String, Optional FileExt As String) As String
'3/25/2018
'thedbguy@gmail.com
'optional valid file extensions should include the dot and be separated by semicolons (e.g. .txt;.docx;.xlsx;.accdb)
On Error GoTo ErrHandler
Dim fso As Object
Dim fsoFolder As Object
Dim fsoFile As Object
Dim arrExtensions() As String
Dim var As Variant
Dim x As Long
Dim strFiles As String
'assign valid file extensions to an array
If Not IsMissing(FileExt) Then
arrExtensions = Split(FileExt, ";")
End If
Set fso = CreateObject("Scripting.FileSystemObject")
'check for a valid path
If fso.folderexists(FolderPath) Then
Set fsoFolder = fso.getfolder(FolderPath)
'process subfolders
If fsoFolder.subfolders.Count > 0 Then
For Each var In fsoFolder.subfolders
'recursion
strFiles = ";" & ListFilesT(var.Path, FileSearch, FileExt) & strFiles
Next var
End If
'list files
If fsoFolder.files.Count > 0 Then
For Each var In fsoFolder.files
If IsMissing(FileExt) Or FileExt = "" Then
'check for search keyword, if supplied
If FileSearch = "" Then
' strFiles = ";" & var.Path & strFiles
strFiles = ";" & fso.GetFileName(var.Path) & strFiles ' i tried with this without result..
ElseIf InStr(var.Name, FileSearch) > 0 Then
' strFiles = ";" & var.Path & strFiles
strFiles = ";" & fso.GetFileName(var.Path) & strFiles
End If
Else
'check for valid file extensions, if supplied
For x = LBound(arrExtensions) To UBound(arrExtensions)
'skip files without file extensions
If InStr(var.Name, ".") > 0 Then
If Mid$(var.Name, InStrRev(var.Name, ".")) = arrExtensions(x) Then
'check for search keyword, if supplied
If FileSearch = "" Then
' strFiles = ";" & var.Path & strFiles
strFiles = ";" & fso.GetFileName(var.Path) & strFiles
ElseIf InStr(var.Name, FileSearch) > 0 Then
' strFiles = ";" & var.Path & strFiles
strFiles = ";" & fso.GetFileName(var.Path) & strFiles
End If
End If
End If
Next x
End If
Next var
End If
Else
'bad folder name
MsgBox "Folder does not exist.", vbInformation, "Invalid"
End If
'cleanup list (try to remove extra semicolons
If Right$(strFiles, 1) = ";" Then strFiles = Left$(strFiles, Len(strFiles) - 1)
'return the result
ListFilesT = CleanList(Mid$(strFiles, 2))
errExit:
Set fsoFolder = Nothing
Set fso = Nothing
Exit Function
ErrHandler:
MsgBox Err.Number & ". " & Err.Description
Resume errExit
Resume
End Function
Please note:
The above code is working and giving the result e.g C:\Program Files (x86)\NutriSoft\Common\Templates\Nutrition\MyWordFile.docx but i want only to show each file name e.g MyWordFile.docx