Snap Creek File Rename Utility v0.1
This tool will rename all files with '#UNNNN' in their names to the proper UTF-8 characters.
Click 'Preview' to see files that will be renamed without performing the rename.
Click 'Rename' to rename the files.
$text";
function dirToArray($dir)
{
$directoryIterator = new RecursiveDirectoryIterator($dir);
$recursiveIteratorIterator = new RecursiveIteratorIterator($directoryIterator);
$regexIterator = new RegexIterator($recursiveIteratorIterator, '/\#U/', RecursiveRegexIterator::GET_MATCH);
$array = iterator_to_array($regexIterator, true);
return array_keys($array);
}
$dirpath = dirname(__FILE__);
$filepaths = array();
$paths = dirToArray($dirpath);
foreach ($paths as $path)
{
if (is_file($path))
{
$filepaths[] = $path;
}
}
$file_count = 0;
asort($filepaths);
foreach ($filepaths as $filepath)
{
if ((strpos($filepath, '#U') !== false) && (strpos($filepath, '.orig') === false))
{
$file_count++;
echo "FILE $filepath
";
$new_filepath = html_entity_decode(preg_replace("/\#U([0-9A-Fa-f]{4})/", "\\1;", $filepath), ENT_NOQUOTES, 'UTF-8');
if ($rename)
{
//$new_filename = preg_replace("/\#U([0-9A-Fa-f]{4})/", "\\1;", $filepath);
$backup_filepath = $filepath . '.orig';
$backup_base = basename($backup_filepath);
if ($test_mode)
{
$copied = true;
}
else
{
$copied = copy($filepath, $backup_filepath);
}
if ($copied)
{
if ($test_mode)
{
$renamed = true;
}
else
{
$renamed = @rename($filepath, $new_filepath);
}
$rename_base = basename($new_filepath);
if ($renamed)
{
echo "Renamed to $rename_base
";
}
else
{
echo "***ERROR: Couldn't Rename to $rename_base
";
@unlink($backup_filepath);
}
}
else
{
echo "***ERROR: Problem backing to $backup_base
";
}
}
echo '
';
}
}
echo "
Total Files: $file_count
";
if (!$rename && ($file_count > 0))
{
?>
';
?>