Skip to content

Instantly share code, notes, and snippets.

@halr9000
Created April 7, 2024 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halr9000/e1ca4ee17b597a03bb92de63ef5196dc to your computer and use it in GitHub Desktop.
Save halr9000/e1ca4ee17b597a03bb92de63ef5196dc to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Moves files to a destination folder based on their creation date.
.DESCRIPTION
This function moves files to a specified destination folder. The destination folder is structured by the file's creation date (yyyy-MM-dd). If the destination folder for a specific date does not exist, it will be created.
.PARAMETER Path
The file(s) to be moved. This parameter accepts pipeline input.
.PARAMETER Destination
The path to the destination folder where files will be moved.
.EXAMPLE
PS C:\> Get-ChildItem -Path "C:\SourceFolder" | Move-FilesByCreationDate -Destination "C:\DestinationFolder"
This example gets all files from "C:\SourceFolder" and moves them to "C:\DestinationFolder", organizing them by their creation date.
#>
function Move-ItemByCreationDate {
param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[System.IO.FileInfo]$Path,
[Parameter(Mandatory=$true)]
[string]$Destination
)
process {
# Convert the file's creation time to a string formatted as "yyyy-MM-dd"
$creationDate = $Path.CreationTime.ToString('yyyy-MM-dd')
# Combine the destination folder path with the creation date to form the new path
$destinationPath = Join-Path -Path $Destination -ChildPath $creationDate
# Check if the destination path does not exist
if (-not (Test-Path -Path $destinationPath)) {
# Create the destination directory if it does not exist
New-Item -Path $destinationPath -ItemType Directory | Out-Null
}
# Move the file to the destination path
Move-Item -Path $Path.FullName -Destination $destinationPath
}
}
@halr9000
Copy link
Author

halr9000 commented Apr 7, 2024

I love that Perplexity oneshot nailed my request, and almost exactly as I would've written it myself! I took that code, had Phind document it in vscode, and boom. So much time saved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment