简体   繁体   English

如何跳过我的vbscript无权访问的文件夹?

[英]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. 我正在编写一个vbscript来列出我系统其中一个驱动器上的所有目录(文件夹)以及它们是否为空或不是excel文件。 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". 当我传递驱动器的文件夹位置时它会成功,但是当我传入整个驱动器位置时,它会显示“权限被拒绝!代码-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. 前5行应该授予代码提升的权限/特权,但这似乎也没有帮助。

Thank you so much @Clijsters for the comment. 非常感谢@Clijsters的评论。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得提供的VBScript复制文件夹的文件? - How do I get the supplied VBScript to copy files for my folders? vbscript跳过将在递归调用中引发权限被拒绝错误的文件夹 - vbscript skip folders that will throw Permission Denied Error in recursive call 使用VBScript移动项目时如何跳过打开的文件夹(和跳过打开的文件)? - How to skip open folders (and skip open files) when moving items using VBScript? 在VBScript中,如何为多个文件夹/文件管理objFSO和objFolder之类的“ Scripting.FileSystemObjects”? - In VBScript, how do I manage “Scripting.FileSystemObjects” like objFSO and objFolder for multiple folders/files? 如何使用vbscript访问itextsharp方法? - How do I access itextsharp methods using vbscript? VBScript:如何调用在Dispatch Map中用元音定义的函数 - VBScript: How do I invoke a function which is defined with a vowel in the Dispatch Map 为什么我的VBScript字符串连接无效? - Why doesn't my VBScript string concatenation work? 如何在VBScript中切换OnBase当前文档以从文档句柄列表访问每个文档? - How do I switch OnBase Current Document in VBScript to access each document from a list of Document Handles? 如何创建VBScript多行数组? - How do I create a VBScript multiline array? 如何在VBScript中重置StdOut流 - How do I reset the stream of StdOut in VBScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM