Brett Code


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
    #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.


And done!

command line image manipulations

© copyright 2020 Brett Paufler