I got a few foreach loops, where I am doing some if and elseif's in. They're basically set to sort out a bunch of files based on their name. But I got a particular case, where there are a few files, that aparantly exist in 2 different locations. But I want to keep the newest file always.
My code looks like this
# Variable for destination folder
$CreoRod = "C:\Test\Creo"
# Variable for source folder
$97MappeRod = "C:\Test\97"
# Variable to get each item in source folder
$97Files = Get-ChildItem -File -Path $97MappeRod
# Variable to get each item in destination folder
$CreoRodFiles = Get-ChildItem -File -Path $CreoRod
# Foreach loop to move files from source folder to corresponding destination folder
foreach ($file in $97Files) {
if ($file -like "0*") {
Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\0xxx"
} elseif ($file -like "48*") {
Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\48xxx"
} elseif ($file -like "49*") {
Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\49xxx"
} elseif ($file -like "97*") {
Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\97xxx"
} elseif ($file -like "98*") {
Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\98xxx"
} elseif ($file -like "99*") {
Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\99xxx"
} elseif ($file -notmatch "config.pro") {
Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\Diverse"
}
}
Is it possible to do something to make it compare with existing file, and if the existing file is newer, then delete the current. But if it is older then replace?