【问题标题】:Script to delete files under each userprofile删除每个用户配置文件下的文件的脚本
【发布时间】:2017-03-20 14:20:51
【问题描述】:

我正在寻找一个脚本来运行以从每个用户配置文件 + 路径中删除文件,例如:userprofile\Appdata\Microsoft\Windows\WER\ReportQueue*

我试过了

Remove-Item "C:\users + \AppData\Local\Microsoft\Windows\WER\ReportQueue\AppCrash*"

不行。

也试过VBScript:

Set fso = CreateObject("Scripting.FileSystemObject")

strOneDrivePath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%USERPROFILE%") & "\AppData\Local\Microsoft\Windows\WER\ReportQueue\"
Search strOneDrivePath

Sub Search(str)
    Set folder = fso.GetFolder(str)
    For Each file In folder.Files
        If file.DateLastModified < (Now() - 3) Then
            file.Delete True
        End If
    Next

    For Each subFolder In folder.SubFolders
        Search subFolder.Path 
        If subFolder.Files.Count = 0  Then
            subFolder.Delete True
        End If
    Next
End Sub

【问题讨论】:

  • 欢迎来到 Stack Overflow!我们是一个帮助程序员和编程爱好者的社区。话虽如此,希望您在发布之前展示您所做或尝试过的事情。这给了我们一些可以建立的东西。到目前为止,这看起来像是一个代码编写请求,这与 SO 无关。将您的问题分解为各个部分,并单独搜索这些问题的解决方案。然后,如果您仍有问题,请edit 展示您的工作,以便我们更好地帮助您和社区。​​span>
  • 您尝试的命令会查找字面上为C:\users + \AppData\Local\Mic... 的路径。您需要枚举C:\Users 的子文件夹并将路径的其余部分加入这些基本路径。

标签: powershell user-profile


【解决方案1】:

您可以使用 Get-ChildItem-Directory 参数来获取 C:\Users 中的子目录,然后将它们的路径 ($_.FullName) 与您想要的子路径连接起来。

然后使用Test-PathRemove-Item 删除你想要的文件。

$path = "C:\Users"
$child_path = "AppData\Local\Microsoft\Windows\WER\ReportQueue"
$files_filter = "AppCrash*"

Get-ChildItem $path -Directory -Exclude Default*,Public | foreach {

    $joined_path = Join-Path -Path $_.FullName -ChildPath $child_path
    If (test-path $joined_path) {
        Remove-Item "$joined_path\$files_filter" -Force
    }
}

【讨论】:

    【解决方案2】:
    $paths = Get-ChildItem -Directory c:\users | Select-Object $_.Name
    
    ForEach ($path in $paths){
        If (test-path "c:\users\$path\AppData\Local\Microsoft\Windows\WER\ReportQueue\AppCrash")
        {
            Remove-Item -Path "c:\users\$path\AppData\Local\Microsoft\Windows\WER\ReportQueue\AppCrash\*" -Force
    
        }
    }
    

    【讨论】:

    • 您能解释一下您的答案,而不仅仅是发布代码吗?请同时格式化代码(缩进 4 个空格或使用{} 按钮)。
    猜你喜欢
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多