遍历文件系统时克服权限错误

Overcome Permissions Errors When Traversing the File System

我正在构建一个 VBScript 来搜索和记录从主目录中复制出的文件的位置(重复项)。

我目前有一个脚本可以递归搜索C盘并将所有文件位置记录到日志文件中。 (这并不优雅,但我仍在进行概念验证。)

然而,当我遍历文件系统时,我发现有很多文件夹脚本甚至无法查看 - Appdata、本地设置、我的视频、我的图片等。

所以这些显然是用户无法访问的,但用户对“我的文档”中的文件夹拥有所有权,所以我无法确定为什么我的脚本甚至无法读取它们的内容。用户是本地管理员。

我已经尝试 运行 通过将此代码段添加到开头且行为没有变化来提升权限的脚本:

If Not WScript.Arguments.Named.Exists("elevate") Then
    CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , WScript.ScriptFullName & " /elevate", "", "runas", 1
    WScript.Quit
End If

这是脚本中的一个重要函数,其中标明了出错的行(请让我知道其他部分是否有帮助):

' Get list of ALL files recursively and record them in a text file
Function getAllFilesRecursively (specifiedFolder, logLocation)

    If (Right(specifiedFolder,7)<>"AppData") And _
       (Right(specifiedFolder,16)<>"Application Data") And _
       (Right(specifiedFolder,7)<>"Cookies") And _
       (Right(specifiedFolder,14)<>"Local Settings") Then

        ' Get list of files in current folder
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolder = objFSO.GetFolder(specifiedFolder.Path)
        Set colFiles = objFolder.Files

        'This function writes to a specified log file, using the specified method
        writeToFile specifiedFolder, logLocationTemp, "Append" 

        ' For each file, perform a task 
        For Each objFile in colFiles '(<<<<<<<<<<<<<<<<<<<< permissions error)
            ' Compose the full path to the file
            fullPath = specifiedFolder & "\" & objFile.name

            'Save the path to a text file (a newline is automatically added)
            writeToFile fullPath, logLocation, "Append"
        Next

        ' For each folder, Recurse
        For Each Subfolder in specifiedFolder.SubFolders
            getAllFilesRecursively Subfolder, logLocation
        Next
    End If
End Function

有什么方法可以让这个脚本访问这些文件夹吗?计算机在域中,但我可以进行任何必要的修改(甚至策略)。

给您错误的文件夹可能不是实际文件夹,而是指向文件夹的符号链接。它们的存在是出于兼容性原因,并带有 ACE "everyone deny list folder, this folder only" 以防止人们浏览。不要乱动它们。制作排除列表以防止您的脚本尝试遍历它们。

Set exclude = CreateObject("Scripting.Dictionary")
exclude.CompareMode = vbTextCompare
exclude.Add "Application Data", True
exclude.Add "Local Settings", True
...

Function getAllFilesRecursively (specifiedFolder, logLocation)
    ...
    For Each Subfolder in specifiedFolder.SubFolders
        If Not exclude.Exists(Subfolder.Name) Then
            getAllFilesRecursively Subfolder, logLocation
        End If
    Next
End Function