Created
April 7, 2024 21:18
-
-
Save halr9000/e1ca4ee17b597a03bb92de63ef5196dc to your computer and use it in GitHub Desktop.
This file contains 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
<# | |
.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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.