简体   繁体   中英

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

I am writing a vbscript to list all directories (folders) on one of the drives of my system along with whether they are empty or not into an excel file. It does so successfully when I pass a folder location of a drive, but when I pass in the entire drive location, it says "permission denied! code-800A0046". This is due to the presence of some hidden folders like System Volume Information etc which require permissions to be accessed. I want to either skip all such folders or find a way to access such folders. How do I achieve this? Below is my script:

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

The first 5 lines are supposed to grant the code elevated rights/ privileges but that doesn't seem to help either.

Thank you so much @Clijsters for the comment. It really helped.

On Error Resume Next

is indeed what I was looking for, apparently.

I have completed what I wanted to do (as far as this question is concerned). Below is my code for future references:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM