Powershell 复杂文件重命名

Powershell 复杂文件重命名,powershell,Powershell,我在一个文件夹里有大约100个文件。我想重命名那些文件。文件的格式如下 AB1234.gif B3245.gif AB2541.gif AB11422.jpg 等等 输出文件应该是 AB-0111-1.gif B-0111-2.gif AB-0111-3.gif AB-0111-4.jpg 逻辑将是 for(int n=0;n<files.count;n++){ if(file is starting with AB) fileName = A

我在一个文件夹里有大约100个文件。我想重命名那些文件。文件的格式如下

AB1234.gif    
B3245.gif  
AB2541.gif  
AB11422.jpg 
等等

输出文件应该是

AB-0111-1.gif
B-0111-2.gif
AB-0111-3.gif
AB-0111-4.jpg
逻辑将是

for(int n=0;n<files.count;n++){
    if(file is starting with AB)
        fileName = AB + "-0111" + n;
    if(file is starting with B)
        fileName = B + "-0111" + n;
}

for(int n=0;n您可以使用[System.IO.Path]::GetExtension获取文件的扩展名,如下所示:

$ext = [System.IO.Path]::GetExtension('AB1234.gif')
if ('AB1234.gif' -match '^\D*') { $first = $matches[0]; }
$first + '-0111-' + '1' + $ext
$i = 1;
while (test-path ($first + '-0111-' + $i + $ext)) {$i++}
dir | foreach -begin{$cnt = 1} -process { mv $_ ($_.Name -replace '\d+',"-0111-$cnt"); $cnt += 1}
您可以使用
$matches
变量获取文件名的第一个字母字符,如下所示:

$ext = [System.IO.Path]::GetExtension('AB1234.gif')
if ('AB1234.gif' -match '^\D*') { $first = $matches[0]; }
$first + '-0111-' + '1' + $ext
$i = 1;
while (test-path ($first + '-0111-' + $i + $ext)) {$i++}
dir | foreach -begin{$cnt = 1} -process { mv $_ ($_.Name -replace '\d+',"-0111-$cnt"); $cnt += 1}
您可以按如下方式构建新文件名:

$ext = [System.IO.Path]::GetExtension('AB1234.gif')
if ('AB1234.gif' -match '^\D*') { $first = $matches[0]; }
$first + '-0111-' + '1' + $ext
$i = 1;
while (test-path ($first + '-0111-' + $i + $ext)) {$i++}
dir | foreach -begin{$cnt = 1} -process { mv $_ ($_.Name -replace '\d+',"-0111-$cnt"); $cnt += 1}
您可以用一个变量替换上面的“1”,您可以通过检查exists属性来增加该变量,如下所示:

$ext = [System.IO.Path]::GetExtension('AB1234.gif')
if ('AB1234.gif' -match '^\D*') { $first = $matches[0]; }
$first + '-0111-' + '1' + $ext
$i = 1;
while (test-path ($first + '-0111-' + $i + $ext)) {$i++}
dir | foreach -begin{$cnt = 1} -process { mv $_ ($_.Name -replace '\d+',"-0111-$cnt"); $cnt += 1}
此循环完成后,您将获得所需的文件名,其中包含
$first+'-0111-“++$i+$ext
,您可以使用此名称使用
重命名文件来重命名文件:

Rename-File 'AB1234.gif' ($first + '-0111-' + $i + $ext)
现在,将所有这些都打包成一个循环,您应该拥有它:

dir | ForEach-Object { ... }
对于测试,将
-whatif
参数添加到
重命名文件
命令的末尾,PS将告诉您它将执行什么操作,而不是实际执行操作

谢谢,
标记

您可以使用[System.IO.Path]::GetExtension获得文件的扩展名,如下所示:

$ext = [System.IO.Path]::GetExtension('AB1234.gif')
if ('AB1234.gif' -match '^\D*') { $first = $matches[0]; }
$first + '-0111-' + '1' + $ext
$i = 1;
while (test-path ($first + '-0111-' + $i + $ext)) {$i++}
dir | foreach -begin{$cnt = 1} -process { mv $_ ($_.Name -replace '\d+',"-0111-$cnt"); $cnt += 1}
您可以使用
$matches
变量获取文件名的第一个字母字符,如下所示:

$ext = [System.IO.Path]::GetExtension('AB1234.gif')
if ('AB1234.gif' -match '^\D*') { $first = $matches[0]; }
$first + '-0111-' + '1' + $ext
$i = 1;
while (test-path ($first + '-0111-' + $i + $ext)) {$i++}
dir | foreach -begin{$cnt = 1} -process { mv $_ ($_.Name -replace '\d+',"-0111-$cnt"); $cnt += 1}
您可以按如下方式构建新文件名:

$ext = [System.IO.Path]::GetExtension('AB1234.gif')
if ('AB1234.gif' -match '^\D*') { $first = $matches[0]; }
$first + '-0111-' + '1' + $ext
$i = 1;
while (test-path ($first + '-0111-' + $i + $ext)) {$i++}
dir | foreach -begin{$cnt = 1} -process { mv $_ ($_.Name -replace '\d+',"-0111-$cnt"); $cnt += 1}
您可以用一个变量替换上面的“1”,您可以通过检查exists属性来增加该变量,如下所示:

$ext = [System.IO.Path]::GetExtension('AB1234.gif')
if ('AB1234.gif' -match '^\D*') { $first = $matches[0]; }
$first + '-0111-' + '1' + $ext
$i = 1;
while (test-path ($first + '-0111-' + $i + $ext)) {$i++}
dir | foreach -begin{$cnt = 1} -process { mv $_ ($_.Name -replace '\d+',"-0111-$cnt"); $cnt += 1}
此循环完成后,您将获得所需的文件名,其中包含
$first+'-0111-“++$i+$ext
,您可以使用此名称使用
重命名文件来重命名文件:

Rename-File 'AB1234.gif' ($first + '-0111-' + $i + $ext)
现在,将所有这些都打包成一个循环,您应该拥有它:

dir | ForEach-Object { ... }
对于测试,将
-whatif
参数添加到
重命名文件
命令的末尾,PS将告诉您它将执行什么操作,而不是实际执行操作

谢谢,
使用您描述的文件名格式标记

,您可以使用powershell
-replace
操作符将中间的数字替换为所需的表达式。唯一棘手的部分是,在循环项目时必须维护计数器。看起来是这样的:

$ext = [System.IO.Path]::GetExtension('AB1234.gif')
if ('AB1234.gif' -match '^\D*') { $first = $matches[0]; }
$first + '-0111-' + '1' + $ext
$i = 1;
while (test-path ($first + '-0111-' + $i + $ext)) {$i++}
dir | foreach -begin{$cnt = 1} -process { mv $_ ($_.Name -replace '\d+',"-0111-$cnt"); $cnt += 1}

使用您描述的文件名格式,您可以使用powershell
-replace
运算符将中间的数字替换为所需的表达式。唯一棘手的部分是,在循环项目时,您必须维护一个计数器。它将如下所示:

$ext = [System.IO.Path]::GetExtension('AB1234.gif')
if ('AB1234.gif' -match '^\D*') { $first = $matches[0]; }
$first + '-0111-' + '1' + $ext
$i = 1;
while (test-path ($first + '-0111-' + $i + $ext)) {$i++}
dir | foreach -begin{$cnt = 1} -process { mv $_ ($_.Name -replace '\d+',"-0111-$cnt"); $cnt += 1}

现在你已经澄清了你要找的是什么,你可以使用zdan发布的答案。从你原来的帖子中,我以为你在寻找更复杂的东西。现在你已经澄清了你要找的东西,你可以使用zdan发布的答案。从你原来的帖子中,我以为你在寻找更复杂的东西。