Rename Files To Time Last Modified
A PowerShell Script which copies files from DIR_IN to DIR_OUT, while renaming each file to the time said file was last modified.
#My Working Directories
$DIR_IN = 'C:\alpha\input'
$DIR_OUT = 'C:\alpha\output'
#Loop Over DIR_IN
$Items = Get-ChildItem -Path $DIR_IN
ForEach ($old_name in $items)
{
#Assemble New Name
$base = $old_name.LastWriteTime
$base = $base.ToString("yyyy-MM-dd-hh-mm-ss")
$base += '-BrettRants'
$ext = $old_name.Extension
$new_name = $base + $ext
#Path In and Out
$path_in = JOIN-PATH $DIR_IN $old_name
$path_out = JOIN-PATH $DIR_OUT $new_name
#A Bit Of Feedback
$path_in
$path_out
#Make The New Copy
Copy-Item $path_in $path_out
}
To work, the above would need to be saved as a Powershell Script and run from file. And to do that, the Execution Policy needs to be reset:
Set-ExecutionPolicy Bypass -Scope Process
The only part of the above which is the least bit interesting to me is the portion entitled
#Assemble New Name, so that's the only portion I will discuss further.#Assemble New Name- Shall we comment?
- Yes, we shall.
$base = $old_name.LastWriteTime- This extracts a time object from the old file...
- Meaning,
$old_namewould have been more accurately called$file_in.- Live & Learn!
- Meaning,
$base = $base.ToString("yyyy-MM-dd-hh-mm-ss")- The time object is converted to a string in the noted format.
$base += '-BrettRants'- You may not want this part.
- I do.
$ext = $old_name.Extension- It's really handy that PowerShell Items are Complete Objects.
- I'm finding everything to be very robust.
$new_name = $base + $ext- And here we have a simple bit of string concatenation.
Bam!
And done!