Created
December 14, 2022 12:15
-
-
Save mlask/c1a2ff22ff09a313bdf03fbbd7bb1d6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
new class | |
{ | |
public function __construct () | |
{ | |
$args = explode("\n", trim(file_get_contents('php://stdin'))); | |
$carg = count($args); | |
$this->log('===== Action started === %d path(s) to process =====', $carg); | |
foreach ($args as $i => $name) | |
{ | |
$bname = basename($name); | |
if ((is_dir($name) && (preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $bname) || preg_match('/^_.+?$/', $bname))) || | |
preg_match('/^\..+?$/', $bname) || | |
preg_match('/^.+?\.(part|download|crdownload)$/', $bname)) | |
{ | |
$this->log('[%d/%d] Path "%s" ignored for some reason', $i + 1, $carg, $name); | |
continue; | |
} | |
if (file_exists($name)) | |
{ | |
if (filectime($name) > 0) | |
{ | |
$date = date('Y-m-d', filectime($name)); | |
if (!file_exists(dirname($name) . '/' . $date)) | |
mkdir(dirname($name) . '/' . $date, 0777, true); | |
if (file_exists(dirname($name) . '/' . $date . '/' . $bname)) | |
{ | |
$this->log('[%d/%d] File "%s" already exists, trying to rename...', $i + 1, $carg, dirname($name) . '/' . $date . '/' . $bname); | |
$path = pathinfo($bname); | |
$fnum = 1; | |
while (file_exists(dirname($name) . '/' . $date . '/' . $bname)) | |
$bname = rtrim(sprintf('%s (%d).%s', $path['filename'], $fnum ++, $path['extension']), '. '); | |
} | |
rename($name, dirname($name) . '/' . $date . '/' . $bname); | |
$this->log('[%d/%d] Moved "%s" to "%s"', $i + 1, $carg, $name, dirname($name) . '/' . $date . '/' . $bname); | |
} | |
else | |
$this->log('[%d/%d] No filectime found for "%s"', $i + 1, $carg, $name); | |
} | |
else | |
$this->log('[%d/%d] File "%s" not found', $i + 1, $carg, $name); | |
} | |
} | |
private function log (...$args) | |
{ | |
file_put_contents(dirname(__FILE__) . '/folderfix.log', date('Y-m-d H:i:s') . "\t" . (count($args) > 1 ? vsprintf(array_shift($args), $args) : array_shift($args)) . "\n", FILE_APPEND); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment