Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
1  
Windows? Linux? –  Martin Jul 10 '13 at 11:01
add comment

1 Answer 1

up vote 1 down vote accepted

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".

share|improve this answer
    
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. –  user1087185 Jul 11 '13 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 '13 at 22:53
    
Thanks Martin. I have been trying to enable execution of powershell. I don't know how to start PS? –  user1087185 Aug 16 '13 at 12:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.