回复 3# czvde
保存为bat与处理文件放一起
测试前请备份文件- @Powershell "& {[ScriptBlock]::Create("'#' + ([io.file]::ReadAllText('%~f0',[text.encoding]::Default))").Invoke()}" & pause & exit
- $files = Get-ChildItem -Path .\* -Include *.txt;
- $count = $files.Count;
- $lastFile='';
- $dic = New-Object -TypeName 'System.Collections.Generic.Dictionary[string,object]'
-
-
- #文件按基名前缀分组
- foreach($file in $files)
- {
- if($file.BaseName -match '^(.*?)-(\d+)$')
- {
- if(-not $dic.ContainsKey($Matches[1])){
- $dic.Add($Matches[1],[System.Collections.ArrayList] @([int] $Matches[2]));
- }
- else {
- [void] $dic[$Matches[1]].Add([int] $Matches[2]);
- }
- }
-
- }
- #组内文件按基名后缀排序
- foreach($key in $dic.Keys)
- {
- $count=$dic[$key].Count;
- $counter=0;
- foreach($index in ($dic[$key]|sort))
- {
-
- switch(++$counter)
- {
- 1 {$subfix = '_A';break;}
- $count {$subfix = '_B';break;}
- default {$subfix = '';break;}
- }
-
- [string] $num=$index;
- if($index -lt 10) {$num="{0:00}" -f $index}
- if($key -match '(\w{4})$'){
- ren "$key-$index.txt" "[$($Matches[1].ToUpper())][$count-$num$subfix]$key-$index.txt"
- }
-
- }
- }
复制代码
|