Created
October 16, 2024 00:58
-
-
Save turnerd18/3ca00619b9d14633b0d5e89c5f005038 to your computer and use it in GitHub Desktop.
Windows move files before date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Define the source and destination folder paths | |
| $sourceFolder = "C:\Path\To\SourceFolder" | |
| $destinationFolder = "C:\Path\To\DestinationFolder" | |
| # Define the cutoff date for file modification (files older than this date will be moved) | |
| $cutoffDate = Get-Date "2023-01-01" | |
| # Create the destination folder if it doesn't exist | |
| if (-not (Test-Path -Path $destinationFolder)) { | |
| New-Item -ItemType Directory -Path $destinationFolder | |
| } | |
| # Get all files in the source folder and its subfolders | |
| $files = Get-ChildItem -Path $sourceFolder -Recurse -File | |
| # Move files that were last modified before the cutoff date | |
| foreach ($file in $files) { | |
| if ($file.LastWriteTime -lt $cutoffDate) { | |
| # Define the destination path for the file | |
| $destinationPath = Join-Path -Path $destinationFolder -ChildPath $file.FullName.Substring($sourceFolder.Length) | |
| # Create the subdirectory in the destination folder if it doesn't exist | |
| $destinationDir = Split-Path -Path $destinationPath | |
| if (-not (Test-Path -Path $destinationDir)) { | |
| New-Item -ItemType Directory -Path $destinationDir | |
| } | |
| # Move the file to the destination | |
| Move-Item -Path $file.FullName -Destination $destinationPath | |
| } | |
| } | |
| Write-Host "Files moved successfully!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment