在 PowerShell 中重命名许多文件夹
我有超过 1000 个文件需要重命名。
第一组文件夹和/或文件按位置分组,因此每个文件的前四个字符相同;有四到五个不同的地点。我需要删除文件夹名称的前几个字符。
例子:
旧文件:ABC_Doe,Jane
新文件:Doe,Jane
对于执行此操作的最快方法有什么建议吗?
我已尝试以下所有方法:
1st Attempt
$a = Get-ChildItem C:\example
$b = Where-Object {$_.name -like “*ABC_*”}
$cmdlet_name = “Rename-Item”
$d = (cmdlet_name $a $b)
invoke-expression $d
2nd Attempt
$e = Get-ChildItem C:\example
$f = $e.TrimStart (“ABC_”)
第三次尝试
Rename-Item -{$_.name -like “*ASD*”, “”}
I have over 1000+ files that have to be renamed.
The first set folder and/or files are grouped by location, so the first four characters are the same for each file; there are four-five different locations. I need to delete the first few characters of the folder's name.
Example:
Old File: ABC_Doe, Jane
New File: Doe, Jane
any suggestions as to the quickest way to carry this out?
I've tried all of the following:
1st Attempt
$a = Get-ChildItem C:\example
$b = Where-Object {$_.name -like “*ABC_*”}
$cmdlet_name = “Rename-Item”
$d = (cmdlet_name $a $b)
invoke-expression $d
2nd Attempt
$e = Get-ChildItem C:\example
$f = $e.TrimStart (“ABC_”)
3rd Attempt
Rename-Item -{$_.name -like “*ASD*”, “”}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个,获取所有子项目(仅文件),通过替换它们(什么都不做)来删除 abc_ 并重命名每个文件。要重命名子目录中的文件,请将 -Recurse 开关添加到 Get-ChildItem 命令:
UPDATE
实际上,这应该也可以工作并且要短得多(不需要附加文件扩展名,因为执行了重命名关于文件名)。
Try this, get all child items (files only), remove abc_ by replacing them (with nothing) and rename each file. To rename files in sub-directories add the -Recurse switch to the Get-ChildItem command:
UPDATE
Actually, this should work as well and is much shorter (no need to append the file extension cause renaming is performed on the file name).
来源:
get-help rename-item -full
Source:
get-help rename-item -full