Powershell中的switch语句

Hituptony

好的,我有一部分脚本看起来像这样。

Switch(GCI $source\*.EDIPROD){
    {(GC $_|Select -first 1).substring(176) -match "^834"}{$_ | ?{$_.Name -match "^.+?\.D(\d{6}).*"} | Rename-Item -NewName {"834Dailyin$($Matches[1]).txt"};Continue}
    {(GC $_|Select -first 1).substring(405) -match "^030"}{$_ | ?{$_.Name -match "^.+?\.D(\d{6}).*"} | Rename-Item -NewName {"834Roster$($Matches[1]).txt"};Continue}
    {(GC $_|Select -first 1).substring(176) -match "^820"}{$_ | ?{$_.Name -match "^.+?\.D(\d{6}).*"} | Rename-Item -NewName {"820Dailyin$($Matches[1]).txt"};Continue}
    {(GC $_|Select -first 1) -match "NO INPUT"}{$_ | ?{$_.Name -match "^.+?\.D(\d{6}).*"} | Rename-Item -NewName {"NOINPUTin$($Matches[1]).txt"};Continue}
    {(GC $_|Select -first 1) -match ""}{$_ | ?{$_.Name -match "^.+?\.D(\d{6}).*"} | Rename-Item -NewName Default {"Could not find 834 or 820 qualifier in file $_"};Continue}
}

我的问题在第一和第二标准上起作用。

因此,它不是在寻找相同的子字符串值,如果一个文件进入并且在子字符串176处具有834,是否会在检查第405列的^ 030之前进行匹配?

我认为它将首先评估176列,所以也许我应该在第二个中进行两次比赛?或将第二个移到第一个?你们有什么感想?

基思·希尔

是的,它将首先在834上匹配,因为这是第一个开关条件。另外,由于使用continue,在834匹配之后,switch语句将不再处理当前文件的任何条件,并移至输入中的下一个文件。因此,如果要在单个文件上匹配多个条件,请不要使用continue而且,如果您需要先匹配030,则将其向上移动,这是第一个条件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章