本帖最后由 flashercs 于 2022-5-30 10:23 编辑
- <#*,:&cls
- @echo off
- cd /d "%~dp0"
- powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
- pause
- exit /b
- #>
- $beginNumber = 1
- Add-Type -TypeDefinition @'
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
-
- namespace MyCode
- {
- public class ExplorerViewComparer : Comparer<string>
- {
- [DllImport("Shlwapi.dll", EntryPoint = "StrCmpLogical", CharSet = CharSet.Auto)]
- internal static extern int StrCmpLogical(string p1, string p2);
-
- public override int Compare(string x, string y)
- {
- return StrCmpLogical(x, y);
- }
- }
- }
- '@
- $ExpComp = New-Object MyCode.ExplorerViewComparer
- [system.collections.arraylist]$alfiles= @(Get-ChildItem -Path .\*_* | Where-Object { -not $_.PSIsContainer } | ForEach-Object { $_.Name })
-
- [void]$alfiles.Sort($ExpComp)
-
- $alfiles | Select-Object -Property @{
- Name = 'prefix'
- Expression = {
- if ($_ -match '^([^_]*_+)(\d+)') {
- $script:m = $Matches
- $script:m[1]
- } else {
- $Script:m = $null
- ""
- }
- }
- }, @{
- Name = 'suffix'
- Expression = { if ($Script:m) { $Script:m[2] } }
- }, @{
- Name = 'number'
- Expression = { if ($Script:m) { $Script:m[2] -as [int] } }
- } | Group-Object -Property prefix | ForEach-Object {
- if ($_.Name -ne "") {
- $arrnumbers = @($_.Group | ForEach-Object { $_.number })
- $numberLength = $_.Group[0].suffix.Length
- for ($i = $beginNumber; $i -le $arrnumbers[-1]; $i++) {
- if ($arrnumbers -notcontains $i) {
- $_.Name + $i.ToString().PadLeft($numberLength, '0')
- }
- }
- }
- } | Set-Content -LiteralPath .\aaa.txt
复制代码
|