bat合并文件时想把合并的顺序数字排序1,10,11,2,20,21 改成 1,2,3...这种数字顺序,请教下有没有办法?

如题所述

不清楚你的实际文件/情况,仅以问题中的样例/说明为据;以下代码复制粘贴到记事本,另存为xx.bat,编码选ANSI,跟要处理的文件放一起运行
<# :
cls
@echo off
rem 将多个文件的名称末尾的数字前面用0补足成相同位数
set #=Any question&set @=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%@% %z%
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::Default))) -Args '%~f0'"
echo;%#% +%$%%$%/%@% %z%
pause
exit
#>
$self=gi -liter $args[0];
$path=$self.Directory.FullName;
$len=0;[System.Collections.ArrayList]$list=@();
$files=@(dir -liter $path|?{($_.Name -ne $self.Name) -and ($_ -is [System.IO.FileInfo])});
for($i=0;$i -lt $files.length;$i++){
$m=[regex]::match($files[$i].BaseName,'\d+$');
if($m.Success){
[void]$list.add($files[$i]);
if($m.groups[0].value.length -gt $len){
$len=$m.groups[0].value.length;
};
};
};
for($i=0;$i -lt $list.Count;$i++){
$num=$list[$i].BaseName -replace '^.*\D','';
$newnum=([Math]::Pow(10, $len)+[int]$num).toString().Substring(1);
$newname=($list[$i].BaseName -replace '\d+$',$newnum)+$list[$i].Extension;
if($list[$i].Name -ne $newname){
write-host ($list[$i].Name+' --> '+$newname);
mv -liter $list[$i].FullName ($list[$i].Directory.FullName+'\'+$newname);
};
}
温馨提示:答案为网友推荐,仅供参考
相似回答