Nice command line script to rename bad file names

When I say "bad file names" I mean spaces and other non word characters. I like to have nice UNIX compatible file names. I know, you can have other characters, but I don't like to escape them, and the most important thing, I have OCD :), so I like things to be the way I want them, and this script does it for me.

I added \. inside of the first preg_replace because I don't want file extensions to get renamed. I haven't tested this though. It may complain.

<?php
foreach(glob("*") as $file)
{
        $newfile = preg_replace("/[^a-zA-Z0-9_-\.]/","_",$file);
        $newfile = preg_replace("/_{2,}/","_",$newfile);
        $newfile = preg_replace("/^_/","",$newfile);
        rename($file,$newfile);
}
?>

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.