2016-08-05 42 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

相關問題