簡體   English   中英

如何跳過我的vbscript無權訪問的文件夾?

[英]How do I skip folders which my vbscript doesn't have permission to access?

我正在編寫一個vbscript來列出我系統其中一個驅動器上的所有目錄(文件夾)以及它們是否為空或不是excel文件。 當我傳遞驅動器的文件夾位置時它會成功,但是當我傳入整個驅動器位置時,它會顯示“權限被拒絕!代碼-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行應該授予代碼提升的權限/特權,但這似乎也沒有幫助。

非常感謝@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM