Search code examples
powershellfor-loopcopy-item

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


I have a script for Adobe AE that has to be installed in the installation folder ex. "C:\Program Files\Adobe\Adobe After Effects CC 2017\Support Files\Scripts\"

We have users with multiple installed versions of AE and will require the script in each. Ideally what I would like for is a script that would copy "script.jsx" to the destination folders if they exist.

I tried using the below but it fails to check if the destination exists.

"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 $_}

I have also tried this

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\
 }

Solution

  • You need to create a conditional with Test-Path -LiteralPath $_ inside your ForEach-Object to limit copying to those input directories that exist:

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

    Note that I've removed -Recurse, because it doesn't apply when you're copying a single file.