Explains the basics of using the Dir() command to get file and folder information.
To Start
Things Off Right
|
| Today's
Topic:
|
Using the Dir
Command
|
| Name
Derived From
|
"Directory"
|
| Used
For:
|
Getting Information about a
particular folder or file.
|
| VB Help
File Description
|
Returns a String representing
the name of a file, directory, or folder that matches a specified pattern
or file attribute, or the volume label of a drive. Syntax
|
| Plain
English Description
|
Depending on the paramters set,
returns:
- The Name of a file in a folder that matches a
patten (*.txt)
- The name of a sub folder within a folder.
- The name of a hard drive.
|
| Usage:
|
To get a file name from a
directory: strFile = Dir
("c:\MyFolder\*.txt")
To get a read-only file name from a directory: strFile = Dir ("c:\MyFolder\*.txt",
vbReadOnly)
To get a sub-directory from a directory: strFile = Dir ("c:\MyFolder\*",
vbDirectory)
To get the label of a drive: strFile = Dir ("d:",
vbVolume)
|
| Parameters:
|
- Path - The root directory to search from.
- Attribute - One of the following VB attribute
values: vbNormal (default), vbReadOnly, vbHidden, VbSystem,
vbVolume, vbDirectory
|
| Copy
& Paste Code:
|
Today's copy and paste code lists
all of the files in a directory to the debug window. For details on usage
of the Dir ()command in this example, see the Notes below.
Dim strPathAndPattern As String Dim strFileName As String strPathAndPattern = InputBox("Enter a path and search pattern (ex: c:\windows\*.exe):") strFileName = Dir(strPathAndPattern) Debug.Print strFileName While strFileName > "" strFileName = Dir Debug.Print strFileName
Wend
|
| Notes
|
- IMPORTANT: To find multiple
files, you must do a "two step" proccess. In the example above, notice
that the first time Dir() is called (before the While...Wend loop), the
parameter strPathAndPatten is used. After that, the call is
simply Dir. (strFileName = Dir). This is sort of confusing if
you don't know what is going on. When I first used the Dir command, I
kept getting the same file name over and over. This is because using
Dir() with a path parameter will always return the FIRST match. To
get subsequent matches, you simply call Dir(). It remembers the last
pattern and passes the NEXT match. Really screwy.
- Dir can be used to see if a file already
exists:
If Dir
("c:\MyFolder\log.txt") > ""
Then
MsgBox "File Already
Exists!" End
If
This is because Dir() will return an empty string ("") if
a match is not found, but will return the file name if a match
is found.
- Dir can also be used to see if a folder exists, but
this is a little different. Because of the way Windows handles folder
names, there is always a folder named "." and another named ".." . As
odd as that seems, these names represent the current folder and the
parent folder. So to find out if a certain folder exists, you can do
this:
If Dir ("c:\windows\system",
vbDirectory) > ".." Then MsgBox "Folder
Already Exists!" End If
- For a downloadable project using the Dir command,
Click Here
|
|
|
|
|
|