deck renamer

Created by Marfisa and explained by Rahenna. Please credit In the Cards if you use this script.

Are you one of the TCG owners who creates decks as one large image, then slices them apart? Depending on the program you use, your files might end up with weird names, and they have to be manually renamed. Not anymore! This simple script will rename all the files in a directory with the deck name of your choice.

This is a PHP tool.
Your host must support PHP in order for you to use this script.

the instructions

Please read these instructions carefully. You must be careful when using this tool! If you upload and run it in your main card directory, you WILL rename ALL of your TCG's active cards!

1. Create the two PHP files as described below.
2. Upload them into a directory that is NOT your active card folder. I keep mine in a subdirectory of the main card folder that is named new.
3. To use the script, simply access rename.php from your browser, type the deck name in the form, and press the rename button. You will see a confirmation of your actions.
4. Once your cards are renamed, move them to the appropriate place in your file structure.
5. REMEMBER: You can only rename one deck at a time. This script will rename ALL the files in the directory without stopping!

the code

Insert this code into a file called rename.php. The part before the form is a simple check of how many image files are in your directory. If you see a number that is larger than the number of cards in one deck, STOP. Something isn't right. Check to make sure the script is in the right folder and you've only uploaded enough funky-named cards for ONE deck.

<?
$directory=dirname(__FILE__);
$dir=opendir($directory);
$pattern="\.(gif|jpg|jpeg|png|bmp)$";

if(!$dir)
{
die("Failed to read directory");
}

$s=readdir($dir);
$count=0;
$image;

while($s)
{
if(ereg($pattern, $s))
{
$image[$count]=$s;
$count++;
}
$s=readdir($dir);
}
closedir($dir);
?>

<p>
The directory is:<br>
<? echo $directory; ?><br>
There are <? echo count($image); ?> image files in this directory.
</p>

<form action="renamed.php" method="post">
Deck Name: <input type="text" name="title">
<input type="Submit" value="Rename">
</form>

See how the form action calls for renamed.php? That's where the magic happens. Create a file called renamed.php and copy the code below into that file.

HINT:
If you need help finding your full PHP path, see the PHP finds your full path! section on the PHP Basics page.

<?php

$title=$_POST['title'];
$dir=opendir("INSERT THE FULL PHP PATH TO YOUR DIRECTORY HERE");
$directory=("INSERT THE URL TO YOUR DIRECTORY HERE");
$pattern="\.(gif|jpg|jpeg|png|bmp|swf)$";

if(!$dir)
{
die("Failed to read directory");
}

$s=readdir($dir);
$count=0;
$image;

while($s)
{
if(ereg($pattern, $s))
{
$image[$count]=$s;
$count++;
}
$s=readdir($dir);
}
closedir($dir);

//Sort in alphabetical order
sort($image);

//Spit it out
$zeros1="0";
$zeros2="";

$limit=count($image);
$i=0;
while ($i < $limit) {

$oldfile = $image[$i];
$i++;
if ($i < 10) {
$newfile = $title . $zeros1 . $i . ".gif";
}
else
$newfile = $title . $zeros2 . $i . ".gif";
rename($oldfile,$newfile);
echo "Renamed " . $oldfile . " to " . $newfile . "
\n"; } ?&rt;

And that's all! Enjoy your new tool!