如何在批处理文件重命名中排除带有后缀的文件?

编程入门 行业动态 更新时间:2024-10-23 16:28:37
本文介绍了如何在批处理文件重命名中排除带有后缀的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图排除任何带有机密"的文件在名称中被此脚本重命名,该脚本批量重命名文件夹中的文件.

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.

更多推荐

如何在批处理文件重命名中排除带有后缀的文件?

本文发布于:2023-11-04 05:28:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557073.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后缀   重命名   批处理文件   文件   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!