Script: Goodbye Special Characters – Automated Cleanup of File Names

Sometimes we need to rename various files and folders, especially when our customers supply us with data. For this reason, we are sharing one of our scripts with you—the very same one we have already provided to many of our customers. With our script for renaming files or folders, which works flawlessly on Windows, Mac, and Linux, you can save a lot of time.
Intelligent File Naming:
Automate Your Files with Ease
The PHP script we are introducing automates the process of renaming files and folders by generating a clean, standardized file name (slug). Initially, you can control, via
parameters such as $dryRun
, $renameFiles
, and $renameFolders
, whether the script only simulates or actually renames files and folders,
and whether both types of objects should be considered.
Using the function toSlug
, special characters are removed, all letters are converted to lowercase, and, if activated, also transformed into title case – with
the chosen delimiter, here the underscore (_
), being used.
The script traverses the current directory, skipping system folders like .
and ..
as well as the script file itself, and creates
a new, cleaned name for each file or folder. It then outputs the original name and the new slug, and renames the file or folder, provided that you have disabled the dry run
($dryRun
). This allows the script to offer you a flexible and secure adaptation of your file names – ideal for consistent and clear file management across
different operating systems.
You can adjust the following parameters before execution:
- dryRun: Outputs the new file names, but does not change anything yet.
- renameFiles: Should files be renamed?
- renameFolders: Should folders be renamed?
- titleCase: The_Is_Title_Case – Should be renamed accordingly?
- separator: Delimiter, e.g. dash, underscore, or even a space
<?php
$dryRun = true;
$renameFiles = true;
$renameFolders = true;
$titleCase = true; // Title-Case-Word-Is
$separator = '_'; // '-' or '_' or ' '
function toSlug($slug) {
global $titleCase, $separator;
$slug = strtolower(trim(preg_replace('~[^0-9a-z]+~i', $separator, html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($slug, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
if ($titleCase): ucwords($slug, $separator); endif;
return $slug;
}
$files = scandir(".");
foreach ($files as $file):
if (in_array($file, ['.', '..', '.DS_Store'])): continue; endif;
if (basename(__FILE__) === $file): continue; endif;
if (!$renameFiles && is_file($file)): continue; endif;
if (!$renameFolders && is_dir($file)): continue; endif;
$slug = toSlug($file);
echo "$file -> $slug" . PHP_EOL;
if (!$dryRun): rename($file, $slug); endif;
endforeach;
How do you run the script?
Here's how to run the script: Copy the entire code into a file named rename.php
and adjust the parameters such as $dryRun
,
$renameFiles
, $renameFolders
, $titleCase
, and the desired separator character to suit your individual needs.
Then, open the terminal (Command Prompt or PowerShell on Windows) and navigate to the directory where your rename.php
is located. Enter the command php
rename.php
to start the script. This process works flawlessly on Windows, macOS, and Linux since PHP is cross-platform.
First, use the Dry-Run mode to see a simulation of the changes before running the script without this mode to perform the actual renaming of your files and folders.