VBS 脚本'Path not found'设置文件系统文件夹对象引用时出错



我正在编写一个脚本来确定每个登录到Windows 2003服务器的用户的配置文件文件夹中特定子文件夹的所有实例的组合大小,例如所有用户的桌面文件夹或所有用户的本地设置文件夹。

Option Explicit
Dim colSubfolders, intCount, intCombinedSize, objFolder2, objFSO1, objFSO2, objUserFolder, strOutput, objSearchFolder, objSubfolder, strSearchFolder, strSubfolderPath
intCount = 0
intCombinedSize = 0
strSearchFolder = "C:Documents and Settings"
Set objFSO1 = CreateObject("Scripting.FileSystemObject")
Set objSearchFolder = objFSO1.GetFolder(strSearchFolder)
Set colSubfolders = objSearchFolder.SubFolders
For Each objUserFolder in colSubfolders
  strSubfolderPath = objUserFolder.Path & "Desktop"
  Set objFSO2 = CreateObject("Scripting.FileSystemObject")
  Set objSubfolder = objFSO2.GetFolder(strSubfolderPath)
  intCount = intCount + 1
  intCombinedSize = intCombinedSize + objSubfolder.Size
Next
MsgBox "Combined size of " & CStr(intCount) & " folders: " & CStr(intCombinedSize / 1048576) & " MB"

此代码在第15行抛出'Path not found'错误(代码800A004C):

Set objSubfolder = objFSO2.GetFolder(strSubfolderPath)

如果我打印出strSubfolderPath,然而,我发现所有返回的字符串都是有效的目录路径,所以我不明白为什么我得到这个错误。

我尝试过在路径末尾使用和不使用反斜杠,我也尝试过在8.3样式路径中删除空格,但没有效果。

当我运行你的代码时,我得到同样的错误。

进一步检查,在我的计算机上有一个名为C:Documents and Settingsmachinename的文件夹,其中machinename是我的计算机名称。此文件夹只包含一个名为ASPNet的子文件夹。

我猜你们也有类似的。

为了尽量减少多个反斜杠的混淆,请始终使用FileSystemObject方法,而不是依赖字符串连接:

strSubfolderPath = objFSO1.BuildPath(objUserFolder.Path,"Desktop")