Powershell
I just had Copilot do this for me
Nice little script that you can run each time you make changes and have it save to OneDrive or similar without having to think too much.
powershell
Copy code
# Define the source and destination folder paths
$sourceFolder = "C:\SourceFolder"
$destinationFolder = "C:\DestinationFolder"
# Check if the source folder exists
if(!(Test-Path $sourceFolder)){
Write-Host "Source folder does not exist."
exit
}
# Check if the destination folder exists
if(!(Test-Path $destinationFolder)){
Write-Host "Destination folder does not exist. Creating destination folder."
New-Item -ItemType Directory -Path $destinationFolder | Out-Null
}
# Get the current contents of the source folder
$sourceFiles = Get-ChildItem $sourceFolder -Recurse
# Get the contents of the destination folder
$destinationFiles = Get-ChildItem $destinationFolder -Recurse
# Compare the contents of the source and destination folders
$filesToCopy = Compare-Object $sourceFiles $destinationFiles -Property Name, Length -PassThru | Where-Object {$_.SideIndicator -eq "<="}
# Copy the files to the destination folder if there are changes
if($filesToCopy){
Write-Host "Copying files to destination folder."
Copy-Item $filesToCopy.FullName -Destination $destinationFolder -Force
}
else{
Write-Host "No changes detected."
}
This script first defines the source and destination folder paths. It then checks if the source folder exists and exits the script if it doesn't. Next, it checks if the destination folder exists and creates it if it doesn't.
The script then gets the current contents of the source folder and the contents of the destination folder. It compares the contents of the two folders based on the file name and length using the Compare-Object cmdlet and stores any files that are in the source folder but not the destination folder in the $filesToCopy variable.
Finally, if there are changes detected, the script copies the files to the destination folder using the Copy-Item cmdlet. If there are no changes detected, it outputs a message saying so.