SOLVED

dir function

Copper Contributor

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

2 Replies
best response confirmed by adnan2004 (Copper Contributor)
Solution

@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
thanks
1 best response

Accepted Solutions
best response confirmed by adnan2004 (Copper Contributor)
Solution

@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

View solution in original post