|
@@ -4,7 +4,7 @@
|
|
|
// www.dbmxpca.com
|
|
// www.dbmxpca.com
|
|
|
// Date Created: May 18, 2020
|
|
// Date Created: May 18, 2020
|
|
|
// Last Updated: May 19, 2020
|
|
// Last Updated: May 19, 2020
|
|
|
-// Version: 1.06
|
|
|
|
|
|
|
+// Version: 2.03
|
|
|
|
|
|
|
|
// ====================================================================================
|
|
// ====================================================================================
|
|
|
// --- SETTINGS --- SETTINGS --- SETTINGS --- SETTINGS --- SETTINGS --- SETTINGS ---
|
|
// --- SETTINGS --- SETTINGS --- SETTINGS --- SETTINGS --- SETTINGS --- SETTINGS ---
|
|
@@ -228,21 +228,54 @@ if ($enable_dir_scan_preview){
|
|
|
echo " ================================\n";
|
|
echo " ================================\n";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+$total_items_original = count($contents);
|
|
|
|
|
+// Account for zero-based indexing.
|
|
|
|
|
+if ($total_items_original > 0)
|
|
|
|
|
+ $total_items_original++;
|
|
|
|
|
+
|
|
|
|
|
+echo " > A total of " . $total_items_original . " files and directories were discovered.\n";
|
|
|
|
|
+
|
|
|
$content_map = array();
|
|
$content_map = array();
|
|
|
$content_map_new = array();
|
|
$content_map_new = array();
|
|
|
|
|
|
|
|
-// Build a content map: array with path => filename format.
|
|
|
|
|
|
|
+echo " > Creating base content map, please wait...\n";
|
|
|
|
|
+$script_time_start = microtime(true);
|
|
|
|
|
+
|
|
|
|
|
+// V2 Update: Build a content map with ONLY the path, while
|
|
|
|
|
+// honoring the $skip_directory setting. The content map will
|
|
|
|
|
+// be roughly the same if $skip_directories is false but should
|
|
|
|
|
+// usually be shortened if the aforementioned setting is true.
|
|
|
foreach ($contents as $c){
|
|
foreach ($contents as $c){
|
|
|
|
|
|
|
|
- if (is_dir($c))
|
|
|
|
|
- $filename = null;
|
|
|
|
|
- else
|
|
|
|
|
- $filename = basename($c);
|
|
|
|
|
-
|
|
|
|
|
- $content_map[$c] = $filename;
|
|
|
|
|
|
|
+ switch($skip_directories){
|
|
|
|
|
+
|
|
|
|
|
+ case true:
|
|
|
|
|
+ if (!is_dir($c)){
|
|
|
|
|
+ array_push($content_map, $c);
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ default:
|
|
|
|
|
+ array_push($content_map, $c);
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-$content_map_new = $content_map;
|
|
|
|
|
|
|
+$script_time_end = microtime(true);
|
|
|
|
|
+$script_runtime = $script_time_end - $script_time_start;
|
|
|
|
|
+
|
|
|
|
|
+// Cleanup items no longer needed.
|
|
|
|
|
+$contents = null;
|
|
|
|
|
+
|
|
|
|
|
+echo " > Base content map creation complete [" . $script_runtime . " ms].\n";
|
|
|
|
|
+$total_items_old = count($content_map);
|
|
|
|
|
+// Account for zero-based indexing.
|
|
|
|
|
+if ($total_items_old > 0)
|
|
|
|
|
+ $total_items_old++;
|
|
|
|
|
+
|
|
|
|
|
+echo " > A total of " . $total_items_old . " item(s) remain, after filtering based on script directory-skip settings.\n";
|
|
|
|
|
+
|
|
|
|
|
+// Instead of copying the entire array, we'll add to it as necessary later.
|
|
|
|
|
+// $content_map_new = $content_map;
|
|
|
|
|
|
|
|
echo " > Creating new content map based on supplied search and replace settings, please wait...\n";
|
|
echo " > Creating new content map based on supplied search and replace settings, please wait...\n";
|
|
|
|
|
|
|
@@ -251,26 +284,48 @@ echo " > Creating new content map based on supplied search and replace settings,
|
|
|
// Build the replacement content map. This contains the same path => filename
|
|
// Build the replacement content map. This contains the same path => filename
|
|
|
// format but with the updated filename instead of the original.
|
|
// format but with the updated filename instead of the original.
|
|
|
$script_time_start = microtime(true);
|
|
$script_time_start = microtime(true);
|
|
|
-foreach ($content_map_new as $p => $f){
|
|
|
|
|
|
|
+// foreach ($content_map_new as $p => $f){
|
|
|
|
|
+
|
|
|
|
|
+// V2 Update: Build a new content map, with ONLY values that will be replaced,
|
|
|
|
|
+// in key => value pairs as old_value => new_value.
|
|
|
|
|
+foreach ($content_map as $p){
|
|
|
|
|
+
|
|
|
|
|
+ // Keep track of old and new filenames.
|
|
|
|
|
+ $filename = $p;
|
|
|
|
|
+ $filename_new = $filename;
|
|
|
|
|
|
|
|
- // REPLACE2($str, $search, $replace)
|
|
|
|
|
- $filename_new = REPLACE2($f, $regex_search, $replace_str);
|
|
|
|
|
- $path_new = REPLACE2($p, $regex_search, $replace_str);
|
|
|
|
|
- $content_map_new[$p] = $filename_new;
|
|
|
|
|
|
|
+ // Perform the search & replacement.
|
|
|
|
|
+ $filename_new = REPLACE2($filename_new, $regex_search, $replace_str);
|
|
|
|
|
+
|
|
|
|
|
+ // If original and new filenames are different, then a replacement was made.
|
|
|
|
|
+ // Add this to the new content map.
|
|
|
|
|
+ if (!ARE_STRINGS_EQUAL($filename, $filename_new)){
|
|
|
|
|
+
|
|
|
|
|
+ $content_map_new[$filename] = $filename_new;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Record number of items for use later.
|
|
|
|
|
+$total_items = count($content_map_new);
|
|
|
|
|
+// Account for zero-based indexing.
|
|
|
|
|
+if ($total_items > 0)
|
|
|
|
|
+ $total_items++;
|
|
|
|
|
+
|
|
|
$script_time_end = microtime(true);
|
|
$script_time_end = microtime(true);
|
|
|
$script_runtime = $script_time_end - $script_time_start;
|
|
$script_runtime = $script_time_end - $script_time_start;
|
|
|
|
|
|
|
|
-echo " > Success: new content map created [" . $script_runtime . " ms].\n";
|
|
|
|
|
|
|
+// Cleanup items no longer needed.
|
|
|
|
|
+$content_map = null;
|
|
|
|
|
|
|
|
|
|
+echo " > Success: new content map created [" . $script_runtime . " ms].\n";
|
|
|
echo " > FILE AND DIRECTORY PREVIEW:\n ================================\n";
|
|
echo " > FILE AND DIRECTORY PREVIEW:\n ================================\n";
|
|
|
|
|
|
|
|
|
|
|
|
|
$counter = 0;
|
|
$counter = 0;
|
|
|
$stop_preview = false;
|
|
$stop_preview = false;
|
|
|
|
|
|
|
|
-foreach ($content_map as $p => $f){
|
|
|
|
|
|
|
+// Display preview of new content map.
|
|
|
|
|
+foreach ($content_map_new as $old => $new){
|
|
|
|
|
|
|
|
if ($stop_preview)
|
|
if ($stop_preview)
|
|
|
break;
|
|
break;
|
|
@@ -286,98 +341,104 @@ foreach ($content_map as $p => $f){
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $new_path = dirname($p) . "/" . $content_map_new[$p];
|
|
|
|
|
- echo " [before]: " . $p . "\n";
|
|
|
|
|
- echo " [after]: " . $new_path . "\n\n";
|
|
|
|
|
|
|
+ echo " [before]: " . $old . "\n";
|
|
|
|
|
+ echo " [after]: " . $new . "\n\n";
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-echo " *** Please review above results carefully ***\n\n";
|
|
|
|
|
-echo " - To begin operation (cannot be undone), type 'Y' and press ENTER/RETURN.\n";
|
|
|
|
|
-// echo " - To preview more results, type 'P' and press ENTER/RETURN.\n";
|
|
|
|
|
-echo " - To cancel script without making any changes, type 'N' and press ENTER/RETURN.\n";
|
|
|
|
|
-echo "\n >> Begin operation? [Y/n]: ";
|
|
|
|
|
-$response = fgets(STDIN);
|
|
|
|
|
-$begin_operation = substr($response, 0, strlen($response) - 1);
|
|
|
|
|
|
|
+echo "\n >>> A total of " . $total_items . " item(s) will be renamed if you continue. <<<\n\n";
|
|
|
|
|
|
|
|
-if (ARE_STRINGS_EQUAL($begin_operation, "Y", true)){
|
|
|
|
|
-
|
|
|
|
|
- echo "\n > Operation started by user.\n";
|
|
|
|
|
- $countdown_timer = 5;
|
|
|
|
|
- while($countdown_timer > 0){
|
|
|
|
|
|
|
+if ($total_items > 0){
|
|
|
|
|
+
|
|
|
|
|
+ echo " *** PLEASE REVIEW ABOVE PREVIEW RESULTS CAREFULLY BEFORE PROCEEDING. ***\n\n";
|
|
|
|
|
+ echo " - To begin operation (cannot be undone), type 'Y' and press ENTER/RETURN.\n";
|
|
|
|
|
+ // echo " - To preview more results, type 'P' and press ENTER/RETURN.\n";
|
|
|
|
|
+ echo " - To cancel script without making any changes, type 'N' and press ENTER/RETURN.\n";
|
|
|
|
|
+ echo "\n >> Begin operation? [Y/n]: ";
|
|
|
|
|
+ $response = fgets(STDIN);
|
|
|
|
|
+ $begin_operation = substr($response, 0, strlen($response) - 1);
|
|
|
|
|
+
|
|
|
|
|
+ if (ARE_STRINGS_EQUAL($begin_operation, "Y", true)){
|
|
|
|
|
|
|
|
- echo " > Operation starting in " . $countdown_timer . " second(s)...\n";
|
|
|
|
|
- $countdown_timer--;
|
|
|
|
|
- sleep(1);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- echo "\n > Operation in progress, please wait...\n";
|
|
|
|
|
-
|
|
|
|
|
- $total_items = count($content_map) + 1;
|
|
|
|
|
- $items_remaining = $total_items;
|
|
|
|
|
- $curr_item = 0;
|
|
|
|
|
- $progress_val = 0;
|
|
|
|
|
- $script_time_start = microtime(true);
|
|
|
|
|
-
|
|
|
|
|
- foreach ($content_map as $p => $f){
|
|
|
|
|
|
|
+ echo "\n > Operation started by user.\n";
|
|
|
|
|
+ $countdown_timer = 5;
|
|
|
|
|
+ while($countdown_timer > 0){
|
|
|
|
|
+
|
|
|
|
|
+ echo " > Operation starting in " . $countdown_timer . " second(s)...\n";
|
|
|
|
|
+ $countdown_timer--;
|
|
|
|
|
+ sleep(1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ echo "\n > Operation in progress, please wait...\n";
|
|
|
|
|
|
|
|
- $curr_item++;
|
|
|
|
|
- $progress_val = $curr_item / $total_items;
|
|
|
|
|
- $progress_val *= 100;
|
|
|
|
|
- $progress_val_d = number_format((float)$progress_val, 2, '.', '');
|
|
|
|
|
|
|
+ $items_remaining = $total_items;
|
|
|
|
|
+ $curr_item = 0;
|
|
|
|
|
+ $progress_val = 0;
|
|
|
|
|
+ $script_time_start = microtime(true);
|
|
|
|
|
|
|
|
- // TODO: Only perform this check if skip_directories is TRUE.
|
|
|
|
|
- if (!is_dir($p)){
|
|
|
|
|
|
|
+ foreach ($content_map_new as $old => $new){
|
|
|
|
|
|
|
|
|
|
+ $curr_item++;
|
|
|
|
|
+ $progress_val = $curr_item / $total_items;
|
|
|
|
|
+ $progress_val *= 100;
|
|
|
|
|
+ $progress_val_d = number_format((float)$progress_val, 2, '.', '');
|
|
|
|
|
|
|
|
- $new_path = dirname($p) . "/" . $content_map_new[$p];
|
|
|
|
|
- // Actual command to be executed.
|
|
|
|
|
- $cmd = "mv \"" . $p . "\" \"" . $new_path . "\"";
|
|
|
|
|
- // Truncated command to be displayed.
|
|
|
|
|
- $cmd_d = "-";
|
|
|
|
|
- // Make sure the strings are actually long enough to get truncated. If either are too short,
|
|
|
|
|
- // then just display the full command.
|
|
|
|
|
- if (strlen($p) >= -$cmd_display_limit && strlen($new_path) >= -$cmd_display_limit){
|
|
|
|
|
- $cmd_d = "mv \"..." . substr($p, $cmd_display_limit) . "\" \"..." . substr($new_path, $cmd_display_limit) . "\"";
|
|
|
|
|
- }
|
|
|
|
|
- else{
|
|
|
|
|
- $cmd_d = $cmd;
|
|
|
|
|
|
|
+ // TODO: Only perform this check if skip_directories is TRUE.
|
|
|
|
|
+ if (!is_dir($p)){
|
|
|
|
|
+
|
|
|
|
|
+ // Actual command to be executed.
|
|
|
|
|
+ $cmd = "mv \"" . $old . "\" \"" . $new . "\"";
|
|
|
|
|
+ // Truncated command to be displayed.
|
|
|
|
|
+ $cmd_d = "-";
|
|
|
|
|
+ // Make sure the strings are actually long enough to get truncated. If either are too short,
|
|
|
|
|
+ // then just display the full command.
|
|
|
|
|
+ if (strlen($old) >= -$cmd_display_limit && strlen($new) >= -$cmd_display_limit){
|
|
|
|
|
+ $cmd_d = "mv \"..." . substr($old, $cmd_display_limit) . "\" \"..." . substr($new, $cmd_display_limit) . "\"";
|
|
|
|
|
+ }
|
|
|
|
|
+ else{
|
|
|
|
|
+ $cmd_d = $cmd;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!$display_progress && !$display_full_cmd){ //00
|
|
|
|
|
+ // print nothing
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (!$display_progress && $display_full_cmd){ //01
|
|
|
|
|
+ echo " >>> [" . $cmd_d . "]\n";
|
|
|
|
|
+ }
|
|
|
|
|
+ else if ($display_progress && !$display_full_cmd){ //10
|
|
|
|
|
+ echo " >>> [Progress: " . $progress_val_d . "% (" . $curr_item . "/" . $total_items . ") | Items Remaining: " . $items_remaining . "]\n";
|
|
|
|
|
+ }
|
|
|
|
|
+ else if ($display_progress && $display_full_cmd){ //11
|
|
|
|
|
+ echo " >>> [Progress: " . $progress_val_d . "% (" . $curr_item . "/" . $total_items . ") | Remaining: " . $items_remaining . "] | [" . $cmd_d . "]\n";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ exec($cmd);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (!$display_progress && !$display_full_cmd){ //00
|
|
|
|
|
- // print nothing
|
|
|
|
|
- }
|
|
|
|
|
- else if (!$display_progress && $display_full_cmd){ //01
|
|
|
|
|
- echo " >>> [" . $cmd_d . "]\n";
|
|
|
|
|
- }
|
|
|
|
|
- else if ($display_progress && !$display_full_cmd){ //10
|
|
|
|
|
- echo " >>> [Progress: " . $progress_val_d . "% (" . $curr_item . "/" . $total_items . ") | Items Remaining: " . $items_remaining . "]\n";
|
|
|
|
|
- }
|
|
|
|
|
- else if ($display_progress && $display_full_cmd){ //11
|
|
|
|
|
- echo " >>> [Progress: " . $progress_val_d . "% (" . $curr_item . "/" . $total_items . ") | Remaining: " . $items_remaining . "] | [" . $cmd_d . "]\n";
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $items_remaining--;
|
|
|
|
|
|
|
|
- exec($cmd);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $items_remaining--;
|
|
|
|
|
|
|
+ $script_time_end = microtime(true);
|
|
|
|
|
+ $script_runtime = $script_time_end - $script_time_start;
|
|
|
|
|
+
|
|
|
|
|
+ echo " > Operation complete. [" . $script_runtime . " ms].\n";
|
|
|
|
|
+ exit(0);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- $script_time_end = microtime(true);
|
|
|
|
|
- $script_runtime = $script_time_end - $script_time_start;
|
|
|
|
|
-
|
|
|
|
|
- echo " > Operation complete. [" . $script_runtime . " ms].\n";
|
|
|
|
|
|
|
+
|
|
|
|
|
+ echo "\n > Operation cancelled by user.\n";
|
|
|
exit(0);
|
|
exit(0);
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-echo "\n > Operation cancelled by user.\n";
|
|
|
|
|
-exit(0);
|
|
|
|
|
-
|
|
|
|
|
|
|
+else{
|
|
|
|
|
+
|
|
|
|
|
+ echo "\n > Operation cancelled: there are no items to replace.\n";
|
|
|
|
|
+ exit(0);
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|