jeudi 15 novembre 2018

Replacing 2nd occurrence of "-" with "_" using Powershell

I've been using Powershell to simplify repetative tasks in creating directories, renaming and moving files. I'm working with Video and PDF files where the required syntax of the filename is very specific. So far I've been able to correct all of the common errors I run across, but this one has me stumped.

The correct syntax for my files would include:

01A-50_02A-50-CIPP-PRE.MP4

01AA-50_02AA-50-CIPP-PNSL.PDF

W01AA-48_02AA-48-CIPP-PST-CMP.MPG

I'm receiving a large amount of files that look like this:

01A-50-02A-50-CIPP-PRE.MP4

01AA-50-02AA-50-CIPP-PNSL.PDF

W01AA-48-02AA-48-CIPP-PST-CMP.MPG

I need to replace the second dash with an underscore while keeping the other dashes unaffected. I can otherwise do this in bulk with some help from excel, but I was hoping to have a short code that could find and correct this error in syntax without having to export a list to excel, use text to columns, and then concatenate the alphanumeric portions back together. I also don't want to correct all these filenames manually.

From what I've researched, it's not possible to target a specific occurrence of a character to replace. the closest I've come to thinking I'd found a solution involved REGEX and identifying and replacing a pattern. I haven't been able to do anything constructive with that.

The way I would use this code would be by opening the folder that contains the missnamed files, opening a Powershell window there, copying the code from a txt file on my desktop, and pasting it into Powershell.

Any help on this would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire