メモ
- 現在のフォルダ内にあるファイル名を StrCmpLogicalW と List<T>.Sort(Comparison<T>) でソートして取得
- このスクリプトを起動させるショートカット(.lnk)をSendToフォルダに入れて使用
using namespace System.IO
Set-StrictMode -Version Latest
$srcCSharp = @'
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
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 int SortStrCmpLW<T>(T obj1, T obj2) where T : FileSystemInfo {
return StrCmpLogicalW(obj1.Name, obj2.Name);
}
public static T[] MySort<T>(T[] obj) where T : FileSystemInfo {
List<T> tmp = new List<T>(obj);
tmp.Sort(SortStrCmpLW<T>);
return tmp.ToArray();
}
}
}
'@
Add-Type -Language CSharp -TypeDefinition $srcCSharp
$gciOrig = [FileSystemInfo[]]@(Get-ChildItem -Force -File)
$gciSorted = [MyNamespace.MyClass]::MySort($gciOrig)
Write-Host "`$gciSorted.count: $($gciSorted.count)"
if ( $gciSorted.count -gt 0 ) {
Write-Host ($gciSorted.Name -join "`n")
$gciSorted.Name | Set-Clipboard
} else {
@() | Set-Clipboard
}