<?php
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
class RenameService
|
|
{
|
|
public function rename($baseDir = "/Volumes/N/v/美腿骇客 Leghacker&Leglegs-TLoB/")
|
|
{
|
|
if (!is_dir($baseDir)) {
|
|
return;
|
|
}
|
|
$this->parseDir($baseDir);
|
|
|
|
}
|
|
|
|
public function yaRename($baseDir = "/Volumes/intel660p/image/xg/zz/")
|
|
{
|
|
if (!is_dir($baseDir)) {
|
|
return;
|
|
}
|
|
$this->yaParseDir($baseDir);
|
|
|
|
}
|
|
|
|
private function parseDir($dir)
|
|
{
|
|
if (!is_dir($dir)) {
|
|
return;
|
|
}
|
|
$files = scandir($dir);
|
|
foreach ($files as $file) {
|
|
// dump($file);
|
|
if ($file == "." || $file == ".." || $file == ".DS_Store" || $file == ".tmp.drivedownload") {
|
|
continue;
|
|
}
|
|
if (is_dir($dir . "/" .$file)) {
|
|
$this->parseDir($dir . "/" . $file);
|
|
}
|
|
if (is_file($dir . "/" .$file) && (pathinfo($file, PATHINFO_EXTENSION) == "JPG" || pathinfo($file, PATHINFO_EXTENSION) == "jpg" || pathinfo($file, PATHINFO_EXTENSION) == "jpeg" || pathinfo($file, PATHINFO_EXTENSION) == "mp4")) {
|
|
echo $dir;
|
|
echo "\n";
|
|
echo $file;
|
|
$dirList = explode("/", $dir);
|
|
$filePrefix = array_pop($dirList);
|
|
if (!str_contains($file, $filePrefix)) {
|
|
rename($dir . "/" . $file, $dir. "/". $filePrefix. "-" . $file);
|
|
}
|
|
echo "\n";
|
|
// exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function yaParseDir($dir, $starter = "zz") {
|
|
if (!is_dir($dir)) {
|
|
return;
|
|
}
|
|
$files = scandir($dir);
|
|
foreach ($files as $file) {
|
|
// dump($file);
|
|
if ($file == "." || $file == ".." || $file == ".DS_Store" || $file == ".tmp.drivedownload") {
|
|
continue;
|
|
}
|
|
if (is_dir($dir . "/" . $file)) {
|
|
$this->yaParseDir($dir . "/" . $file);
|
|
}
|
|
if (is_file($dir . "/" . $file) && (pathinfo($file, PATHINFO_EXTENSION) == "jpg" || pathinfo($file, PATHINFO_EXTENSION) == "jpeg" || pathinfo($file, PATHINFO_EXTENSION) == "mp4")) {
|
|
echo $dir;
|
|
echo "\n";
|
|
echo $file;
|
|
$dirList = explode("/", $dir);
|
|
$filePrefix = array_pop($dirList);
|
|
$filePrefixList = explode("-", $filePrefix);
|
|
if (!str_contains($file, $starter . "-" . $filePrefixList[0])) {
|
|
rename($dir . "/" . $file, $dir . "/" . $starter . "-" . $filePrefixList[0] . "-" . $file);
|
|
}
|
|
echo "\n";
|
|
// exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function mvFiles($dir = "")
|
|
{
|
|
$dirList = [];
|
|
$fileList = [];
|
|
if (is_dir($dir)){
|
|
$files = scandir($dir);
|
|
foreach ($files as $file) {
|
|
if ($file == "." || $file == "..") {
|
|
continue;
|
|
}
|
|
if (is_dir($dir . $file)) {
|
|
$dirList[$file] = $dir . $file;
|
|
}
|
|
if (is_file($dir . $file) && pathinfo($file, PATHINFO_EXTENSION) == "mp4") {
|
|
$fileList[$file] = $dir . $file;
|
|
}
|
|
}
|
|
// foreach ($fileList as $key => $value) {
|
|
// $fileKey = explode(" ", $key)[0];
|
|
// if (array_key_exists($fileKey, $dirList)) {
|
|
// echo $fileKey . "\n";
|
|
//// copy($value, $dirList[$fileKey] . "/" . $key);
|
|
// shell_exec("mv " . escapeshellarg($value) . " " . escapeshellarg($dirList[$fileKey] . "/" . $fileKey . ".mp4"));
|
|
//// exit;
|
|
// }
|
|
//// exit;
|
|
// }
|
|
foreach ($fileList as $key => $value) {
|
|
$fileMatches = [];
|
|
preg_match("#VN.\d+#", $key, $fileMatches);
|
|
foreach ($dirList as $dirKey => $dirValue) {
|
|
$dirMatches = [];
|
|
preg_match("#VN.\d+#", $dirKey, $dirMatches);
|
|
if (count($fileMatches) == 1 && count($dirMatches) == 1 && $fileMatches[0] == $dirMatches[0]) {
|
|
|
|
echo "file is " . $key . " dir is " . $dirKey . "\n";
|
|
shell_exec("mv " . escapeshellarg($value) . " " . escapeshellarg($dirList[$dirKey] . "/" . $dirKey . ".mp4"));
|
|
}
|
|
}
|
|
}
|
|
|
|
print_r($dirList);
|
|
print_r($fileList);
|
|
}
|
|
}
|
|
|
|
}
|