Rename, add sequence

software development

#1

I’m looking for a way I can rename files in my webspace, taking an image file, and renaming it with a number sequence on the end. However, I want the number sequence to be set by the modified date.

Shell scripting would be a fine way to accomplish this, but a web-based (perl, php,) would be allright too. I’d like to get this script combined with some image magick stuff in the end, so I’m hopping to be able to set up one script to accomplish both.

Thanks. :slight_smile:

-Matttail


#2

Actually, that is really easy to do with PHP. Making use of the rename() function, you could write a script that adds a timestamp to the end. For example:

<?php $timestamp = time(); $path = [i]path from document root to file[/i]; $current_name = [i]current filename[/i]; $desired_name = $current_name.$timestamp; rename("$path/$current_name", "$path/$desired_name") ?>You may wish to build safeguards into the script, like checking to see if the file exists, and whether or not the renaming was successful.


Simon Jessey
Keystone Websites (business site) | si-blog (personal site with affiliate links)


#3

I’m sorry, I wasn’t clear. I ment to say that the number sequence needs to be applied to the files ‘sorted’ by modififed time, rather than alphabetically.

I’ve got a a bunch of images, pictures being taken from a game actually, that are saved snapshot_session#_randomness.jpg so it’s all out of order if taken alphanumericaly. I’d like to rename that file to a-001.jpg, a-002.jpg, etc. But becuase of the random numbers and letters affixed on the file, things get out of order when the rename script goes through and takes things alphabetically.

sorry for not being clear the first time.

-Matttail


#4

I saw a forum post (not in this forum) that might start you out - see post #15. There’s this php function called filemtime, that gets the modified time of a file. You could rename in 2 steps. First rename all your files with the modified file time in the filename, which gets it into alphabetical order, and then go through the files again and rename it to 001, 002, etc.


#5

No problem. I’m still a little confused about the naming convention of your files. Can you give me a short list of example filenames, exactly as they occur, so that I can see what they look like? I think I can still write a renaming script to cope with that.


Simon Jessey
Keystone Websites (business site) | si-blog (personal site with affiliate links)


#6

Ok, here’s a few files.
snapshot_d0989b77_5098a9cc.jpg - 3:32pm
snapshot_5098b997_f098b998.jpg - 4:33pm
snapshot_5098b997_7098b9b1.jpg - 5:40pm
snapshot_108b2724_5098c89b.jpg - 5:41pm
snapshot_108b2724_d098cb9f.jpg - 6:44pm
snapshot_108b2724_b098cbe1.jpg - 6:58pm

I sorted thoes by date before pasting them here. The first string is the ‘session’ it changes each time I boot into the game. The seconed appears to be entirley random.

-Matttail


#7

[quote]snapshot_d0989b77_5098a9cc.jpg - 3:32pm
snapshot_5098b997_f098b998.jpg - 4:33pm
snapshot_5098b997_7098b9b1.jpg - 5:40pm
snapshot_108b2724_5098c89b.jpg - 5:41pm
snapshot_108b2724_d098cb9f.jpg - 6:44pm
snapshot_108b2724_b098cbe1.jpg - 6:58pm[/quote]
Hmm. Those letter/number combinations look like hexadecimal - interesting, but probably useless information.


Simon Jessey
Keystone Websites (business site) | si-blog (personal site with affiliate links)


#8

Okay, I’ve created a script that can rename everything in a directory for you. I have not built in any significant error checking, so right now it will rename any file (including a subdirectory) in the chosen directory (currently the “images” directory off of your web root).

[code]<?php
// create array of filenames sorted by modification time
function get_file_list($folder) {
$dircontent = scandir($folder);
$arr = array();
foreach($dircontent as $filename) {
if ($filename != ‘.’ && $filename != ‘…’) {
if (filemtime($folder.$filename) === false) return false;
$dat = date(“YmdHis”, filemtime($folder.$filename));
$arr[$dat] = $filename;
}
}
if (!ksort($arr)) return false;
return $arr;
}

// display or rename files
$path = $_SERVER[‘DOCUMENT_ROOT’]."/images";
$file_array = get_file_list($path);
foreach($file_array as $key => $value) {

// display list
print “$value - Last modified: $key
”;

// rename files with format “a-YYYYMMDDHHMMSS.jpg”
// rename("$path/$value","$path/a-$key.jpg");
}
?>[/code]It is setup to harmlessly list the contents of a directory at the moment. Comment-out the “display list” section and remove the comments from the “rename” section to make the script work as expected. Make sure you call the function with the correct directory.

It creates filenames like this (with a MySQL-style timestamp embedded in it):

a-YYYYMMDDHHMMSS.jpg

The filename is more useful than blind numbering like 001,002, etc. Hopefully, you can adapt this to suit your specific needs.


Simon Jessey
Keystone Websites (business site) | si-blog (personal site with affiliate links)


#9

I really apprechate your help Simon. Thanks.

First I found out scandir is a php5 only function, so I created a sub-domain and turned on php5, and I’m getting these errors:

Warning: filemtime() [function.filemtime]: stat failed for /home/alexandra/php5/imagessnapshot_108b2724_108c0332.jpg in /home/.unbelievable/alexandra/php5/resize/index.php on line 8

Warning: Invalid argument supplied for foreach() in /home/.unbelievable/alexandra/php5/resize/index.php on line 21

I found this page that gives an example of how to accomplish the same thing through php4, but I’m not sure what parts I need to repalce with what. For now php5 is fine for now, but it would be convient to be able to get it into 4 eventually.

Edit: if it makes any difference you can see it at http://www.php5.googlies.net

Again, thanks for your help.

-Matttail


#10

Usually you get that error when the directory is invalid. Check this line of the script to make sure it has the right directory:

$path = $_SERVER['DOCUMENT_ROOT']."/images";Assuming it is in a folder called “resize” which is inside your web folder, it should be “/resize” (with no trailing slash).


Simon Jessey
Keystone Websites (business site) | si-blog (personal site with affiliate links)


#11

It seems to be properly finding the images, becuase if I rename the folder, I get a directory not found error. Also the error pulled in one of the file names, but there’s no / between the images directory and the file name.

-Matttail


#12

I’m an idiot! That’s what I get for trying to juggle too many balls at once.

Sorry, dude, but you DO need the trailing slash after the directory name. My bad.


Simon Jessey
Keystone Websites (business site) | si-blog (personal site with affiliate links)


#13

Ok, I change it to /images/ and got the script working. Well, sorta. Tried out the script and it listed my files. “Cool!” I say. I comment out the dir list, and took out the comments for the rename part. I then run the script again (from firefox each time) and it renames 16 of the 20 images in the directory.

“Odd…” I say. I cleard out the images directory, copy in a fresh set from the .snapshot directory, set permissions to 777 for the folder and all the files. “Now let’s try the php script again” I say. It only renames two of the files. “Hmmm…”

“Perhaps I messed somehting up when I took out and put in comments. Let’s try copying and pasting Simon’s script from the froum again to be sure”. I delete the origional index file, then make a new index file and pop in a trailing forward slash for the images directory and write out the file.

It only lists two files. very strange me thinks. I tried running the file from ssh to see if it produces any errors that make sense to me… none - but here’s what it said, if it’s worth anything.


[galaga]$ bash index.php
index.php: line 1: ?php: No such file or directory
index.php: line 2: //: is a directory
index.php: line 3: syntax error near unexpected token $folder' index.php: line 3:function get_file_list($folder) {’

Thanks for your continued help.

-Matttail


#14

I’m scratching my head here. I’ll have a fresh look at it tomorrow - I’m busy watching the lame NASCAR awards show LOL.


Simon Jessey
Keystone Websites (business site) | si-blog (personal site with affiliate links)


#15

Cool, no rush. The script is still at http://php5.googlies.net, the images are at http://php5.googlies.net/images/ and I used the cp command from ssh to create http://php5.googlies.net/index.txt so you can see the script as I’m using it, incase the error is something I did.

-Matttail


#16

I actually dreamed about this problem in the middle of the night. Do you ever code in your dreams? I do all the time, and more often than not, I actually solve problems that way. There was something you said earlier that was haunting me:

And then it hit me. PHP may be an interpreted language, but one of the cool things about it is that it is still fast. We must be overwriting the files, because we are renaming them so quickly they are all getting the same filename. What’s more, they probably aren’t in order either, because when you copied the files, they all got the same “Last Modified” date and time.

All I can think of is to modify the script to add a unique identifier to each file, instead of relying on the “last modified” date/time:

[code]<?php
// create array of filenames sorted by modification time
function get_file_list($folder) {
$dircontent = scandir($folder);
$arr = array();
foreach($dircontent as $filename) {
if ($filename != ‘.’ && $filename != ‘…’) {
if (filemtime($folder.$filename) === false) return false;
$dat = date(“YmdHis”, filemtime($folder.$filename));
$arr[$dat] = $filename;
}
}
if (!ksort($arr)) return false;
return $arr;
}

// display or rename files
$path = $_SERVER[‘DOCUMENT_ROOT’]."/images/";
$file_array = get_file_list($path);
$counter = 1;
foreach($file_array as $key => $value) {

// display list
// print “$value - Last modified: $key
”;

// create a zero-padding string from $counter
$string = sprintf("%010s",$counter);

// rename files like “a-0000000000.jpg”, “a-0000000001.jpg” and so on
// rename("$path/$value","$path/a-$string.jpg");
// $counter ;
}
?>[/code]I had to make the % sign bold because the rendering engine of this forum doesn’t have the correct settings for dealing with percentages followed by numbers.

I’m not certain if this is going to solve your problem, but at least it doesn’t rename everything the same!


Simon Jessey
Keystone Websites (business site) | si-blog (personal site with affiliate links)


#17

I’ve been kinda busy with college recently and couldn’t work on this. I tried the new script and it still wasn’t working properly - then I checked the modify time for the files I uploaded again via FTP, and it’s loosing the origional modify times from my computer. And becuase filezilla opens multiple connections for upload some of the files are created at exactly the same time - so obviusly this messes with the script.

What I’m wondering is if I create an upload script with php and html, what order will the files be uploaded in? For example, if I have this script with 5 upload fields and I select the files in the order I want them, then can I somehow carry out the upload and append a sequential number to the files, all in the order I select the files in?

The other thought that I had, is that if that part of the file is hexadecimal, can that be ‘translated’ and rename the files with whole numbers - thus listing it properly in the first place?

One other idea I had is if there’s some FTP program that will upload the files and maintain the modify time. I can’t find any such setting in filezilla, and a google search didn’t reveal much, so not sure about this one.

Any ideas?

-Matttail