【问题标题】:How do I skip folders which my vbscript doesn't have permission to access?如何跳过我的 vbscript 无权访问的文件夹?
【发布时间】:2016-08-05 12:10:44
【问题描述】:

我正在编写一个 vbscript 来列出我系统的一个驱动器上的所有目录(文件夹)以及它们是否为空到一个 excel 文件中。当我传递驱动器的文件夹位置时,它会成功,但是当我传递整个驱动器位置时,它会显示“权限被拒绝!code-800A0046”。这是由于存在一些需要访问权限的隐藏文件夹,例如系统卷信息等。我想跳过所有此类文件夹或找到访问此类文件夹的方法。我如何实现这一目标? 下面是我的脚本:

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , WScript.ScriptFullName & " /elevate", "", "runas", 1
  WScript.Quit
End If
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Add
objExcel.Visible = True
intRow = 1
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each objFolder In FSO.GetFolder("C:\").SubFolders
    if ((objFolder.Attributes = 0) OR (objFolder.Attributes AND 1)) then
        ShowSubFolders objFolder
    End If
Next

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
    if ((Subfolder.Attributes = 0) OR (Subfolder.Attributes AND 1)) then
        If Subfolder.Size = 0 Then
            objExcel.Cells(intRow,1) = SubFolder.Path
            objExcel.Cells(intRow,2) = "Empty"
            intRow = intRow + 1
        Else
            objExcel.Cells(intRow,1) = SubFolder.Path
            objExcel.Cells(intRow,2) = "Not Empty"
            intRow = intRow + 1
            End If
    End If
    Next
End Sub
Set FSO = nothing

前 5 行应该授予代码提升的权限/特权,但这似乎也无济于事。

【问题讨论】:

  • 你的前 5 行给你的脚本Administrator Privileges。要访问System Volume Information,您需要是SYSTEM。跳过它们的一种简单快捷的方法是“On Error Resume Next”。授予访问权限意味着获得所有权......您不想这样做。

标签: vbscript directory permission-denied


【解决方案1】:

非常感谢@Clijsters 的评论。真的很有帮助。

On Error Resume Next

显然,这确实是我想要的。

我已经完成了我想做的事情(就这个问题而言)。以下是我的代码供将来参考:

On Error Resume Next
' Giving the script administrator privileges
If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , WScript.ScriptFullName & " /elevate", "", "runas", 1
  WScript.Quit
End If

'creating an excel application object
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Add
intRow = 1
objExcel.Cells(intRow,1) = "Folder Path"
objExcel.Cells(intRow,2) = "Empty or Not"
intRow = intRow + 2

Set FSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = FSO.Drives
For Each objDrive in colDrives
    For Each objFolder In FSO.GetFolder(objDrive.RootFolder).SubFolders
        ShowSubFolders objFolder
    Next
Next

'Function to determine whether a folder is Empty or not and enter its path in an excel
Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        If Subfolder.Size = 0 Then
            objExcel.Cells(intRow,1) = Subfolder.Path
            objExcel.Cells(intRow,2) = "Empty"
            intRow = intRow + 1
        Else
            objExcel.Cells(intRow,1) = Subfolder.Path
            objExcel.Cells(intRow,2) = "Not Empty"
            intRow = intRow + 1
        End If
    Next
End Sub
Set FSO = Nothing
objExcel.Activeworkbook.SaveAs("EmptyFolders.xlsx")
objExcel.Visible = True

【讨论】:

  • 感谢您提供答案!如您所知,答案不适用于其他问题或与评论者的对话。如果您想引起特定评论者的注意,请在 cmets 中回复他们。
猜你喜欢
  • 2013-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 2019-02-14
  • 1970-01-01
相关资源
最近更新 更多