You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Get-ChildItem加正则不递归及删除._FILENAME脚本递归失效问题咨询

Hey there! Let's work through these two PowerShell headaches together—they're both super common, so I’ve got straightforward fixes for you.

1. Fixing Recursive Search with Get-ChildItem and Regex Patterns

The most likely issue here is mixing up PowerShell's wildcard filters with true regex, or using the wrong parameters alongside -Recurse. Here's the breakdown:

  • Get-ChildItem's -Filter and -Include parameters use PowerShell wildcards (like *, ?, []), not full regular expressions. If you tried passing a regex directly to these parameters, it won't behave as expected.
  • To use regex for matching, you need to pipe the recursive results to Where-Object and use the -match operator, which supports proper regex syntax.

Here's a working example to recursively find files matching your regex pattern:

# Replace "C:\Your\Root\Path" with your target directory
# Replace "^YourRegexPattern$" with your actual regex (e.g., "^report_\d{4}\.txt$" for files like report_2024.txt)
Get-ChildItem -Path "C:\Your\Root\Path" -Recurse -File | Where-Object { $_.Name -match "^YourRegexPattern$" }

If you only need wildcards (not full regex), you can simplify with -Include—just make sure your path ends with * to ensure recursion works correctly:

Get-ChildItem -Path "C:\Your\Root\Path\*" -Recurse -Include "*.log"
2. Recursively Deleting All ._FILENAME Files

Your script is only scanning the current directory because it's missing the -Recurse parameter. Let's fix that with a safe, tested approach:

First, always preview the files you're about to delete to avoid accidental data loss. Run this command to list all ._-prefixed files recursively:

# Replace "C:\Your\Target\Directory" with your root folder
Get-ChildItem -Path "C:\Your\Target\Directory" -Recurse -Filter "._*" -File

Once you confirm the list is correct, you can delete them. Add -WhatIf first to simulate the deletion (no files will actually be removed):

Get-ChildItem -Path "C:\Your\Target\Directory" -Recurse -Filter "._*" -File | Remove-Item -WhatIf

If the simulation looks good, remove -WhatIf to perform the actual deletion:

Get-ChildItem -Path "C:\Your\Target\Directory" -Recurse -Filter "._*" -File | Remove-Item

Extra Tips:

  • Add -Force to delete read-only ._ files, if needed: Remove-Item -Force
  • If you need to target only specific ._FILENAME matches (not all ._ files), swap -Filter for a regex check with Where-Object, like:
    Get-ChildItem -Path "C:\Your\Target\Directory" -Recurse -File | Where-Object { $_.Name -match "^._SpecificFileName$" } | Remove-Item
    

内容的提问来源于stack exchange,提问作者Jonathan Lafleur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:51:44