PowerShell
Copy-Item
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 }
Line-By-Line
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...$THIS_FOR_THAT = @(@(".","_"), @("-","_"))
- $ precedes all variables.
- @ is the splat operator.
- In this context, it denotes the start of a list...
- of a list.
- In this context, it denotes the start of a list...
- Starting the script with this replacement list will (hopefully) make further use of this script easier...
- You know, for when my replacements need changing.
$DIR_IN = 'C:\alpha\input'
$DIR_OUT = 'C:\alpha\output'- Your needs will no doubt be different.
- I work out of the same directories time and time again.
$Items = Get-ChildItem -Path $DIR_IN
- Provides a list of all the contents of a directory.
- For this script, I am sure the existence of subdirectories in the target directory would cause problems.
- But then, I work out of the same directories (time and time again), which have no subdirectories.
- So, it's really no problem at all.
- But then, I work out of the same directories (time and time again), which have no subdirectories.
ForEach ($old_name in $items)
- A simple for loop.
- The working section is set off by curly braces...
- {}
$base = $old_name.BaseName
$ext = $old_name.Extension- Extracts the File Object's base name and extension.
- Meaning,
$old_name
is a crappy name.
- It should be something more like
$old_file
...- Or better yet
$file_in
.
- Or better yet
- Sorry, I'm not going back to change it.
- I'll likely never look at anything outside of the first line of code from this script ever again.
- Meaning,
ForEach ($replacements in $THIS_FOR_THAT)
- Another for loop.
$new, $old = $replacements
- Extracts my replacement pair.
$base = $base.Replace($new, $old)
- A simple replace method.
$new_name = $base + $ext
- Builds the new file name.
$path_in = JOIN-PATH $DIR_IN $old_name
$path_out = JOIN-PATH $DIR_OUT $new_name- Takes the file names and creates complete paths.
- And so, once again,
$file_name_in
and$file_name_out
would have made better names for the trailing variables. Copy-Item $path_in $path_out
- See, a good name tells it all.
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.