Skip to content

Instantly share code, notes, and snippets.

@johnnyhalife
Created April 3, 2010 04:58
Show Gist options
  • Save johnnyhalife/354130 to your computer and use it in GitHub Desktop.
Save johnnyhalife/354130 to your computer and use it in GitHub Desktop.
rename-series
function capitalize {
$result = ""
$args[0].trim().split(" ") | %{ ($_.trim()[0].toString().toUpper()) + $_.substring(1)} | %{ $result += $_ + " "}
return $result.trim()
}
function namify {
$currentName = $args[0].name -replace [regex]"(wWw.*|notv|hdtv.*).avi", [String]::Empty
$segments = ([regex]"(.*).[s|S](\d+)[e|E](\d+).*").matches($currentName)
$name = $segments |
%{ $_.groups[1].value } |
%{ $_ -replace "FF", "Flashforward"} | %{$_ -replace "tbbt", "the.big.bang.theory"} | %{$_ -replace "FlashForward", "Flashforward"} | %{$_ -replace "TIC", "the.it.crowd"} |
%{ [regex]::replace($_, "\.(\w)", ' $1') } |
%{ $_.toString().toLower() }
if(-not [string]::isNullOrEmpty($name)){
$season = $segments | %{ $_.groups[2]}
$episode = $segments | %{ $_.groups[3]}
$filename = "$name-s$season" + "e$episode"
$foldername = capitalize($name)
# extension is now read from the actual file instead of guessed to AVI
$extension = [regex]::match($args[0], ".*(avi|mkv)$").groups[1].value
mkdir $foldername -force | %{}
mv $_.FullName "./$foldername/$filename.$extension"
}
}
#added support for MKV (HD) files
ls | where{[regex]::match($_, ".*.(avi|mkv)$").success} | %{ namify($_) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment