メモ
- 自然順ソートとして StrCmpLogicalW を DllImport して Add-Type で加える
- ソートは List.Sort メソッドかバブルソート
- デスクトップにあるファイルのファイル名を StrCmpLogicalW の順にしてクリップボードにコピーする使い捨てスクリプト
Set-StrictMode -Version Latest
Set-Location "${env:USERPROFILE}\Desktop"
$str = @(Get-ChildItem -File -Name)
$srcCSharp = @'
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace MyNamespace
{
public class MyClass
{
[DllImport("Shlwapi.dll", EntryPoint = "StrCmpLogicalW", CharSet = CharSet.Unicode)]
public static extern int StrCmpLogicalW(
[MarshalAs(UnmanagedType.LPWStr)] string psz1,
[MarshalAs(UnmanagedType.LPWStr)] string psz2
);
public static void MySort(ref string[] str) {
List<string> tmp = new List<string>(str);
tmp.Sort(StrCmpLogicalW);
tmp.CopyTo(str);
}
}
}
'@
Add-Type -Language CSharp -TypeDefinition $srcCSharp
[MyNamespace.MyClass]::MySort([ref]$str)
$str
$str | Set-Clipboard
- ショートカットアイコンにドラッグされたファイルのファイル名を StrCmpLogicalW の順にしてクリップボードにコピーする使い捨てスクリプト
Set-StrictMode -Version Latest
$strCSharp = @'
[DllImport("Shlwapi.dll", EntryPoint = "StrCmpLogicalW", CharSet = CharSet.Unicode)]
public static extern int StrCmpLogicalW(
[MarshalAs(UnmanagedType.LPWStr)] string psz1,
[MarshalAs(UnmanagedType.LPWStr)] string psz2
);
'@
Add-Type -Language CSharp -MemberDefinition $strCSharp -Namespace "Win32API" -Name "NativeMethods"
$strNames = @(Split-Path -Path $args -Leaf)
for ($i = 0; $i -lt $strNames.Length - 1; $i++) {
for ($j = $strNames.Length - 1; $j -gt $i; $j--) {
if ([Win32API.NativeMethods]::StrCmpLogicalW($strNames[$j - 1], $strNames[$j]) -gt 0) {
$strTmp = $strNames[$j - 1]
$strNames[$j - 1] = $strNames[$j]
$strNames[$j] = $strTmp
}
}
}
$strNames
$strNames | Set-Clipboard