如果存在,如何将单个文件复制到多个目标

How can I copy a single file to Multiple Destinations if they exist

我有一个 Adob​​e AE 脚本,必须安装在安装文件夹 ex 中。 "C:\Program Files\Adobe\Adobe After Effects CC 2017\Support Files\Scripts\"

我们的用户安装了多个版本的 AE,每个版本都需要脚本。理想情况下,我想要的是一个脚本,可以将 "script.jsx" 复制到目标文件夹(如果存在)。

我尝试使用下面的方法,但无法检查目的地是否存在。

"C:\Program Files\Adobe\Adobe After Effects CC 2018\Support Files\Scripts\","C:\Program Files\Adobe\Adobe After Effects CC 2017\Support Files\Scripts\","C:\Program Files\Adobe\Adobe After Effects CC 2016\Support Files\Scripts\" | 
ForEach-Object {Copy-Item $dirFiles\rd_RenderLayers.jsx -Recurse -Destination $_}

这个我也试过了

if ( $(Try { Test-Path "C:\Program Files\Adobe\Adobe After Effects CC 2014\Support Files\Scripts\".trim() } Catch { $false }) ) {
   Copy-Item -Path C:\Users\Desktop\rd_renderlayers_v3.1\rd_RenderLayers.jsx -Destination C:\Program Files\Adobe\Adobe After Effects CC 2014\Support Files\Scripts\
 }
 if ( $(Try { Test-Path "C:\Program Files\Adobe\Adobe After Effects CC 2015\Support Files\Scripts\".trim() } Catch { $false }) ) {
   Copy-Item -Path C:\Users\Desktop\rd_renderlayers_v3.1\rd_RenderLayers.jsx -Destination C:\Program Files\Adobe\Adobe After Effects CC 2015\Support Files\Scripts\
 }
 if ( $(Try { Test-Path "C:\Program Files\Adobe\Adobe After Effects CC 2016\Support Files\Scripts\".trim() } Catch { $false }) ) {
   Copy-Item -Path C:\Users\Desktop\rd_renderlayers_v3.1\rd_RenderLayers.jsx -Destination C:\Program Files\Adobe\Adobe After Effects CC 2016\Support Files\Scripts\
 }
 if ( $(Try { Test-Path "C:\Program Files\Adobe\Adobe After Effects CC 2017\Support Files\Scripts\".trim() } Catch { $false }) ) {
   Copy-Item -Path C:\Users\Desktop\rd_renderlayers_v3.1\rd_RenderLayers.jsx -Destination C:\Program Files\Adobe\Adobe After Effects CC 2017\Support Files\Scripts\
 }
 if ( $(Try { Test-Path "C:\Program Files\Adobe\Adobe After Effects CC 2018\Support Files\Scripts\".trim() } Catch { $false }) ) {
   Copy-Item -Path C:\Users\Desktop\rd_renderlayers_v3.1\rd_RenderLayers.jsx -Destination C:\Program Files\Adobe\Adobe After Effects CC 2018\Support Files\Scripts\
 }
 if ( $(Try { Test-Path "C:\Program Files\Adobe\Adobe After Effects CC 2019\Support Files\Scripts\".trim() } Catch { $false }) ) {
   Copy-Item -Path C:\Users\Desktop\rd_renderlayers_v3.1\rd_RenderLayers.jsx -Destination C:\Program Files\Adobe\Adobe After Effects CC 2019\Support Files\Scripts\
 }
foreach ($dest in 'there2','there3') { cp there $dest }

您需要在 ForEach-Object 中使用 Test-Path -LiteralPath $_ 创建一个条件,以限制复制到那些存在的输入目录:

$dirs | 
  ForEach-Object {
    if (Test-Path -LiteralPath $_) {
      Copy-Item $dirFiles\rd_RenderLayers.jsx -Destination $_
    }
  }

请注意,我已删除 -Recurse,因为它在您复制单个文件时不适用。