Powershell: Rename Files in Bulk

Howdy folks,

If you’re like me and have ever digitized a bunch of media (in this specific case, the first couple seasons of It’s Always Sunny in Philadelphia from DVDs) you’ve probably run across problems getting applications such as Plex to properly classify episodes and end up with something that looks like this:

Despite totally reasonable file names like “Its Always Sunny in Philadelphia Season 03 Episode 02 “, PLEX is not able to correctly parse the names out. To fix this, we are going to leverage the Powershell CMDlets Get-ChildItem and pipe that to Rename-Item. Easy, right?

Let’s use the following as a base to edit, replacing the “old” phrase with the “new” one:

Get-ChildItem -filter "*old*" -recurse | Rename-Item -NewName {$_.name -replace 'old','new'}

That little snippet should crawl through the current directory (and those under it) and rename the old phrase to the new. That “|” character in there takes the output of the first half and pipes it to the other half…

  • Get-ChildItem -filter “*old*”
    • This grabs files with the “old” anywhere in the filename – even if it occurs multiple times.
  • -recurse
    • This actually allows the cmdlet to dig into child folders – super handy if you wanted to, say, knock out an entire series rather than going one folder at a time. Depending on your show structure you may not be able to do this, which is why I left it out later.
  • Rename-Item -NewName
    • Renames an item
    • Note: you can throw on a -whatif parameter to get an output of files that would be changed
  • {$_.name -replace ‘old’,’new’}
    • This tells Rename-Item to rename the string “old” with “new”

Using this knowledge, we can transform this script into something like:

Get-ChildItem -Filter '*Season 03 Episode *' | Rename-Item -NewName {$_.name -replace 'Season 03 Episode ','s03e'}

In this example, we are only selecting “Season 03 Episode” to retain the 03. This allows us to change our naming scheme to s03e03!

Using -whatif we are able to see our effects without breaking anything:

PS Microsoft.PowerShell.Core\FileSystem::\nfs-01\Media SMB Share\tv\It's Always Sunny in Philadelphia\Season 3> Get-ChildItem
-Filter '*Season 03 Episode *' | Rename-Item -NewName {$_.name -replace 'Season 03 Episode ','s03e'} -whatif
What if: Performing the operation "Rename File" on target "Item: \nfs-01\Media SMB Share\tv\It's Always Sunny in Philadelphia\Season 3\Its Always Sunny in Philadelphia Season 03 Episode 04 - The Gang Gets Held Hostage.avi Destination: \nfs-01\Media SMB Share\tv\It's Always Sunny in Philadelphia\Season 3\Its Always Sunny in Philadelphia s03e04 - The Gang Gets Held Hostage.avi".

 

That’s all! A pretty simple command that’s exceptionally flexible at renaming things from production data to your favorite TV show.

Using LEDE / OpenWRT Firmware with PPPoE and VLANs

Growing Pains

I wanted to share some thoughts I had on flashing LEDE (the OpenWRT successor) to my WRT1900AC, and share some guidance on configuring the WAN interfaces for PPPoE and setting up VLAN tagging. While I use Century Link, this guide should be pretty applicable to anyone who has an ISP which uses PPPoE and VLAN tagging to lock down their modems.

A few months ago I was presented with an opportunity to upgrade my homelab to Century Link’s gigabit offerings – and while the service was immediately better than Comcast’s, I noticed that my (now aging) Linksys WRT1900AC v1 router running OpenWRT 15.05.1 barely broke 300mbps, whereas my much older Netgear router was able to run at around 900mbps. After doing some digging I discovered the the OpenWRT firmware was the culprit and promptly returned back to stock. While I got my full speeds back, sacrificing the ability to make a litany of software changes was a pretty big trade off, and as someone who’s more comfortable working with BIND over AD DNS it became kind of a pain in the ass to manage DNS in my lab.

Earlier this evening, I came across the LEDE project, which was effectively a continuation of OpenWRT, and better yet, they even had an updated system image for my router. Grabbing the image (available here for those of you with the same router as I), I flashed my router to LEDE and got up and running…

How-To

Prior Info

Please make sure you have the following:

  • Your PPPoE credentials from your service provider, as well as the VLAN (Century Link uses 201, which I will be using in this guide). Most of them will give them to you if you call technical support.
  • A router that is compatible with LEDE. Check https://lede-project.org/supported_devices to see if your router is on there.
  • A router that you have already flashed LEDE to. I won’t be covering how to flash the firmware to your device, but this guide will assume you have already done so and done minor set up.

VLAN Config

  1. Log in to your LuCI interface (normally http://192.168.1.1), and click Network -> Interfaces.
  2. By default, you should see a bridged LAN interface called LAN, WAN, and WAN6.
  3. Verify that you have a WAN entry, and click on  Edit, and then the Physical Settings tab. This will reveal which physical eth port (eth1 in my case) your interface is using, and will look something like this:
  4. Navigate to Network -> Switch. Here we will create a new VLAN ID 201 and tag CPU (eth1) and WAN:
  5. Click Save & Apply.

Interface Config

  1. Go back to Network -> Interfaces.
  2. Click Edit on WAN.
  3. Select Protocol and set it to PPPoE. Click Switch Protocol to confirm your changes:
  4. At this point, enter your PPPoE Credentials into the PAP/CHAP Username and PAP/CHAP Password fields. Leave the other fields empty:
  5. Click the Physical Settings tab, and select the VLAN we just created:
  6. As you can see, the new physical interface name for our new VLAN is eth1.201! Click Save & Apply and go back to the Interface Overview.
  7. Click Connect to bring the interface online. You should see an IPv4 address at this point! If you have an ISP which supports IPv6 repeat the same steps on that interface.

 

I hope this post helps someone. I did a lot of running around trying to figure this out on my last go at this, and didn’t have this info on-hand.

New Website Design!

Sorry for the mess, I’ve been updating my design and working on several other projects at the same time. This blog is going to serve two parts – first, a repository of technical notes for myself, which I want to share with the community in hopes that other engineers are able to learn from my trial and error – and second, to deliver well designed technical articles to help you succeed with the IT portion of your business. I look forward to the upcoming journey, and thanks for viewing!

– Zack