【问题标题】:Powershell not continuing/not catching/not trappingPowershell 不继续/不捕获/不捕获
【发布时间】:2021-06-08 14:57:14
【问题描述】:

我正在尝试加快我编写的用于在新文件夹中复制图像的程序。当它们碰巧都有一个标准化的名称(例如 EA101、EA102 等)时,我已经让它工作了。

我对 Powershell 还很陌生,但对其他编程很熟悉。 我正在处理的问题出现在处理唯一名称时,例如 EA101-AG 或 EA105-ST 等。这是我的代码:


$variations = @("-1212W",
"-1216M",
"-1216W",
"-1818W",
"-1824M",
"-1824W",
"-2424W",
"-912M",
"-912W",
"-COAST",
"-MAG",
"-MBC",
"-MSWD",
"-ORN",
"-POST",
"-SWD",
"-TAG",
"-BMKR",
"-PIN",
"-KEY",
"-28W",
"-416W",
"-624W",
"-832W",

"-33BLK",
"-77BLK",
"-46BLK",
"-57BLK",
"-659BLK",
"-1212BLK",
"-1818BLK",
"-2424BLK",
"-912BLK",
"-1216BLK",
"-1824BLK",
"-630W",
"-630M")


foreach($n in 1..9){
   
   Copy-Item "C:\Design Images for Web\PARK\PARK10$n.jpg" -Destination "C:\Design Images for Web\PARK\JunkFiles\PARK10$n.jpg" 
    trap{
    continue;
    }

    
    foreach($m in 1..38) {
        $v = $variations[$m]
        Copy-Item "C:\Design Images for Web\PARK\PARK10$n.jpg" -Destination "C:\Design Images for Web\PARK\NewFiles\PARK10$n$v.jpg"
      
    }
}
foreach($n in 10..99){

    Copy-Item "C:\Design Images for Web\PARK\PARK1$n.jpg" -Destination "C:\Design Images for Web\PARK\JunkFiles\PARK1$n.jpg"
   
    foreach($m in 1..38){
        $v = $variations[$m]
        Copy-Item "C:\Design Images for Web\PARK\PARK1$n.jpg" -Destination "C:\Design Images for Web\PARK\NewFiles\PARK1$n$v.jpg"
       catch [System.Management.Automation.ItemNotFoundException] {break}
}
}

foreach($n in 200..999){

    Copy-Item "C:\Design Images for Web\PARK\PARK$n.jpg" -Destination "C:\Design Images for Web\PARK\JunkFiles\PARK$n.jpg"
  
    
    foreach($m in 1..38){
        $v = $variations[$m]
        Copy-Item "C:\Design Images for Web\PARK\PARK$n.jpg" -Destination "C:\Design Images for Web\PARK\NewFiles\PARK$n$v.jpg"
       
}
}

我已尝试实现陷阱、中断、继续和尝试/捕获以跳过程序运行到非标准化名称的实例。我编写了另一个脚本来处理这些。问题是,脚本 REALLY 不想停止运行整个循环并尝试制作 38 个不存在文件的副本。考虑到有近 500 个文件具有相同的命名标准,运行时间太长。我无论如何都可以运行它,但我更感兴趣的是了解 Powershell 究竟是如何忽略这些行来让它停止的。我有一种设置类型的工作,其中 Powershell 只运行一次内循环:

foreach($n in 1..9){
   
   Copy-Item "C:\Design Images for Web\PARK\PARK10$n.jpg" -Destination "C:\Design Images for Web\PARK\JunkFiles\PARK10$n.jpg" 
    trap{
    continue;
    }

    
    foreach($m in 1..38) {
        $v = $variations[$m]
        Copy-Item "C:\Design Images for Web\PARK\PARK10$n.jpg" -Destination "C:\Design Images for Web\PARK\NewFiles\PARK10$n$v.jpg"
        trap{break;}
        catch{break;} # this could also be continue, didnt make a difference...
    }
}

然而,这对我不起作用,因为在不需要中断的情况下,它只在循环运行一次后就中断了。

【问题讨论】:

  • $ErrorActionPreference = 'Stop' 在脚本顶部捕获终止错误。
  • 是的,你想要 continue 而不是 break 在你的 catch 块。
  • $ErrorActionPreference 行就像一个魅力。去展示我对powershell的了解。谢谢!

标签: powershell exception try-catch break continue


【解决方案1】:

trap 语句仅对 terminating 错误起作用,您的代码 - 至少使用默认的 $ErrorActionPreference'Continue' - 不会 产生。

用于捕获终止错误的更灵活的替代构造是try / catch / finally 语句:

  • 这样的语句允许更细粒度的控制,而trap 适用于整个封闭范围;在实践中,trap 很少使用(现在)。

使用traptry/catch,最好使用后者。 如果你这样做了,你也需要一个try 子句——你不能只使用catch

无论哪种方式,为了让您的代码产生终止错误,您必须将Copy-Item 命令产生的终止错误提升为终止 的,使用以下方法之一:

应用于你最后的代码sn-p:

# Promote all non-terminating errors to (script-)terminating ones.
$ErrorActionPreference = 'Stop'

foreach($n in 1..9){
   
    try {
      Copy-Item "C:\Design Images for Web\PARK\PARK10$n.jpg" -Destination "C:\Design Images for Web\PARK\JunkFiles\PARK10$n.jpg" 
    } catch [System.Management.Automation.ItemNotFoundException] {
      continue # continue with the enclosing `foreach` loop 
    }
    
    foreach($m in 1..38) {
        $v = $variations[$m]
        try {
          Copy-Item "C:\Design Images for Web\PARK\PARK10$n.jpg" -Destination "C:\Design Images for Web\PARK\NewFiles\PARK10$n$v.jpg"
        } catch [System.Management.Automation.ItemNotFoundException] {
          continue # continue with the enclosing `foreach` loop 
        }
    }
}

【讨论】:

    猜你喜欢
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 2019-08-25
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    相关资源
    最近更新 更多