Forum Discussion

adnan2004's avatar
adnan2004
Copper Contributor
Aug 21, 2022
Solved

dir function

when we get any additional file name in same pathname,why  we call Dir again with no arguments

  • adnan2004 

    You must always call Dir first with arguments. This will return the name of the first file (or folder) that matches the specification.

    Each time you call Dir without arguments after that, it will return the name of the next file (or folder) that matches the original specification.

    So you can use code like this:

        Const sFolder = "C:\Excel\"
        Dim sFile As String
        ' Get the name of the first Excel workbook in the folder
        sFile = Dir(sFolder & "*.xlsx")
        ' Loop through the workbooks until none is found
        Do While sFile <> ""
            ' Do something with the file name
            MsgBox "Found file: " & sFile
            ' Get the next file name
            sFile = Dir
        Loop
  • adnan2004 

    You must always call Dir first with arguments. This will return the name of the first file (or folder) that matches the specification.

    Each time you call Dir without arguments after that, it will return the name of the next file (or folder) that matches the original specification.

    So you can use code like this:

        Const sFolder = "C:\Excel\"
        Dim sFile As String
        ' Get the name of the first Excel workbook in the folder
        sFile = Dir(sFolder & "*.xlsx")
        ' Loop through the workbooks until none is found
        Do While sFile <> ""
            ' Do something with the file name
            MsgBox "Found file: " & sFile
            ' Get the next file name
            sFile = Dir
        Loop

Resources