Microsoft KB Archive/322717

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

Article ID: 322717

Article Last Modified on 12/12/2003

-

APPLIES TO

 Microsoft JScript 3.0, when used with:  Microsoft Visual Basic 6.0 Enterprise Edition

 Microsoft Visual Basic 6.0 Professional Edition  Microsoft JScript 4.0, when used with:  Microsoft Visual Basic 6.0 Enterprise Edition

 Microsoft Visual Basic 6.0 Professional Edition</li></ul> </li> Microsoft JScript 5.0, when used with:  Microsoft Visual Basic 6.0 Enterprise Edition</li></ul>

 Microsoft Visual Basic 6.0 Professional Edition</li></ul> </li> Microsoft JScript 5.5, when used with:  Microsoft Visual Basic 6.0 Enterprise Edition</li></ul>

 Microsoft Visual Basic 6.0 Professional Edition</li></ul> </li> Microsoft Visual Basic, Scripting Edition 3.0, when used with:  Microsoft Visual Basic 6.0 Enterprise Edition</li></ul>

 <li>Microsoft Visual Basic 6.0 Professional Edition</li></ul> </li> <li>Microsoft Visual Basic, Scripting Edition 4.0, when used with: <ul> <li>Microsoft Visual Basic 6.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual Basic 6.0 Professional Edition</li></ul> </li> <li>Microsoft Visual Basic, Scripting Edition 5.0, when used with: <ul> <li>Microsoft Visual Basic 6.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual Basic 6.0 Professional Edition</li></ul> </li> <li>Microsoft Visual Basic, Scripting Edition 5.5, when used with: <ul> <li>Microsoft Visual Basic 6.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual Basic 6.0 Professional Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q322717

<div class="symptoms_section">

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 (&quot;C:&quot;) 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(&quot;C:\&quot;) 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.

<div class="cause_section">

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 &quot;FolderPath\*&quot; to fail on the root folder because the resulting string is &quot;C:\\*&quot;. 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 &quot;Symptoms&quot; 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.

<div class="resolution_section">

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

<div class="status_section">

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

<div class="moreinformation_section">

Steps to Reproduce the Behavior
<ol> <li>Create a new Standard EXE project in Visual Basic 6.0.</li> <li>On the Project menu, click References, and then click to select the Microsoft Scripting Runtime check box.</li> <li>Add two CommandButton controls and one TextBox control to the form.</li> <li> 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(&quot;C:\&quot;) 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(&quot;C:\&quot;) 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 </li> <li>To compile the project, click Make Project1.exe on the File menu.</li> <li>Run the Project1.exe file on a computer that is running Windows 95, Windows 98, or Windows Millennium Edition.</li> <li>Click Command1. Notice that you receive the error message that is listed in the &quot;Symptoms&quot; section of this article.</li> <li>Click Command2. Notice that the text box displays the size of drive C.</li></ol>

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, &quot;C:\&quot;). All of the other folders are returned without a trailing slash (for example, &quot;C:\Temp&quot;). 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) = &quot;\&quot; 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.

Additional query words: error 76 path found FSO

Keywords: kbbug kbpending KB322717

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.