2021年3月17日星期三

rename files with powershell by deleting everything after the @ mark in the filename

I have files named like 12420025@pa20s6gz@12420025_xxxgggopppxk.pdf

I want to rename them so that everything after the @ mark until the . and file extension are removed. So it would look like 12420025.pdf I tried this but it gives an error. I cd'd to the directory in powershell and tried

Get-ChildItem -Filter *.pdf | Foreach-Object -Process {       $NewName = [Regex]::Match($@.Name,"^[^@]*").Value + '.pdf'      $@ | Rename-Item -NewName $NewName  

but I am getting this error

At line:2 char:31  +     $NewName = [Regex]::Match($@.Name,"^[^@]*").Value + '.pdf'  +                               ~  Missing ')' in method call.  At line:2 char:31  +     $NewName = [Regex]::Match($@.Name,"^[^@]*").Value + '.pdf'  +                               ~~~~~~~  Unexpected token '$@.Name' in expression or statement.  At line:2 char:38  +     $NewName = [Regex]::Match($@.Name,"^[^@]*").Value + '.pdf'  +                                      ~  Missing argument in parameter list.  At line:1 char:55  + Get-ChildItem -Filter *.pdf | Foreach-Object -Process {  +                                                       ~  Missing closing '}' in statement block or type definition.  At line:2 char:47  +     $NewName = [Regex]::Match($@.Name,"^[^@]*").Value + '.pdf'  +                                               ~  Unexpected token ')' in expression or statement.      + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException      + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall  
https://stackoverflow.com/questions/66683208/rename-files-with-powershell-by-deleting-everything-after-the-mark-in-the-file March 18, 2021 at 08:34AM

没有评论:

发表评论