Copy Only The Updated or Modified Files – PowerShell and xcopy on Windows

PowerShell script for this task that will copy only the updated or modified files from a source directory to a destination directory:

$source = “C:\path\to\source\folder”
$destination = “D:\path\to\backup\folder”

#Get the list of files in the source folder

$sourceFiles = Get-ChildItem -Path $source -Recurse

foreach ($file in $sourceFiles) {
# Build the full destination path
$destinationPath = $file.FullName.Replace($source, $destination)

# Check if the destination file exists, and compare the last modified time
if (Test-Path $destinationPath) {
    $sourceFileTime = (Get-Item $file.FullName).LastWriteTime
    $destinationFileTime = (Get-Item $destinationPath).LastWriteTime

    # If the source file is newer or doesn't exist in the destination, copy it
    if ($sourceFileTime -gt $destinationFileTime) {
        Write-Host "Copying modified file: $file"
        Copy-Item $file.FullName -Destination $destinationPath -Force
    }
}
else {
    # If the file doesn't exist in the destination folder, copy it
    Write-Host "Copying new file: $file"
    Copy-Item $file.FullName -Destination $destinationPath -Force
}

}

How it works:

  1. Source and Destination: You define the source folder and the destination folder.
  2. File Comparison: It checks each file’s last modified timestamp in the source and destination folders. If the source file is newer or if the file doesn’t exist in the destination, it copies the file over.
  3. Recurse: It will process files recursively in subfolders.

How to run:

  1. Replace the paths in $source and $destination with your actual folder paths.
  2. Save the script as IncrementalBackup.ps1.
  3. Run the script from PowerShell.

This script provides a basic way to backup only updated or modified files. If you need additional features (like excluding specific file types, logging, etc.), the script can be expanded to fit those needs.

Here’s a batch script that you can paste into Notepad and save as a .bat file. This script will perform the incremental backup, copying only modified or new files from the source folder to the destination folder.

Batch Script for Incremental Backup:

@echo off
set source=C:\path\to\source\folder
set destination=D:\path\to\backup\folder

:: Create the destination folder if it doesn’t exist
if not exist “%destination%” mkdir “%destination%”

:: Copy modified or new files
xcopy “%source%*.*” “%destination%\” /D /E /H /Y

echo Backup complete.
pause

Explanation:

  • xcopy Command:
    • /D ensures only files that are newer in the source folder (or do not exist in the destination) are copied.
    • /E copies all subdirectories, including empty ones.
    • /H copies hidden and system files as well.
    • /Y suppresses confirmation prompts during copying.
  • Create Destination Folder: The script checks if the destination folder exists, and if not, it creates it using mkdir.

Steps to Use:

  1. Open notepad
  2. Paste the script above into the Notepad.
  3. Modify the paths for source and destination folders according to your needs.
  4. Save the file as IncrementalBackup.bat (make sure to select “All Files” in the “Save as type” dropdown).
  5. Run the .bat file by double-clicking it.

Steps to Schedule the Batch File:

  1. Open Task Scheduler:
    • Press Windows + R, type taskschd.msc, and hit Enter. This opens Task Scheduler.
  2. Create a New Task:
    • In the Task Scheduler window, click Create Basic Task on the right side under “Actions.”
  3. Name the Task:
    • Give your task a name, like Incremental Backup, and click Next.
  4. Choose the Trigger:
    • Choose when you want the backup to run. For example:
      • Daily: To run the task every day.
      • Weekly: To run it once a week.
      • Monthly: To run it once a month.
    • Select your preferred schedule and click Next.
  5. Set the Start Time:
    • Set the time you want the task to start (like at night or when you’re not using the computer). Click Next.
  6. Choose “Start a Program”:
    • In the next screen, select Start a program and click Next.
  7. Browse for the Batch File:
    • Click Browse… and locate the .bat file you saved (e.g., IncrementalBackup.bat).
    • Select it and click Open.
  8. Finish the Setup:
    • Click Next, review the details, and then click Finish to schedule the task.

Optional Settings (for Advanced Control):

If you need more control over when or how the task runs (e.g., only when the computer is idle or running on AC power), you can choose Create Task instead of Create Basic Task and configure these additional settings in the General, Triggers, and Conditions tabs.

Edit or Delete Scheduled Task:

  • If you need to change or remove the task later, return to Task Scheduler, locate your task under Task Scheduler Library, and either edit or delete it.

Leave a Reply

Your email address will not be published. Required fields are marked *