Need Script for Syncing Folders

I’m still very new to Powershell & very much at the beginning stages. But my company needs a script that will move files from one folder to another and as a cleanup option, delete files that are more than 7 days old. This will need to run very frequently (once a minute?) to serve the purpose.

Here is a quick rundown of what we’re doing. We our separating two of our networks and are going to use a ‘jumpbox’ that will have access to both networks. Inside of each network, we have shares set up, an “in” folder and an “out” folder. The jumpbox will sync the ‘in’ box of network 1 with the ‘out’ box of network 2 and sync the ‘in’ box of network 2 with the ‘out’ box of network 1. We want the files to get moved from the ‘in’ box to the ‘out’ box. And then delete any file that’s more than 7 days old as a cleanup feature.

I know this can be done with a simple Robocopy and some extra commands, but a Powershell script would be far more elegant & specific solution.

Have you by chance done any research prior to this post? If you are looking for someone to write a script for this scenario you might be out of luck. You are more likely to get some answers if you research a bit, bring a rough draft of code and start asking questions from there.

Here are some links to get you started.

For your scheduled task
New-ScheduledTask (ScheduledTasks) | Microsoft Learn
Setting your threshold of time and run frequency
New-ScheduledTaskTrigger (ScheduledTasks) | Microsoft Learn
Checking the creation time on files in the directory among other properties
Get-ChildItem (Microsoft.PowerShell.Management) - PowerShell | Microsoft Learn

Mark,
Welcome to the forum. :wave:t4:

I can only echo what @PoshDad said - This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request. And you didn’t even asked a question … :smirk:

We actually expect you to make an own attempt at the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

Regardless of that …

I completely disagree. Robocopy is an industry standard tool that is made for only one purpose - to copy or move files either locally or over network connections. And it would very likely outperform every sophisticated PowerShell script you could come up with. Especially when you want to run it with such a short interval. It can even monitor the source and run again when more changes are detected than specified. :point_up_2:t4:

Have you considered other technologies as well? For example DFS-R?

I’d like to urgently encourage you to reconsider your plan and maybe get help from an IT professional experienced with the topic. Especially when the solution you’re trying to implement is crucial for your enterprise.

My scripting abilities are still far too beginner to even attempt a script like this. Hence why I was asking for help from someone who’s already proficient and could easily put one together for me.

I didn’t ask a scripting question because I don’t have a script already going to ask about.

I’ve considered Robocopy, but it’s designed to mirror two folders, not simply move from one folder to another. And then there’s the issue where I still need it to delete files that have been there for 7 days. These are tasks I thought would be more easily handled and more centrally handled by a single powershell script.

That’s actually exactly my point - no - it is not. You may carefully re-read the help for robocopy again. It is a highly optimized tool to do file transfers - multithreaded if you want. And you can copy or move files. You can even use it to delete files. :wink:

1 Like

Here is a PowerShell script that will do what you described:

Source and destination paths

$sourcePath = “\server1\in”
$destPath = “\server2\out”

Delete files older than 7 days

$daysBack = 7

Get current date/time

$now = Get-Date

Get files in source folder

$files = Get-ChildItem $sourcePath

Move files from source to destination

foreach ($file in $files) {
# Skip files that were modified within the last 7 days
if ($now.Subtract($file.LastWriteTime).TotalDays -lt $daysBack) {
continue
}

# Move the file 
Move-Item -Path "$sourcePath\$($file.Name)" -Destination $destPath 

}

Get files in destination folder

$destFiles = Get-ChildItem $destPath

Delete files older than 7 days

foreach ($file in $destFiles) {
# Skip files that were modified within the last 7 days
if ($now.Subtract($file.LastWriteTime).TotalDays -lt $daysBack) {
continue
}

# Delete the file 
Remove-Item -Path "$destPath\$($file.Name)" 

}

This script will:

  • Get all files in the source path
  • Move any files not modified in the last 7 days to the destination
  • Get all files in the destination path
  • Delete any files not modified in the last 7 days
  • This will run on a schedule to keep moving and deleting files

On the other hand, tools like Freefilesync , Allwaysync , Gs Richcopy 360 and Syncback can do these jobs very easily

Adrian,
Welcome to the forum. :wave:t3:

Please … when you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink: