Last active
August 30, 2023 00:40
-
-
Save catwhocode/3412d6e5a96950a3c05a50ffd2ed563f to your computer and use it in GitHub Desktop.
Cut the file name taking advantage of their title pattern.
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 | |
class FileUtils | |
{ | |
public $keyword; | |
public $filenames; | |
public $extension; | |
public $folder; | |
public $newFile; | |
public $is_left; | |
function __construct() | |
{ | |
$this->keyword = ''; | |
$this->filenames = []; | |
$this->extension = 'php'; | |
$this->folder = '.'; | |
$this->newFile = []; | |
$this->is_left = true; // left to right | |
} | |
function setFolder($folder) | |
{ | |
$this->folder = $folder; | |
} | |
function getFolder() | |
{ | |
return $this->folder; | |
} | |
function setExtension($extension) | |
{ | |
$this->extension = $extension; | |
} | |
function getActiveExtension() | |
{ | |
return $this->extension; | |
} | |
function setKeyword($keyword) | |
{ | |
$this->keyword = $keyword; | |
} | |
function getKeyword() | |
{ | |
return $this->keyword; | |
} | |
function insertDot() | |
{ | |
// | |
} | |
function getExtension($filename) | |
{ | |
return substr($filename, strlen($filename)-3, 3); | |
} | |
function longerThan($filename) { | |
$lengthMinusExtension = strlen($filename)-3; | |
if ($lengthMinusExtension <= strlen($this->keyword)) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
function isMatch($filename) | |
{ | |
if (substr($filename, 0, strlen($this->keyword)) == $this->keyword) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function getNewName($filename) | |
{ | |
$panjangKeyword = strlen($this->keyword); | |
$panjangNamaFile = strlen($filename); | |
return substr($filename, $panjangKeyword, $panjangNamaFile); | |
} | |
function hasString($source, $str, $pos) | |
{ | |
if (substr($source, $pos, strlen($str)) == $str) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function printNewFilenames() | |
{ | |
foreach($this->newFilenames as $key => $filename) { | |
echo $filename . "\n"; | |
} | |
} | |
function insertString($str, $pos) | |
{ | |
$arrFilenames = []; | |
foreach($this->filenames as $key => $filename) { | |
$leftSide = substr($filename, 0, $pos-1); | |
$rightSide = substr($filename, $pos, strlen($filename)); | |
$newFile = $leftSide . $str . $rightSide; | |
if ($newFile != $filename) { | |
$oldFilePath = $this->folder . '/' . $filename; | |
$newFilePath = $this->folder . '/' . $newFile; | |
if (rename($oldFilePath, $newFilePath)) | |
{ | |
$arrFilenames[] = $newFile; | |
} | |
} | |
} | |
$this->newFilenames = $arrFilenames; | |
} | |
function renameCutFilenames($moveNumbering=false) | |
{ | |
$arrFilenames = []; | |
foreach($this->filenames as $key => $filename) { | |
if ($this->isMatch($filename)) { | |
if ($moveNumbering == true) { | |
$arr = explode('#', $filename); | |
$filename = trim($arr[1]) . ' ' . trim($arr[0]); | |
} | |
$oldFile = $this->folder . '/' . $filename; | |
$newFile = $this->folder . '/' . $this->getNewName($filename); | |
if (rename($oldFile, $newFile)) { | |
$arrFilenames[] = $this->getNewName($filename); | |
} | |
} | |
} | |
$this->newFilenames = $arrFilenames; | |
} | |
function getFiles() | |
{ | |
if ($handle = opendir($this->folder)) { | |
$arrFiles = []; | |
while (false !== ($entry = readdir($handle))) { | |
if ($entry != "." && $entry != ".." && !is_dir($entry) && $this->getExtension($entry)==$this->extension) { | |
$arrFiles[] = $entry; | |
} | |
} | |
closedir($handle); | |
$this->filenames = $arrFiles; | |
} else { | |
$this->filenames = []; | |
} | |
} | |
} | |
$fileUtils = new FileUtils(); | |
// file name extension | |
$extension = 'mp4'; | |
$fileUtils->setExtension($extension); | |
// string to be cut from the file name | |
$keyword = 'ReactJS Tutorial - '; | |
$fileUtils->setKeyword($keyword); | |
// target folder | |
$folder = 'D:/learn/reactjs/codevolution'; | |
$fileUtils->setFolder($folder); | |
$files = $fileUtils->getFiles(); | |
$fileUtils->renameCutFilenames(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment