Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows Powershell重命名文件正则表达式_Windows_Powershell_Scripting - Fatal编程技术网

Windows Powershell重命名文件正则表达式

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 ????????? } }

我在一个文件夹中有很多.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 ????????? }
}
正确的方法是什么?

这应该有效:

$_.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])
}