試圖簡化此代碼的顯而易見的原因..... 發現這個小snippit作爲一個開始,但我真的不知道如何將其納入更大的計劃。簡化PowerShell代碼
它基本上是一個電話簿CSV
文件,我們需要分解成多個文件。下面的代碼的工作,但它的效率非常低:低於
[char[]](65..90)
代碼:
GC C:\Users\x\documents\Telephonebook.csv | %{
if ($_.StartsWith("A")){
$_ | out-file -filepath c:\users\aricci\documents\A.asp -append
}
ElseIf ($_.StartsWith("B")){
$_ | out-file -filepath c:\users\aricci\documents\B.asp -append
}
ElseIf ($_.StartsWith("C")){
$_ | out-file -filepath c:\users\aricci\documents\C.asp -append
}
ElseIf ($_.StartsWith("D")){
$_ | out-file -filepath c:\users\aricci\documents\D.asp -append
}
ElseIf ($_.StartsWith("E")){
$_ | out-file -filepath c:\users\aricci\documents\E.asp -append
}
ElseIf ($_.StartsWith("F")){
$_ | out-file -filepath c:\users\aricci\documents\F.asp -append
}
ElseIf ($_.StartsWith("G")){
$_ | out-file -filepath c:\users\aricci\documents\G.asp -append
}
ElseIf ($_.StartsWith("H")){
$_ | out-file -filepath c:\users\aricci\documents\H.asp -append
}
ElseIf ($_.StartsWith("I")){
$_ | out-file -filepath c:\users\aricci\documents\I.asp -append
}
ElseIf ($_.StartsWith("J")){
$_ | out-file -filepath c:\users\aricci\documents\J.asp -append
}
ETC ....
可能值得注意的是,對於非常大的文件,應避免使用此方法(文件不適合可用內存=>系統啓動交換=>操作減慢到爬行)。 –