Friday, November 29, 2019

 

Option Explicit



Function GetCurrentDir() As String
    GetCurrentDir = ThisWorkbook.path  'this vbs source code owner workbook'path
End Function

Function GetDefaultDir() As String
    GetDefaultDir = Application.DefaultFilePath
End Function



'--------------------------------------------------------------
'CheckFileExists: check file exists o not
'path: file path
'      "C:\Users\Nikola\Desktop\VBA articles\Test File Exists.xlsx"
'--------------------------------------------------------------
Function CheckFileExists(path As String) As Boolean
    CheckFileExists = False
    If path = "" Then
        Exit Function
    End If
    
    Dim strExists As String
    strExists = Dir(path)
    If strExists <> "" Then
        CheckFileExists = True
    End If
End Function


'--------------------------------------------------------------
'CheckFolderExists: check folder exists o not
'path: folder path
'      "C:\Users\Nikola\Desktop\VBA articles\Test Folder\"
'--------------------------------------------------------------
Function CheckFolderExists(path As String)
    CheckFolderExists = False
    If path = "" Then
        Exit Function
    End If

    Dim strExists As String
    strExists = Dir(path, vbDirectory)
    If strExists <> "" Then
        CheckFolderExists = True
    End If
End Function



'--------------------------------------------------------------
'ScanFolder: Scan folder and get list files
'@para path: folder path
'       "C:\Users\Nikola\Desktop\VBA articles\Test Folder"
'       "C:\Users\Nikola\Desktop\VBA articles\Test Folder\"
'@para deep: scan deep level 0,1,2,3...
'          -1: scan all sub folder
'           0: do not scan sub folder
'   1,2,3,...: scan sub deep level 1,2,3,...
'@para fileExt: file extension (txt, xlsx, docs, ...)
'--------------------------------------------------------------
Sub ScanFolder(path As String, deep As Integer, fileExt As String, filelist() As Variant)
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Call LoopScanFolder(fso.GetFolder(path), filelist)
End Sub
Function LoopScanFolder(Folder As Object, filelist() As Variant) As Boolean
    Dim SubFolder As Object
    For Each SubFolder In Folder.SubFolders
'        Debug.Print SubFolder.path
        Call LoopScanFolder(SubFolder, filelist)
    Next
    Dim File  As Object
    For Each File In Folder.Files
        If Dir(File.path) <> "" Then 'check file exists or not
'            Debug.Print File.path
            Call UpdateListFiles(File.path, filelist)
        End If
    Next
End Function
Function UpdateListFiles(path As String, filelist() As Variant) As Integer 'return current number of file
    Dim arrayLen As Integer
    arrayLen = getArrayLen(filelist)
    ReDim Preserve filelist(arrayLen)
    filelist(arrayLen) = path
End Function




Sub test_scanFolder()
    Dim path As String
    path = ActiveWorkbook.path & "\"
    Dim list() As Variant
    Call ScanFolder(path, 1, "txt", list)
    
    Dim arrayLen As Integer
    arrayLen = getArrayLen(list)
    Debug.Print "arrayLen: " & arrayLen

    
    'list up
    Dim i As Integer
    For i = 0 To arrayLen - 1
        Debug.Print list(i)
    Next i
End Sub
Sub Main()
    

'    Debug.Print CheckFileExists("")
'    Debug.Print CheckFileExists("D:\VBA\dictionary.xlsm")
'
'    Debug.Print CheckFolderExists("")
'    Debug.Print CheckFolderExists("D:\VBA\")


    test_scanFolder
    

End Sub



Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Lập trình hệ thống nhúng Linux . Powered by Luong Duy Ninh -