1

Here are my folders:

Dexter\Season 1\season 1 ep 1  
Dexter\Season 1\season 1 ep 1  
Dexter\Season 2\season 1 ep 1  
Dexter\Season 2\season 1 ep 1  
Dexter\Season 3\season 1 ep 1  
Dexter\Season 3\season 1 ep 1  

I am currently in Dexter\

What I want to do is loop through all the sub folders in Dexter and rename all the files accordingly:

Dexter\Season 1\season 1 ep 1 -> Dexter\Season 1\1x1

How would I go about doing this?

1
  • 1
    Windows? Linux?
    – Martin
    Jul 10, 2013 at 11:01

1 Answer 1

3

Since I'm wasting my summer inside anyways I might aswell do something helpful, and write the script for you. Assuming you're using Windows, the following PowerShell script would fit your scenario (just save as a .ps1 file and modify the path at the top).

$TVShowFolderPath = "X:\Series\Dexter";

$TVShowSeasons=$(get-childitem "$TVShowFolderPath");

foreach( $s in $TVShowSeasons)
{
    if( ($s.PSisContainer) -and ($s -imatch "^(Season )(\d{2}|\d{1})$") )
    {
        $season = $matches[2];
        write-host "Season $season";

        $episodes=$(get-childitem $s.FullName);

        foreach( $ep in $episodes)
        {
            if( $ep -imatch "^(season) (\d{2}|\d{1}) (ep) (\d{2}|\d{1})(.*)$")
            {
                $newName = "$($season)x$($matches[4])$($matches[5])";
                write-host "`tEpisode: `"$($ep.Name)`" --> `"$newName`"";
                Rename-Item $ep.FullName $newName;
            }
        }
    }
}

If you want to see if it spits out the correct names first just comment out the line starting with "Rename-Item".

3
  • Thanks for that. I was after more of a batch rather than PowerShell. I have a batch script which I am wanting to add this to. Jul 11, 2013 at 22:27
  • To launch the PowerShell script from a batch script; powershell "& '%~dp0\renameScript.ps1'" PS: Enable execution of PowerShell scripts, start PS as admin and type "Set-ExecutionPolicy RemoteSigned" (This can also be added to the batch script.
    – Martin
    Jul 11, 2013 at 22:53
  • Thanks Martin. I have been trying to enable execution of powershell. I don't know how to start PS? Aug 16, 2013 at 12:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .