【问题标题】:Is there a PowerShell script that displays the files within the directory and deletes the .xml files within those folders是否有 PowerShell 脚本显示目录中的文件并删除这些文件夹中的 .xml 文件
【发布时间】:2019-07-22 23:59:46
【问题描述】:

我正在尝试显示 .xml 文件,然后将它们全部删除。所有其他文件,例如 .txt、.pdf 文件都可以保留在该文件夹中。这是我到目前为止所拥有的。由于我有多个文件夹,因此我对脚本编写很陌生,并试图找到有效的方法。

$Folders = Get-childitem "my path" -Name
$HR = $Folders
$HR
foreach ($item in $HR)
{
    $path = "my path" +$item + "\config\"
    $path
    $file = Get-ChildItem $path
    $file
        foreach ($item2 in $file) {

          if ($file.name -eq 'XML*') {
             $file
          }
    Get-ChildItem -include *
  }
}

我确实从每个目录中显示了文件,但我无法以某种方式排除 .txt、.pdf 文件。

【问题讨论】:

    标签: powershell scripting


    【解决方案1】:

    你可以作为一个班轮做到这一点:

    Get-ChildItem | Where-Object {$_.Extension -eq ".xml"} | ForEach-Object { del $_.FullName}
    

    只需从要删除所有 xml 文件的目录中调用它即可。

    或者如果它是目录的层次结构,例如

    E:\AppFiles .\临时文件 .\图片

    您也可以使用 -recurse 开关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-28
      • 2023-03-23
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多