Visual Basic Concepts

Working with Drives and Folders

See Also

With the FSO object model you can work with drives and folders programmatically just as you can in the Windows Explorer interactively. You can copy and move folders, get information about drives and folders, and so forth.

Getting Information About Drives

The Drive object allows you to gain information about the various drives attached to a system, either physically or over a network. Its properties allow you to obtain information about:

Example Usage of the Drive Object

The example below shows how to use the Drive object to gather information about a drive. Remember that you won't see a reference to an actual Drive object in the following code; rather, you use the GetDrive method to get a reference to an existing Drive object (in this case, drv):

Private Sub Command3_Click()
   Dim fso As New FileSystemObject, drv As Drive, s As String
   Set drv = fso.GetDrive(fso.GetDriveName("c:"))
   s = "Drive " & UCase("c:") & " - "
   s = s & drv.VolumeName & vbCrLf
   s = s & "Total Space: " & FormatNumber(drv.TotalSize / 1024, 0)
   s = s & " Kb" & vbCrLf
   s = s & "Free Space: " & FormatNumber(drv.FreeSpace / 1024, 0)
   s = s & " Kb" & vbCrLf
   MsgBox s
End Sub

Using CurDir, ChDrive, ChDir, or App.Path

If you use the CurDir function, the ChDrive and ChDir statements, or the Path property (App.Path), be aware that they may return a UNC path (that is, \\Server\Share…) rather than a drive path (such as E:\Folder), depending on how you run your program or project.

App.Path returns a UNC path:

ChDrive cannot handle UNC paths, and thus raises an error when App.Path returns one. You can handle this error by adding On Error Resume Next before the ChDrive statement, or by testing the first two characters of App.Path to see if they are backslashes:

On Error Resume Next
ChDrive App.Path
ChDir App.Path

This modification handles all cases in which the program is started from Windows using a UNC path (for example, in the Run dialog accessed from the Start menu), because Windows sets the current directory to a UNC path. ChDir handles changes between UNC paths correctly. (The failure of ChDrive can be ignored, because there is no drive letter for a UNC path.)

However, the above code above won't work if you run the program by entering a UNC path at MS-DOS command prompt. This is because the command prompt always has a drive path for the current directory, so CurDir is set to a drive path. ChDir does not raise an error, but it fails to change the directory from a drive path to a UNC path. The only workaround for this situation is to locate a local drive that's mapped to the share specified in the UNC path, or to use network commands to create such a mapping.

If the project is loaded into the Visual Basic IDE from a network share – either a UNC path or a mapped drive path – then App.Path returns a UNC path when the project is run and ChDrive fails and raises an error. ChDir doesn't raise an error, but the directory is not changed. The only workaround is to manually set the drive and directory:

Const PROJECTSHARE = "E:\VBPROJ\MYPROJECT"
#Const Debug = True
#If Debug Then
    ChDrive PROJECTSHARE
    ChDir PROJECTSHARE
#Else
    On Error Resume Next
    ChDrive App.Path
    ChDir App.Path
#End If

If more than one person might open the project on the network share, a DOS environment variable can be used to allow each person to have their own mapping for the share:

#Const Debug = True
#If Debug Then
    ChDrive Environ("MYPROJECTDIR")
    ChDir Environ("MYPROJECTDIR")
#Else
    On Error Resume Next
    ChDrive App.Path
    ChDir App.Path
#End If

The value of MYPROJECTDIR specifies the mapped drive letter and the path, for example:

SET MYPROJECTDIR=M:\VBProj\MyProject

Working with Folders

This list shows common folder tasks and the methods for doing them:

Task Method
Create a folder FileSystemObject.CreateFolder
Delete a folder Folder.Delete or
FileSystemObject.DeleteFolder
Move a folder Folder.Move or
FileSystemObject.MoveFolder
Copy a folder Folder.Copy or
FileSystemObject.CopyFolder
Retrieve the name of a folder Folder.Name
Find out if a folder exists on a drive FileSystemObject.FolderExists
Get an instance of an existing Folder object FileSystemObject.GetFolder
Find out the name of a folder's parent folder FileSystemObject.GetParentFolderName
Find out the path of system folders FileSystemObject.GetSpecialFolder

Example

This example demonstrates usage of the Folder and FileSystemObject objects to manipulate folders and gain information about them:

Private Sub Command10_Click()
   ' Get instance of FileSystemObject.
   Dim fso As New FileSystemObject, fldr As Folder, s As String
   ' Get Drive object.
   Set fldr = fso.GetFolder("c:")
   ' Print parent folder name.
   Debug.Print "Parent folder name is: " & fldr
   ' Print drive name.
   Debug.Print "Contained on drive " & fldr.Drive
   ' Print root file name.
   If fldr.IsRootFolder = True Then
      Debug.Print "This folder is a root folder."
   Else
      Debug.Print "This folder isn't a root folder."
   End If
   ' Create a new folder with the FileSystemObject object.
   fso.CreateFolder ("c:\Bogus")
   Debug.Print "Created folder C:\Bogus"
   ' Print the base name of the folder.
   Debug.Print "Basename = " & fso.GetBaseName("c:\bogus")
   ' Get rid of the newly-created folder.
   fso.DeleteFolder ("c:\Bogus")
   Debug.Print "Deleted folder C:\Bogus"
End Sub