Windows Powershell重命名文件正则表达式
我在一个文件夹中有很多.pdf文件,命名格式为whatevername space random code,以Windows Powershell重命名文件正则表达式,windows,powershell,scripting,Windows,Powershell,Scripting,我在一个文件夹中有很多.pdf文件,命名格式为whatevername space random code,以U.pdf开头,我想删除U之前的命名 名称格式示例: Alex U153569.pdf->应重命名为U153569.pdf 这就是我到目前为止所做的: foreach ($test in $testpdf) { Get-ChildItem -Filter *.pdf | Rename-Item -NewName { $_.name -Replace ????????? } }
U.pdf
开头,我想删除U之前的命名
名称格式示例:
Alex U153569.pdf
->应重命名为U153569.pdf
这就是我到目前为止所做的:
foreach ($test in $testpdf) {
Get-ChildItem -Filter *.pdf | Rename-Item -NewName { $_.name -Replace ????????? }
}
正确的方法是什么?这应该有效:
$_.name -Replace '.*\s(?=U)'
这应该起作用:
$_.name -Replace '.*\s(?=U)'
如果不进行测试,您应该能够使用split操作符(不需要正则表达式)。您可以按如下所示在空间上拆分,并索引到第二个拆分
[1]
$testpdf = Get-ChildItem *.pdf
foreach ($test in $testpdf) {
Rename-Item $test.name -NewName (($test.name -split " ")[1])
}
如果不进行测试,您应该能够使用split操作符(不需要正则表达式)。您可以按如下所示在空间上拆分,并索引到第二个拆分
[1]
$testpdf = Get-ChildItem *.pdf
foreach ($test in $testpdf) {
Rename-Item $test.name -NewName (($test.name -split " ")[1])
}