Forum Discussion
adnan2004
Aug 21, 2022Copper Contributor
dir function
when we get any additional file name in same pathname,why we call Dir again with no arguments
- Aug 21, 2022
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
HansVogelaar
Aug 21, 2022MVP
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- adnan2004Aug 22, 2022Copper Contributorthanks