PowerShell copy latest file to 2 locations, rename at destination locations

Hi everyone,

I'm leveraging an existing PowerShell script (see below) to work in the following method:

- Only copy the latest file in the source folder to a destination and archive directory (we use a job scheduler app to run every hour).
- Do not touch or move the source
- Drop the prefix of the file which starts with "OPT_" in only the destination and archive (i.e. rename-item to OPT_File_12345.csv to File_123456)
- When the job runs again, it should know not to copy previously copied files but understands they were renamed.

I've tried using -replace to remove the prefix, but now I am coming to realization this will require additional logic. Code is seen below.... can someone help??

# Set the path for our working directories
$Source_Folder = '\\pension2\shared\PPSA Dev\Src\'
$Archive_Folder = '\\pension2\shared\PPSA Dev\Arc\'
$SFTP_Folder = '\\pension2\shared\PPSA Dev\Dst\'

# This gets a collection of all of the files in each of our working directories
$Source_Files = Get-ChildItem "$Source_Folder\*.csv"
$Archive_Files = Get-ChildItem "$Archive_Folder\*.csv"
$SFTP_Files = Get-ChildItem "$SFTP_Folder\*.csv"

$Found = $False

# This loops through each file in the source directory and checks them against both destinations
$Source_Files | ForEach-Object {
    if (!(Test-Path($_.FullName.Replace($Source_Folder, $Archive_Folder)))) {
        # This is going to output every file we don't find
        Write-Host "$_.Name not found in $Archive_Folder"

        # This will Copy every file we didn't find
        Copy-Item $_.FullName -Destination $Archive_Folder -Verbose
        Copy-Item $_.FullName -Destination $SFTP_Folder -Verbose
        
        # This is a flag we're setting, so we can simply output if no files were found
        $Found = $True
    }
}

# Output that we found no files
if (!($Found)) {
    Write-Host "No new files found."
}

Open in new window

OPT ExpertAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try it like this:
# Set the path for our working directories
$Source_Folder = '\\pension2\shared\PPSA Dev\Src\'
$Archive_Folder = '\\pension2\shared\PPSA Dev\Arc\'
$SFTP_Folder = '\\pension2\shared\PPSA Dev\Dst\'

$Found = $False
# This loops through each file in the source directory and checks them against both destinations
Get-ChildItem "$($Source_Folder)\*.csv" | ForEach-Object {
	$Target_Name = $_.Name -replace '\AOPT_'
	If (-not (Test-Path -Path "$($Archive_Folder)\$($Target_Name)")) {
		# This is going to output every file we don't find
		Write-Host "$($_.Name) not found in $($Archive_Folder)"

		# This will Copy every file we didn't find
		Copy-Item -Path $_.FullName -Destination "$($Archive_Folder)\$($Target_Name)" -Verbose
		Copy-Item -Path $_.FullName -Destination "$($SFTP_Folder)\$($Target_Name)" -Verbose
        
		# This is a flag we're setting, so we can simply output if no files were found
		$Found = $True
	}
}

# Output that we found no files
if (-not $Found) {
	Write-Host "No new files found."
}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PberSolutions ArchitectCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: oBdA (https:#a42380886)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Pber
Experts-Exchange Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.