2014年7月4日金曜日

◆拡張子毎にファイルをフォルダに振り分ける

  PowerShellの紹介とかサンプルでよく出てきそうなやつ。

以下のようなごちゃごちゃのファイルを拡張子毎にフォルダを作って振り分けてコピーする。

image

001
002
003
004
005
006

$outDir = "d:\Desktop\out1"
dir "d:\Desktop\imp1" | group extension | %
{
 
$outDirEx = Join-Path $outDir $_.Name.Substring(1
)
 
if(!(Test-Path $outDirEx)){mkdir $outDirEx
} 
 
$_.Group | %{copy $_.FullName $outDirEx }
}

結果

image

2014年7月1日火曜日

◆演算子を連結して実行する

PowerShellの演算子は連結して実行できたりする。

PS>"a,b,c,d,e" -split ','
a
b
c
d
e
PS>"a,b,c,d,e" -split ',' -notlike 'c'
a
b
d
e
PS>"a,b,c,d,e" -split ',' -notlike 'c'-replace 'b','{0}'
a
{0}
d
e
PS>"a,b,c,d,e" -split ',' -notlike 'c'-replace 'b','{0}'-join '-'
a-{0}-d-e
PS>("a,b,c,d,e" -split ',' -notlike 'c'-replace 'b','{0}'-join '-') -f 'bbb'
a-bbb-d-e

最後のフォーマット演算子(-f)はなぜか括弧が必要だった。
(優先順位が違うのだろうか)