Powershell Get-ChildItem -recurse无法获取所有项目

Dan*_*sen 7 powershell recursion get-childitem subdirectory delete-file

我正在使用powershell脚本擦除文件夹中的某些文件,并将其余文件移动到预定义的子文件夹中.

我的结构看起来像这样

Main
    (Contains a bunch of pdb and dll files)
    -- _publish
        --Website
            (Contains a web.config, two other .config files and a global.asax file)
            -- bin
                (Contains a pdb and dll file)
            -- JS
            -- Pages
            -- Resources
Run Code Online (Sandbox Code Playgroud)

我想在开始移动它们之前从整个文件结构中删除所有pdb,config和asax文件.我用的是:

$pdbfiles = Get-ChildItem "$executingScriptDirectory\*.pdb" -recurse

foreach ($file in $pdbfiles) {
    Remove-Item $file
}
Run Code Online (Sandbox Code Playgroud)

对于我需要删除的所有文件类型等等.它的工作原理很好,除了位于网站bin文件夹中的pdb文件.并为网站文件夹中的ASAX文件.出于某种原因,Get-ChildItem recurse搜索会忽略它们.

这是由于resursive结构内物品的深度造成的吗?或者是别的什么?我该如何修复它,因此它会删除指定的所有文件.

编辑:我已经尝试添加-force - 但它没有改变任何东西

答案:以下工作:

$include = @("*.asax","*.pdb","*.config")
$removefiles = Get-ChildItem "$executingScriptDirectory\*" -recurse -force -include $include 

foreach ($file in $removefiles) {
    if ($file.Name -ne "Web.config") {
        Remove-Item $file
    }
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*ant 14

Get-ChildItem -path <yourpath> -recurse -Include *.pdb
Run Code Online (Sandbox Code Playgroud)

  • @Xenoxsis注意 - 如果只需要一个过滤器,请使用`-filter`而不是`-include`,因为它更快.您会发现与大型目录结构的区别. (2认同)