2016年4月19日火曜日

powershellでファイル名変更

powershell を使ったファイル名の変更 正規表現を使って「1.1.あいうえお.txt」というファイル名があった場合には、「1.01.あいうえお.txt」に変更する
@powershell -NoProfile -ExecutionPolicy Unrestricted "$s=[scriptblock]::create((gc \"%~f0\"|?{$_.readcount -gt 1})-join\"`n\");&$s" %*&goto:eof

$ary=ls|%{$_.name}
foreach($i in $ary){
  if($i -match "^[0-9]\.[0-9]\."){
    Get-ChildItem $i | Rename-Item -NewName { $_.Name -replace '^([0-9])\.([0-9])\.','$1.0$2.' }
  }
}