Sunday 03/02/2008 6:53 PM
Find and replace in filenames in command line (Windows)
Author: Panther
So… The windows command line has a bunch of commands built in for common tasks such as creating directories, renaming stuff, moving things about, yada yada yada. Its kind of stunted though when I compare it to the kind of file operations I do all the time in my development work.
I just finished sorting a huge number of files into appropriate directories, and noticed annoyingly that about 10,000 files (images in this case) had signatures in the file name making them ugly when displayed on my HTPC as a slide show. For example “Blue Iris (!downloaded from precariouspanther.net!)”… (The site link obviously changed in this case). Rather than having that garbage show up on my screen, I much prefer it to just be “Blue Iris”.
The easiest way to do this is just selecting the file, pressing F2 (or right click/rename) and editing the name removing out the garbage. This is normally fine - however with this collection of around 10,000 images it would take, oh I dunno… three years???
Frell that for a joke! After five mins scouring google I didn’t find a quick and easy solution, so whipped up a quick php script to enable simple find/replace on file names in a directory, opened up trusty CMD, typed in
findReplaceName * ” (!downloaded from precariouspanther.net!)” “”
and within 2 minutes all the work is done. I used to use perl for my shell scripting in linux all the time on the web servers, but given I’m coding php all day every day (and setting up perl on windows is annoying) this worked out much much better for me.
Anywho, I’ve attached the shell script and its cmd loader as a zip. To install it just make sure you have php cli set up (and php is in your systems path vars), and extract the zip to c:\phpcli\ and lastly add c:\phpcli\ (if it isnt) also to your system path vars. From then on you can call it from any command prompt (or application).
You can use a directory other than c:\phpcli\ if you like, just make sure you update the corresponding line in the .cmd file so it can find the script.
Its usage is as follows:
findReplaceName FILTER STRING_TO_FIND REPLACEMENT_STRING
FILTER is a wildcard filter for the files you want to apply the transformation to ( * for all)
STRING_TO_FIND is the string you want to search for and replace
REPLACEMENT_STRING is what you want to replace it with.
Remember, as its command line make sure you wrap your string to find and replacement string in “” quotes.
BTW for anybody who doesn’t know how to add system path vars this script probably isn’t the best solution for you as it can be a bit ‘nitty gritty’.
The guts of the fairly simple script are:
//Adams funkadelic file-name component stripperiser
$filter=$argv[1];
$stringToFind=$argv[2];
$stringToReplace=$argv[3];
if(!$stringToReplace && !$stringToFind){
echo "Usage: findReplaceName FILTER TEXT_TO_FIND TEXT_TO_REPLACE";
} else {
foreach(glob($filter) as $file){
echo $file."n";
$newName=str_replace($stringToFind,$stringToReplace,$file);
echo "Currently Processing: $file - to $newName";
rename($file,$newName);
}
}
Download - PHP CLI Script and Loader - Find and Replace in Filenames
You can leave a response, or trackback from your own site.

