PowerShellで漢字をカタカナに変換する

| 2009年6月24日水曜日
漢字をひらがなやカタカナに変換したくなったので調べてみた。
簡単に変換するにはExcelのルビを振る関数でPhonetic関数を使うのが簡単そうだ。

ただし、「あぁアアAaAa1」を変換すると全部全角の「アァアアAaAa1」になってしまうので注意。

--- ConvertTo-KanjiFromKatakana.ps1 ---
  1. function global:ConvertTo-KanjiFromKatakana([string]$word) {  
  2.         $excel = New-Object -comObject Excel.Application  
  3.         $result = $excel.GetPhonetic($word)  
  4.           
  5.         #Excel COM Objectを解放  
  6.         [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel)  
  7.           
  8.         return $result  
  9. }  

C#でエクスプローラーで表示されるファイルの並び順と同じにソートしてみる

| 2009年6月20日土曜日
必要があったのでメモ。

IComparerを使ってArray.Sort()でソートする。

使い方は、

  1. string[] list = { "01.txt""1.txt""02.txt" }  
  2. Array.Sort(list, new ExplorerSortComparer());  

こんな感じ
--- ExplorerSortComparer Class ---
  1. public class ExplorerSortComparer : IComparer {  
  2.     [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]  
  3.     public static extern int StrCmpLogicalW(string str1, string str2);  
  4.   
  5.     public int Compare(object obj1, object obj2) {  
  6.         return StrCmpLogicalW((string)obj1, (string)obj2));  
  7.     }  
  8. }  

VBScriptでゴミ箱のファイルを削除してみる

|
PowerShellではできたので、VBScriptで指定日以前にごみ箱に捨てたファイルを削除してみる。
PowerShellがもっと流行れば、VBScriptから逃げられるんだけど…その日はまだ遠そうだ。

※ごみ箱の中のフォルダを消そうとすると「実行時エラー:・・・」となるので一度Tempフォルダに移動してからcmdのrmdirで消している。

3日前に削除したファイルをけすなら、cmdなどから

> CScript /nologo DeleteGarbabeFiles.vbs /Date:3

とする。
/Date:3 をつけなければ、一週間前までのを削除する。

--- DeleteGarbabeFiles.vbs ---
  1. If WScript.Arguments.Named("Date") = "" Then  
  2.   nDays = 6  
  3. Else  
  4.   nDays = Int(WScript.Arguments.Named("Date"))  
  5. End If  
  6.   
  7. Set objFSO = CreateObject("Scripting.FileSystemObject")  
  8. Set objShell = CreateObject("Shell.Application")  
  9. Set WshShell = CreateObject("WScript.Shell")  
  10. Set trash = objShell.NameSpace(10)  
  11.   
  12. For Each item in trash.Items()  
  13.   If Int(Now() - CDate(trash.GetDetailsOf(item, 2))) >= nDays Then  
  14.     If objFSO.FileExists(item.Path) Then  
  15.       objFSO.DeleteFile item.Path  
  16.     ElseIf objFSO.FolderExists(item.Path) Then  
  17.       dustFolderPath = objFSO.GetSpecialFolder(2).Path & "_TempDeleteFolder"  
  18.       objFSO.MoveFolder item.Path, dustFolderPath  
  19.       delCommand = "cmd.exe /C rmdir /S /Q " & """" & dustFolderPath & """"   
  20.       WshShell.Run delCommand, 0, true  
  21.     End If  
  22.   End If  
  23. Next  
  24.   
  25. Set objFSO = Nothing  
  26. Set objShell = Nothing  
  27. Set WshShell = Nothing  
  28. Set trash = Nothing  

PowerShellから新しいウィンドウのPowerShellを開く

| 2009年6月16日火曜日
PowerShellから新しいウィンドウのPowerShellを開くには、WScript.Shellを使う。

> $WShell = New-Object -comObject WScript.Shell
> $posh = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
> [void]$WShell.Run($posh , 1)

これを「New-PowerShellWindow.ps1」とかにしてパスの通ったところに置いておけば、キーボードだけでやりたい人には使えるかもしれない。