简体   繁体   English

使用PowerShell重命名给定目录和子文件夹中所有没有扩展名的文件

[英]Rename all files with no extension in a given directory and sub-folders using PowerShell

I need to write a script in PowerShell which renames all the files with no extension under a given directory and all subfolders. 我需要在PowerShell中编写一个脚本,该脚本重命名给定目录和所有子文件夹下没有扩展名的所有文件。 By renaming I mean to add an extension eg ".html" to the file which doesn't have it. 通过重命名,我的意思是向没有扩展名的文件添加扩展名,例如“ .html”。 So far I've tried to build something like this: 到目前为止,我已经尝试构建如下内容:

Get-ChildItem -Path 'C:\dev\blah' -Filter *. -Recurse | ForEach-Object {
    Rename-Item -Path $_.FullName -NewName ($_.FullName + ".html")
}

You need to exclude folders from the enumerated items. 您需要从枚举项目中排除文件夹。 Use the switch -File if you're running PowerShell v3 or newer, otherwise add a Where-Object filter. 如果您正在运行PowerShell v3或更高版本,请使用-File开关,否则请添加Where-Object过滤器。 Also, you don't need ForEach-Object since Rename-Item accepts pipeline input. 此外,由于Rename-Item接受管道输入,因此您不需要ForEach-Object

Get-ChildItem -Path 'C:\dev\blah' -Filter *. -Recurse | Where-Object {
    -not $_.PSIsContainer
} | Rename-Item -NewName {$_.Name + ".html"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM