0

I want to copy a file to multiple destinations using a script that filters through a directory and selects the newest file in the $File_path then change its name and copies it to the $destination, the script i'm using is this:

 $File_path = "C:\TEMP\export\liste\Text_Utf8\"
 $destination = "C:\TEMP\export\C7E001"
get-childitem -path $File_path -Filter "Ges?*.txt" | 
    where-object { -not $_.PSIsContainer } | 
    sort-object -Property $_.CreationTime | 
    select-object -last 1  | copy-item -Destination (join-path $destination "FRER3000CCFETES01_IN.DEV")

this only copies it to one location, is there a way to improve it to copy the same file to multiple locations? i have seen this thread but it seems different.

the other locations are as follow:

C:\TEMP\export\C7P001 C:\TEMP\export\C7F001 C:\TEMP\export\C7S001 and so on.

thank you.

RJ96
  • 303
  • 4
  • 13
  • But where are your 'multiple destinations'? All we see is you copy a file to a single destination `C:\TEMP\export\C7E001` – Theo Sep 28 '20 at 08:17
  • These are my other destenations: C:\TEMP\export\C7P001 C:\TEMP\export\C7F001 C:\TEMP\export\C7S001, how to implement them in the script is what i need help with, i have tried making an array of folders, then iterates through each one but it didn't work because i didn't manage to implement it so that the selection of the newest file and renaiming it still works. – RJ96 Sep 28 '20 at 08:22
  • 1
    If you want the latest file, then better use `LastWriteTime` instead of `CreationTime` – Theo Sep 28 '20 at 08:25
  • Thank you @Theo, i will try that, i am interested in seeing your solution to this problem, any help would be appreciated :) – RJ96 Sep 28 '20 at 08:47

2 Answers2

1

You can use an foreach object loop

$File_path = "C:\TEMP\export\liste\Text_Utf8\"
$destination = "C:\TEMP\export\C7E001", "C:\TEMP\export\C7P001", "C:\TEMP\export\C7F001", "C:\TEMP\export\C7S001"
$Files = get-childitem -path $File_path -Filter "Ges?*.txt" | 
    where-object { -not $_.PSIsContainer } | 
    sort-object -Property $_.CreationTime | 
    select-object -last 1 
$Destination | Foreach-Object {copy-item $Files -Destination (join-path $_ "FRER3000CCFETES01_IN.DEV")}
  • Thank you for your reply, i have tried that rn, it is showing me an error that in this line `{copy-item $Files -Destination (join-path $_ "FRER3000CCFETES01_IN.DEV")}` the path **G:\Ges0888_Utf8.TXT**does not exist! where did we use this path? – RJ96 Sep 28 '20 at 08:45
1

Although my answer isn't very different to Peter's answer, This uses the LastWriteTime property to get the latest file and uses the FullName property of the file to copy in the Copy-Item cmdlet.

$File_path    = "C:\TEMP\export\liste\Text_Utf8"
$destinations = "C:\TEMP\export\C7E001", "C:\TEMP\export\C7F001", "C:\TEMP\export\C7S001"
$fileToCopy   = Get-ChildItem -Path $File_path -Filter "Ges*.txt" -File | 
                Sort-Object -Property $_.LastWriteTime | 
                Select-Object -Last 1

foreach ($dest in $destinations) {
    Copy-Item -Path $fileToCopy.FullName -Destination (Join-Path -Path $dest -ChildPath "FRER3000CCFETES01_IN.DEV")
}
Theo
  • 57,719
  • 8
  • 24
  • 41