Do you find yourself looking for some software that could easilly rename a lot of files? And after installing a few of them and tried them you realize that it is not exactly what you are looking for? And that you just wasted your precious time? I had this exact feeling lately.
It was a nice afternoon. I was relaxing watching some tv series that I like when suddenly I got an email from my colleage: ‘Could you please add this logos to our app?’. There were about 50 jpg files. So I thought this will be a fast action. But when I examined them I found out that our client send us those files not exactly the way we agreed. We have specific naming convention for easy assets management and this was an Android app so there cannot be dashes or polish special characters. And names of those files was just wrong. So I had to change all file names.
Then I started looking for some software that could do batch renaming. And after 10 – 15 minutes I realized that it is silly :) I just got a lightbulb moment. Come on. Use PowerShell stupid.
And I did. It was fun and fast and easy :)
dir *.png |
And a little explanation:
- line 1: get all PNG files and pass them to the pipe
- line 2: the ‘%’ sign is a shortcut for ‘foreach’, so iterate over every element (from this pipe)
- line 3: the ‘ren’ is a shortcut for ‘rename’, and the first parameter is ‘old name’ and the second is ‘new name’
- line 4: the ‘ole name’ value: the ‘$_’ is the current object in the foreach loop and I am accessing the ‘Name’ property of it
- line 5: the ‘new name’ value: I am creating it using the ‘partner_’ prefix and concatenate the changed file name
- lines 6 – 17: actual renaming of files
The next time I will need to rename many files I certainly use this approach first.