Skip to content

Instantly share code, notes, and snippets.

@pranid
Last active March 28, 2017 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pranid/e25aae68092cf88868ce89021baa14ec to your computer and use it in GitHub Desktop.
Save pranid/e25aae68092cf88868ce89021baa14ec to your computer and use it in GitHub Desktop.
Bulk File re-name :: tested with MP3
<?php
/*$dir_files = glob($_SERVER["DOCUMENT_ROOT"]."/*");
getFolderFiles($dir_files);*/
$dir = "D:\MUSICS\English\Collections\FAVS";
$dir = "F:\MOVIES";
$final_array = array();
$file_filter = array(
'video' => array('mkv','mov','mp4','flv', 'avi'),
'image' => array('png','jpg','jpeg','gif')
);
$dir_files = getFolderFiles($dir);
function byteConvertor($bytes)
{
$sizes = array(
'KB' => pow(1024, 1),
'MB' => pow(1024, 2),
'GB' => pow(1024, 3),
'TB' => pow(1024, 4),
);
$sizes = array_reverse($sizes);
$bytes = abs($bytes);
foreach ($sizes as $key => $size) {
if($bytes >= $size) {
$size = $bytes / $size;
return round($size,2) ." ". $key;
}
}
return $bytes ." Bytes";
}
function getFolderFiles($dir) {
global $final_array,$file_filter;
$scan_dir = scandir($dir);
foreach($scan_dir as $scan_dir_result) {
// SKIP BACKING FOLDERS
if($scan_dir_result != '.' && $scan_dir_result != '..') {
$result_path = $dir."\\".$scan_dir_result;
$data = array(); // result array
if(is_dir($result_path)) {
$data['dirname'] = $scan_dir_result;
$data['child'] = getFolderFiles($result_path, $final_array);
} else {
$data = pathinfo($result_path);
if(is_file($result_path)) {
try {
$data['file_size'] = byteConvertor(filesize($result_path));
} catch (Exception $e) {
}
}
if(in_array($data['extension'], $file_filter['video'])) {
array_push($final_array, $data);
}
}
// rename($result_path, $new_file_name);
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment