Skip to content

Instantly share code, notes, and snippets.

@zoka123
Created July 17, 2015 13:59
Show Gist options
  • Save zoka123/a513dd3f6909ab84f5e6 to your computer and use it in GitHub Desktop.
Save zoka123/a513dd3f6909ab84f5e6 to your computer and use it in GitHub Desktop.
Rename .srt files to match .avi filenames
<?php
$files = scandir(__DIR__);
$pairs = array();
foreach($files as $file){
$type = null;
if (strpos($file,'.srt') !== false) {
$type = 'srt';
}
if (strpos($file,'.avi') !== false) {
$type = 'avi';
}
if($type === null){
continue;
}
preg_match("/.*(S\d+E\d+).*/", $file, $res);
if(isset($res[1])){
$pairs[$res[1]][$type] = $file;
}
}
foreach($pairs as $episode => $pair){
$oldName = $pair['srt'];
$newName = $pair['avi'];
$newName = str_replace('.avi', '.srt', $newName);
echo 'Renaming ' . $oldName . ' to ' . $newName . PHP_EOL;
rename($oldName, $newName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment