Предположим, у вас есть файлы вроде:
SIGMOID 01012019 - 01082019.XLS
SIGMOID 01012019 - 01022019.XLS
SOMETHINGELSE 01012019 - 02022019.XLS
YETANOTHERPREFIX 01012019 - 03022019.XLS
тогда следующий код переименует их:
$sourceFolder = 'THE PATH OF THE ROOTFOLDER THAT STORES THE XLS FILES TO RENAME' #'# enter your rootpath here
Get-ChildItem -Path $sourceFolder -File |
Where-Object { $_.Name -match '^\w+ +\d{8}.*\.xlsx?' } |
ForEach-Object {
$_ | Rename-Item -NewName ($_.Name -replace '^(\w+) +(\d{8}.*\.xlsx?)', '$1_$2') -WhatIf
}
стать
SIGMOID_01012019 - 01082019.XLS
SIGMOID_01012019 - 01022019.XLS
SOMETHINGELSE_02012019 - 01022019.XLS
YETANOTHERPREFIX_03012019 - 01022019.XLS
Примечание: удалите -WhatIf
, если вы довольны результатами
Сведения о регулярном выражении:
^ Assert position at the beginning of a line (at beginning of the string or after a line break character)
( Match the regular expression below and capture its match into backreference number 1
\w Match a single character that is a “word character” (letters, digits, etc.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\ Match the character “ ” literally
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 2
\d Match a single digit 0..9
{8} Exactly 8 times
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\. Match the character “.” literally
xls Match the characters “xls” literally
x Match the character “x” literally
? Between zero and one times, as many times as possible, giving back as needed (greedy)
)