首页 文章

VBScript查找文件夹和文件(以及子文件夹!)

提问于
浏览
0

我已经按照其他一些帖子写了一个vbscript,它会计算/列表/移动/目录中的所有文件夹或文件,我有一些工作,但它有2个限制 .

1)它不返回正确的数字(至少不匹配Windows资源管理器) . I.E.,我搜索我的C:\并返回433个文件夹,当windows explore停留在10,000(和一个子文件夹,C:\ Windows,返回2,234)!

2)即使我以管理员身份运行脚本,在访问各个位置的文件夹时也会出现权限错误 .

这是一个简单的代码,当我测试较小的文件夹时,即使有很多子文件夹,它也可以工作:

[Option Explicit
'on error resume next

Dim objFolder, objFSO, objSubFolder, iFolders
Set objFSO = CreateObject("Scripting.FileSystemObject")

iFolders = 0

Call CountFolders("C:\Windows")

Sub CountFolders(strPath)
    Set objFolder = objFSO.GetFolder(strPath)
        For Each objSubFolder In objFolder.SubFolders
                iFolders = iFolders + 1
                If Right(iFolders, 2) = "00" Then
                    IF MsgBox(iFolders & " folders found so far.", VBOKCancel) = 2 Then
                        Wscript.quit
                    End If
                End If
                Call CountFolders(objSubFolder.Path)
        Next
End Sub

msgbox(iFolders)]

1我是否只是推动文件系统对象可以做的限制,而不会遇到其他问题?

谢谢

1 回答

  • 0

    它必须是VBScript吗?微软已经放弃了VBScript,任何新的脚本应该用PowerShell完成 .

    在PowerShell中:

    $everything = get-childitem -path c:\ -recurse -force;
    $Foldercount = $everything|where-object{$_.psiscontainer}|measure-object |select-object -ExpandProperty count;
    $Filecount = $everything|where-object{-not $_.psiscontainer}|measure-object |select-object -ExpandProperty count;
    
    write-output "$Foldercount folders";
    write-output "$Filecount files";
    

    这是做什么的:

    • 收集C盘上的所有内容

    • 计算文件夹中的项目数

    • 计算没有文件夹的项目数

    • 打印结果

相关问题