Created
March 28, 2022 17:59
-
-
Save catwhocode/02b553fd4b489a520383d007f0e934e4 to your computer and use it in GitHub Desktop.
Batch remove string from all filenames in a folder
This file contains hidden or 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; | |
function __construct() | |
{ | |
$this->keyword = ''; | |
$this->filenames = []; | |
$this->extension = 'php'; | |
$this->folder = '.'; | |
$this->newFile = []; | |
} | |
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 lebihPanjang($filename) { | |
$panjangMinusExtension = strlen($filename)-3; | |
if ($panjangMinusExtension <= 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() | |
{ | |
$arrFilenames = []; | |
foreach($this->filenames as $key => $filename) | |
{ | |
if ($this->isMatch($filename)) | |
{ | |
$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(); | |
$extension = 'mp4'; | |
$fileUtils->setExtension($extension); | |
$keyword = 'string_to_be_removed'; | |
$fileUtils->setKeyword($keyword); | |
// path where file reside | |
$folder = 'D:/videos'; | |
$fileUtils->setFolder($folder); | |
// read folder contents | |
$files = $fileUtils->getFiles(); | |
// remove string from filenames | |
$fileUtils->renameCutFilenames(); | |
// $fileUtils->insertString('.', 3); // 1-based | |
// $fileUtils->printNewFilenames(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment