2016-08-05 60 views
1

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

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行应该授予码提升的权限/特权,但似乎并没有帮助的。

+0

你的第一个5个liness给你的脚本[管理员权限](https://msdn.microsoft.com/en-US/library/windows/desktop/ms717801(V = vs.85)的.aspx)。要访问“系统卷信息”,您需要[SYSTEM](https://msdn.microsoft.com/en-us/library/windows/desktop/ms684190(v = vs.85).aspx)。一种简单而快速的方式来跳过它们是'On Error Resume Next'。授予访问权将意味着拥有所有权......您不想这样做。 – Clijsters

回答

0

非常感谢你@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 
+0

感谢您提供答案!只是你知道,答案不是针对与评论者的其他问题或对话。如果您想提请某位评论者注意,请在评论中回复他们。 – halfer