BUG: File System Object Size Method Fails on Root Folder on Windows 95, Windows 98, and Windows Millennium Edition (322717)



The information in this article applies to:

  • Microsoft JScript 3.0, when used with:
    • Microsoft Visual Basic Enterprise Edition for Windows 6.0
    • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft JScript 4.0, when used with:
    • Microsoft Visual Basic Enterprise Edition for Windows 6.0
    • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft JScript 5.0, when used with:
    • Microsoft Visual Basic Enterprise Edition for Windows 6.0
    • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft JScript 5.5, when used with:
    • Microsoft Visual Basic Enterprise Edition for Windows 6.0
    • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic, Scripting Edition 3.0, when used with:
    • Microsoft Visual Basic Enterprise Edition for Windows 6.0
    • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic, Scripting Edition 4.0, when used with:
    • Microsoft Visual Basic Enterprise Edition for Windows 6.0
    • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic, Scripting Edition 5.0, when used with:
    • Microsoft Visual Basic Enterprise Edition for Windows 6.0
    • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic, Scripting Edition 5.5, when used with:
    • Microsoft Visual Basic Enterprise Edition for Windows 6.0
    • Microsoft Visual Basic Professional Edition for Windows 6.0

This article was previously published under Q322717

SYMPTOMS

If you use the File System Object to determine the size of a folder, you receive the following error message when you call the Size method for a root folder ("C:") on a computer that is running Microsoft Windows 95, Microsoft Windows 98, or Microsoft Windows Millennium Edition (Me):
Runtime Error 76
Path Not Found
The code that causes this error appears similar to the following code:
Dim fso As FileSystemObject
Set fso = New FileSystemObject
Dim f As Folder
Set f = fso.GetFolder("C:\")
Dim amnt As Double
' This line fails with the error.
amnt = f.Size
				
This same code works as expected on a computer that is running Microsoft Windows NT, Microsoft Windows 2000, or Microsoft Windows XP.

CAUSE

When you use the name of the folder (the FolderPath property) in the code for the Size function of the File System Object, the root folder has a trailing slash, and all of the other folders do not have a trailing slash. This causes logic such as "FolderPath\*" to fail on the root folder because the resulting string is "C:\\*". A string such as this should return all of the files and the folders in that folder. However, this string fails on a computer that is running Windows 95, Windows 98, or Windows Millennium Edition with the error message that is listed in the "Symptoms" section of this article. Windows NT, Windows 2000, and Windows XP just ignore the double slash and return the correct list of files and folders.

The code in the Size method should remove the slash from the FolderPath for the root folder before it performs operations.

RESOLUTION

To work around this problem, provide a custom Size method that handles the root folder. For example, use the following code instead of Folder.Size:
Function Size ( f As Folder ) As Double
  Dim amnt As Double
  If f.IsRootFolder = False Then
    amnt = f.Size
  Else
    Dim myFile As File
    For Each myFile In f.Files
       amnt = amnt + myFile.Size
    Next
    Dim myFolder As Folder
    For Each myFolder In f.SubFolders
       amnt = amnt + myFolder.Size
    Next
    amnt = amnt
  End If
  Size = amnt
End Function
				

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Standard EXE project in Visual Basic 6.0.
  2. On the Project menu, click References, and then click to select the Microsoft Scripting Runtime check box.
  3. Add two CommandButton controls and one TextBox control to the form.
  4. Add the following code to the form:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim fso As FileSystemObject
        Set fso = New FileSystemObject
        Dim f As Folder
        'Get a reference to the File object.
        Set f = fso.GetFolder("C:\")
        Text1.Text = f.Size
    End Sub
    
    Private Sub Command2_Click()
        Dim fso As FileSystemObject
        Set fso = New FileSystemObject
        Dim f As Folder
        'Get a reference to the File object.
        Set f = fso.GetFolder("C:\")
        Text1.Text = Size(f)
    End Sub
    
    Function Size ( f As Folder ) As Double
      Dim amnt As Double
      If f.IsRootFolder = False Then
        amnt = f.Size
      Else
        Dim myFile As File
        For Each myFile In f.Files
           amnt = amnt + myFile.Size
        Next
        Dim myFolder As Folder
        For Each myFolder In f.SubFolders
           amnt = amnt + myFolder.Size
        Next
      End If
      Size = amnt
    End Function
    					
  5. To compile the project, click Make Project1.exe on the File menu.
  6. Run the Project1.exe file on a computer that is running Windows 95, Windows 98, or Windows Millennium Edition.
  7. Click Command1. Notice that you receive the error message that is listed in the "Symptoms" section of this article.
  8. Click Command2. Notice that the text box displays the size of drive C.
NOTE: This same behavior occurs in any environment that uses the Microsoft Scripting Runtime.

General Advice

When you work with the root folder, the folder name is returned with a trailing slash (for example, "C:\"). All of the other folders are returned without a trailing slash (for example, "C:\Temp"). When programmatic logic expects that the folder name does not include a trailing slash, this logic can fail when it is run on the root folder (for example, as in the case of Folder.Size).

You must work around this problem when you create paths. If a path can be concatenated off of the root folder or off of another folder location, double slashes can appear when you use the same logic for both the root folder and the other folder paths.

The following code removes the slash from the end of a string if the slash is present (for example, for the root folder):
Function DirPath(ByRef Path As String) As String
    If  Right(Path, 1) = "\" Then
        DirPath = Mid(Path, 1, Len(Path) - 1)
    Else
        DirPath = Path
    End If
End Function
				
The problem is that you must add special handling to code that deals with the file system to account for the root folder. You may experience this problem in other scenarios, but they are all handled in a similar way.

Modification Type:MajorLast Reviewed:12/12/2003
Keywords:kbbug kbpending KB322717 kbAudDeveloper