如何在Powershell中更改多个文件名?
我正在尝试从多个文件的末尾删除名称“-xx_xx”的部分内容。我用这个,效果很好 dir | Rename Item-NewName{$\uu.Name-replace“-xx_xx”,“”} 但是,还有其他部分,如: “-yy_-yy” “-zz_-zz”如何在Powershell中更改多个文件名?,powershell,rename,Powershell,Rename,我正在尝试从多个文件的末尾删除名称“-xx_xx”的部分内容。我用这个,效果很好 dir | Rename Item-NewName{$\uu.Name-replace“-xx_xx”,“”} 但是,还有其他部分,如: “-yy_-yy” “-zz_-zz” 如何一次删除所有这些,而不是一次又一次地运行它来更改要删除的名称部分?最简单的方法 如果需要的话,您可以继续编写-replace语句,直到奶牛回家 $myLongFileName = "Something xx_xx yy_yy
如何一次删除所有这些,而不是一次又一次地运行它来更改要删除的名称部分?最简单的方法 如果需要的话,您可以继续编写
-replace
语句,直到奶牛回家
$myLongFileName = "Something xx_xx yy_yy zz_zz" -replace "xx_xx","" -replace "yy_yy"
更简洁的语法
如果每个文件都有这些,您还可以创建一个要替换的片段数组,就像这样,只需用逗号分隔它们
$stuffWeDontWantInOurFile =@("xx_xx", "yy_yy", "zz_zz")
$myLongFileName -replace $stuffWeDontWantInOurFile, ""
还有另一种方式
如果文件元素之间用空格、破折号或其他可预测的分隔符分隔,则可以在该位置拆分文件名
$myLongFileName = "Something xx_xx yy_yy zz_zz"
PS> $myLongFileName.Split()
Something
xx_xx
yy_yy
zz_zz
PS> $myLongFileName.Split()[0] #select just the first piece
Something
对于空格,可以使用Spit()
方法,其中没有重载
如果是破折号或其他字符,您可以像这样提供
Split(“-”)
。在这些技术之间,你应该能够做你想做的事。如果你说,模式-xx\u xx
总是在文件名的末尾,我建议你使用如下方式:
Get-ChildItem -Path '<TheFolderWhereTheFilesAre>' -File |
Rename-Item -NewName {
'{0}{1}' -f ($_.BaseName -replace '\s*-\s*.._..$'), $_.Extension
} -WhatIf
正则表达式详细信息:
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
- Match the character “-” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
. Match any single character that is not a line break character
. Match any single character that is not a line break character
_ Match the character “_” literally
. Match any single character that is not a line break character
. Match any single character that is not a line break character
$ Assert position at the end of the string (or before the line break at the end of the string, if any)
\s匹配单个“空白字符”(空格、制表符、换行符等)
*在零次和无限次之间,尽可能多次,根据需要回馈(贪婪)
-按字面意思匹配字符“-”
\s匹配单个“空白字符”(空格、制表符、换行符等)
*在零次和无限次之间,尽可能多次,根据需要回馈(贪婪)
. 匹配不是换行符的任何单个字符
. 匹配不是换行符的任何单个字符
_按字面意思匹配字符“u”
. 匹配不是换行符的任何单个字符
. 匹配不是换行符的任何单个字符
$Assert位置位于字符串末尾(或字符串末尾的换行符之前,如果有)
对不起,我是新手,也许我不清楚。并不是在同一个文件名中有多个此约定的实例,而是分布在不同的文件名中。例如:filename-xx_xx.html或filename2-yy_yy.html或filename3-zz_zz.html//回顾一下,第一个答案就确定了。这太棒了。但是,我发现命名约定有时是不同的,类似于“-xx_xx-yy_zz-X-Y”或“-xx_xx-aa_bb-X-Y”,我想删除所有这些。当我使用您的第一个选项并在中输入此选项而不是“-xx_xx”时,它不起作用。为什么会这样? \s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) * Between zero and unlimited times, as many times as possible, giving back as needed (greedy) - Match the character “-” literally \s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) * Between zero and unlimited times, as many times as possible, giving back as needed (greedy) . Match any single character that is not a line break character . Match any single character that is not a line break character _ Match the character “_” literally . Match any single character that is not a line break character . Match any single character that is not a line break character $ Assert position at the end of the string (or before the line break at the end of the string, if any)