【发布时间】: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