Skip to content

Instantly share code, notes, and snippets.

@kiler129
Created January 15, 2016 14:22
Show Gist options
  • Save kiler129/7a3f40cdb107462adaee to your computer and use it in GitHub Desktop.
Save kiler129/7a3f40cdb107462adaee to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
if(!isset($argv[1])) die("No prefix!\n");
define('DRY_RUN', (!isset($argv[2]) || $argv[2] !== '-r'));
define('F_PREFIX', (empty($argv[1])) ? '' : ($argv[1] . '.'));
if(DRY_RUN) echo "-- DRY RUN --\n\n";
$fi = new FilesystemIterator('./');
$ri = new RegexIterator($fi, '/(?:(?:(\d?\d)x(\d\d))|(?:s(\d\d)e(\d\d))).*?\.([a-z]{3,4})$/i', RegexIterator::GET_MATCH);
foreach($ri as $f) {
if(empty($f[1])) {
$s = $f[3];
$e = $f[4];
} else {
$s = $f[1];
$e = $f[2];
}
if($s < 10 && $s[0] !== '0') $s = "0$s";
if($e < 10 && $e[0] !== '0') $e = "0$e";
$nf = sprintf('%sS%sE%s.%s', F_PREFIX, $s, $e, $f[5]);
$f = $fi->current()->getFilename();
echo $f . "\t=>\t" . $nf . "\n";
if(!DRY_RUN) rename($f, $nf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment