Last active
March 10, 2022 06:35
-
-
Save pranid/08525c27a01e03e26e9cd25092a01164 to your computer and use it in GitHub Desktop.
Rename bulk files or folders :: PHP
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 | |
/** | |
* BULK FILE AND FOLDER RENAMER | |
* @author PRANEETH NIDARSHAN | |
* @version 1.3 | |
* @email [email protected] | |
*/ | |
$results = array(); | |
$error_msg = false; | |
$success_msg = array(); | |
function getFolderFiles($dir,$txt_find,$txt_replace) { | |
global $results, $error_msg, $success_msg; | |
$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 (strpos($scan_dir_result, $txt_find) !== false) { | |
if($txt_replace) { | |
$new_file_name = str_replace($txt_find,$txt_replace,$result_path); | |
rename($result_path, $new_file_name); | |
$rename_res = TRUE; | |
} else { | |
$rename_res = FALSE; | |
$new_file_name = 'NO CHANGE'; | |
} | |
array_push($results, array( | |
'OLD_NAME' => $result_path, | |
'NEW_NAME' => $new_file_name, | |
'STATUS' => $rename_res, | |
'RENAMED' => $txt_find ." --> ".$txt_replace | |
)); | |
} | |
if (is_dir($result_path)) { | |
getFolderFiles($result_path, $txt_find,$txt_replace); | |
} | |
} | |
} | |
} | |
if(isset($_POST['fm_directory']) && isset($_POST['fm_find'])) { | |
$dir = $_POST['fm_directory']; | |
$txt_find = $_POST['fm_find']; | |
$txt_replace = isset($_POST['fm_replace']) ? $_POST['fm_replace'] : false; | |
try { | |
if(is_dir($dir)) { | |
getFolderFiles($dir,$txt_find,$txt_replace); | |
} else { | |
$error_msg = "Invalid directory or directory could not be found. <br><em>".$dir."</em>"; | |
} | |
} catch (Exception $e) { | |
} | |
if(count($results) > 0) { | |
$success_msg = count($results) . ' results has been found.'; | |
} else { | |
$error_msg = 'No match has been found.'; | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>BULK FILE & FOLDER RENAMER</title> | |
<meta http-equiv="cache-control" content="max-age=0" /> | |
<meta http-equiv="cache-control" content="no-cache" /> | |
<meta http-equiv="expires" content="0" /> | |
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> | |
<meta http-equiv="pragma" content="no-cache" /> | |
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> | |
<style type="text/css"> | |
.header-con{ | |
padding:3% 5%; | |
} | |
.form-control, | |
.btn { | |
width:100%; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="row header-con"> | |
<div class="col-sm-4 col-sm-offset-4"> | |
<img src="http://inndeinc.com/assets/img/INNDEINC-SLOGON.png" class="img-responsive"> | |
<h3 class="text-center">BULK FILE & FOLDER RENAMER</h3> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-sm-8 col-sm-offset-2"> | |
<?php if ($error_msg): ?> | |
<div class="alert alert-danger" role="alert"><?= $error_msg; ?></div> | |
<?php endif ?> | |
<?php if ($success_msg): ?> | |
<div class="alert alert-success" role="alert"><?= $success_msg; ?></div> | |
<?php endif ?> | |
<div class="panel panel-default"> | |
<div class="panel-body"> | |
<form action="" method="POST"> | |
<div class="form-group col-sm-12"> | |
<div class="panel panel-info"> | |
<div class="panel-body"> | |
<em class="text-info">Search mode is case sensitive</em> | |
</div> | |
</div> | |
</div> | |
<div class="form-group col-sm-12"> | |
<label for="fm_directory">Search Directory</label> | |
<input type="text" class="form-control" name="fm_directory" placeholder="Search Directory" required="true" value="<?= (isset($dir) ? $dir : '') ?>"> | |
</div> | |
<div class="form-group col-sm-6"> | |
<label for="fm_find">Find</label> | |
<input type="text" class="form-control" name="fm_find" placeholder="Find" required="true" value="<?= (isset($txt_find) ? $txt_find : '') ?>"> | |
</div> | |
<div class="form-group col-sm-6"> | |
<label for="fm_replace">Replace</label> | |
<input type="text" class="form-control" name="fm_replace" placeholder="Replace" value="<?= (isset($txt_replace) ? $txt_replace : '') ?>"> | |
</div> | |
<div class="form-group col-sm-12"> | |
<button type="submit" class="btn btn-primary">Execute</button> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
<div class="col-sm-12"> | |
<?php if (count($results) > 0): ?> | |
<hr> | |
<h4>Results</h4> | |
<hr> | |
<table class="table table-bordered"> | |
<thead> | |
<tr> | |
<th>OLD_NAME</th> | |
<th>NEW_NAME</th> | |
<th>RENAMED</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php foreach ($results as $key => $result): ?> | |
<tr> | |
<td><?= $result['OLD_NAME']; ?></td> | |
<td><?= $result['NEW_NAME']; ?></td> | |
<td><?= ($result['STATUS']) ? 'YES' : 'NO'; ?></td> | |
</tr> | |
<?php endforeach ?> | |
</tbody> | |
</table> | |
<?php endif ?> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment