试图简化此代码的显而易见的原因..... 发现这个小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 ....
可能值得注意的是,对于非常大的文件,应避免使用此方法(文件不适合可用内存=>系统启动交换=>操作减慢到爬行)。 –