PowerShell批量重命名与正则表达式?

时间:2017-11-15 18:13:43

标签: powershell

所以我有一组文件名如下:

A-2.txt
B-1.TXT
c-3.txt

我正在尝试将它们重命名为:

1.txt -----> b-1.txt

2.txt -----> a-2.txt

3.txt -----> c-3.txt

但是,我无法弄清楚相应的正则表达式是什么,以及如何将其编码到PowerShell中。我看到大量的例子来删除空格或下划线,但没有任何东西要从字符串中删除。

1 个答案:

答案 0 :(得分:-2)

试试这个(删除whatif)

Get-ChildItem "c:\temp" -file -Filter "*.txt" | where BaseName -Match ".-." | %{
$newname="{0}{1}" -f ($_.BaseName -split '-')[1], $_.Extension
Rename-Item $_.FullName $newname -WhatIf
}
相关问题