Skip to content

Instantly share code, notes, and snippets.

@alekrutkowski
Created May 22, 2023 08:49
Show Gist options
  • Save alekrutkowski/db0fb0a383b7a0bc200b8dde45b36bd7 to your computer and use it in GitHub Desktop.
Save alekrutkowski/db0fb0a383b7a0bc200b8dde45b36bd7 to your computer and use it in GitHub Desktop.
Copy file with timestamp prefixed to its name before saving in the destination (Date and Time when File Created)
## Code generated by the free-access ChatGPT May 12 Version
## Usage example:
# Copy-FileWithTimestampPrefix -SourceFilePath "F:\PRIVATE\M4ROOT\CLIP\C0040.MP4" -DestinationFolderPath "B:\Videos"
## File copied to: B:\Videos\2023-05-17_15.43.50__C0043.MP4
function Copy-FileWithTimestampPrefix {
param (
[Parameter(Mandatory = $true)]
[ValidateScript({Test-Path $_ -PathType 'Leaf'})]
[string]$SourceFilePath,
[Parameter(Mandatory = $true)]
[string]$DestinationFolderPath
)
$SourceFile = Get-Item -Path $SourceFilePath
$TimestampPrefix = $SourceFile.CreationTime.ToString("yyyy-MM-dd_HH.mm.ss_")
$DestinationFilePath = Join-Path -Path $DestinationFolderPath -ChildPath "$TimestampPrefix`_$($SourceFile.Name)"
Copy-Item -Path $SourceFilePath -Destination $DestinationFilePath
Write-Output "File copied to: $DestinationFilePath"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment