Brett Code


i.e. Copy/Rename

I had a bunch of images (76 of them) that included dots in their names ('name.more_name.png' or more precisely '2019-06-12_22.14.25.png'), which I don't like. So, I wanted to rename the images prior to pushing them to my website.

First comes the script I created, followed by a line-by-line explanation.

I'm not saying it's great code. But it works. I wrote it. And that's good enough for me.

PowerShell Copy/Rename Script

This won't work on the Command Line. It has to be run from inside a PowerShell Script.

$THIS_FOR_THAT = @(@(".","_"), @("-","_")) 

$DIR_IN = 'C:\alpha\input'
$DIR_OUT = 'C:\alpha\output'

$Items = Get-ChildItem -Path $DIR_IN

ForEach ($old_name in $items)
    $base = $old_name.BaseName
    $ext = $old_name.Extension
    ForEach ($replacements in $THIS_FOR_THAT)
        $new, $old = $replacements
        $base = $base.Replace($new, $old)

    $new_name = $base + $ext
    $path_in = JOIN-PATH $DIR_IN $old_name
    $path_out = JOIN-PATH $DIR_OUT $new_name

    Copy-Item $path_in $path_out


I removed all the comments and dead-end print statements from the above code. As for something this small, they just tend to get in the way of an extended write-up. Certainly, if a comment is required to describe what a variable is for, that variable is not named very well...

The Debriefing

Between the code and the write-up, I've sunk several hours into this project... which is OK. After all, I did a quick initial search and found a one-liner that would have fit my needs... or half of my needs. I guess, I would have had to run that one-liner twice, back-to-back. Once to get rid of the dashes and another time to eliminate any periods. But that is a small thing.

Bottom line, I could have been done hours ago.

But then, what do they say?

It's not the destination.

It's the journey.

And part of my journey is making thousands upon thousands of trivial and near useless web-pages.

That said, I hope you have enjoyed... or at least, found what you were looking for.

command line image manipulations

© copyright 2019 Brett Paufler