DIR

DIR([pathname] [,attributes])

Returns the name of a file or directory matching a pattern or attribute (Variant / String).


pathname(Optional) The full path of a file or directory (String).
attributes(Optional) A vbFileAttribute constant specifying the file attributes to (Integer):
0 = vbNormal (default) (just File Names)
1 = vbReadOnly
2 = vbHidden
4 = vbSystem
8 = vbVolume (Macintosh only)
16 = vbDirectory (File Names and Folders)
64 = vbAlias (Macintosh only)

REMARKS
* This function supports wildcards (? and *).
* The "pathname" must be provided the first time this function is called, otherwise an error will occur.
* The "pathname" can include a directory and drive.
* If "pathname" cannot be found, then a zero length string ("") is returned.
* The "attribute" can be a constant or a numerical expression.
* If "attributes" is left blank, then 0 is used. These are files that match "pathname" but have no attributes.
* To iterate over all the files in a folder, specify an empty string for the "pathname".
* To get additional file names, call this function again with no arguments.
* Any subsequent calls must specify a "pathname" otherwise an error will occur.
* When no more files exist an empty string is returned.
* File names are not necessarily returned in any particular order so it may be worth adding them to an array and sorting them before you display them.
* The vbAlias and vbVolumn attributes are only available on the Macintosh.
* You cannot call this function recursively to display sub folders.
* If "attribute" > 256, then it is assumed to be a MacID value.
* You can use the DIR$ function if you want to return a String data type instead of a Variant data type.
* You can use the MKDIR function to create new directories.
* You can use the GETATTR function to return the attributes of a file or directory.
* You can use the SETATTR statement to define the attributes of a file or directory.
* If you are using SharePoint then you must use forward slashes instead of backward slashes between the subfolders.
* The equivalent .NET function is [[Microsoft.VisualBasic.FileSystem.Dir]]
* For more information, refer to the Files & Directories > Code Snippets pages.
* For the Microsoft documentation refer to learn.microsoft.com

' save one file in the folder "C:\temp\" and return the filename
' using this function once returns the first name
Dim sFileName as String
sFileName = Dir("C:\temp\test.ini")
Debug.Print sFileName 'test.ini
sFileName = Dir("C:\temp\*ini") 'using a wildcard
Debug.Print sFileName 'test.ini
sFileName = Dir("C:\temp\missing.ini") 'file does not exist
Debug.Print sFileName '"" empty string

' save three files in the folder "C:\temp\"
' to get the names of the second and third files you need to call the function again
Dim sFileName As String
sFileName = Dir("C:\temp\")
Do While (sFileName <> "")
   Debug.Print sFileName
   sFileName = Dir()
Loop

Dir("//sharepoint-site\folder\subfolder")

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top