问题描述
我试图排除任何带有机密"的文件在名称中被此脚本重命名,该脚本批量重命名文件夹中的文件.
I'm trying to exclude any file with "Confidential" in the name from being renamed by this script which batch renames files in a folder.
但是,当我尝试这个脚本时,文件名带有机密"它不会改变,没有机密"的也不会改变.在里面.
However, when I try this script the file names with "Confidential" in it don't change, nor do the ones without "Confidential" in it.
Get-ChildItem -Exclude "*Confidential*" | rename-item -NewName { $_.BaseName + " - Confidential" +$_.Extension }
推荐答案
不幸的是,在没有 -Recurse
的情况下,-Include
和 -Exclude
参数仅适用于输入路径(当前目录,在您的情况下)的叶组件(文件/目录名称),不适用于其子级.
Unfortunately, in the absence of -Recurse
, the -Include
and -Exclude
parameters apply only to the leaf component (file/directory name) of the input path (the current dir., in your case), not to those its children.
解决方法是改用 Get-Item *
(或 Get-ChildItem *
).这确保(隐含的)目标路径的 children 是有针对性的(使用显式目标路径,append /*
; add -Force
以包含 hidden 子项),从而确保将任何 -Include
/-Exclude
参数应用于那些的名称孩子们:
The workaround is to use Get-Item *
(or Get-ChildItem *
) instead. which ensures that the (implied) target path's children are targeted (with an explicit target path, append /*
; add -Force
to include hidden children), which in turn ensures that any -Include
/ -Exclude
arguments are applied to the names of those children:
Get-Item * -Exclude *Confidential* |
Rename-Item -NewName { $_.BaseName + " - Confidential" + $_.Extension }
有关讨论,请参阅 GitHub 问题 #3304.
这篇关于如何在批处理文件重命名中排除带有后缀的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!