【问题标题】:Overcome Permissions Errors When Traversing the File System克服遍历文件系统时的权限错误
【发布时间】:2015-10-18 06:52:35
【问题描述】:

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

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

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

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

我尝试通过将此 sn-p 添加到开头而不改变行为来以提升的权限运行脚本:

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

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

【问题讨论】:

    标签: recursion vbscript permissions filesystems file-permissions


    【解决方案1】:

    出现错误的文件夹可能不是实际的文件夹,而是文件夹的符号链接。它们出于兼容性原因而存在,并且在其上有一个 ACE“每个人拒绝列表文件夹,仅此文件夹”以防止人们浏览。不要篡改它们。制作排除列表以防止您的脚本尝试遍历它们。

    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
    

    【讨论】:

    • 啊。这就说得通了。谢谢你。编辑:不小心发布得太早了。那么如何遍历这些文件夹的内容呢?我可以以某种方式询问他们的实际路径并使用它吗?知道我收到错误的原因很有价值,但这并不能帮助我执行我正在尝试的任务。
    • @th3ophilos 如果必须,您可以使用junction 获取目标路径(fsutil reparsepoint query 将失败并出现“拒绝访问”错误)。但是,所有这些符号链接都指向您已经遍历的其他文件夹。忽略它们。
    • 也许我太密集了。例如,如果我排除“桌面”,我的脚本将如何查看这些文件?我已经排除了导致这些错误的所有内容,据我所知,脚本完全忽略了这些文件的内容。
    • @th3ophilos 像Application Data 这样的符号链接指向其他可访问的文件夹(AppData\RoamingApplication Data 的情况下),并且目标应该位于同一文件系统中。但是,Desktop 通常不是符号链接。如果您有一个非标准环境,则需要提供有关给您错误的文件夹以及它们给您带来的实际错误的更多信息。
    • 据我所知,我使用的是标准环境。我必须排除的文件夹如下列表,(每个都会产生相同的权限错误):AppData、Application Data、Cookies、Local Settings、NetHood、PrintHood、Recent、SendTo、开始菜单、模板、文档、收藏夹、默认用户、桌面、我的音乐、我的图片、我的文档、我的视频
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 2014-08-28
    • 2015-04-16
    • 2023-03-16
    • 2018-07-24
    • 1970-01-01
    相关资源
    最近更新 更多