Web Hosting Talk







View Full Version : File Renaming


akaize
03-11-2002, 03:28 PM
In my site I have many files and I need a command to rename it automatically, it goes like this

The files I have now are names..

k201.jpg
k202.jpg
k203.jpg
k204.jpg

I need to rename it...
201t.jpg
202t.jpg
203t.jpg
204t.jpg

Removing the k from the begining and adding a t at the last of every file name.

My server is a Linux, how can I do it from command prompt or by a PHP script?

Ahmad
03-11-2002, 04:19 PM
This is a PHP script that does that:


<?php
$directory = "/path/to/file";

$d = opendir($directory);
while (($original = readdir($d)) !== false) {
rename($original,ereg_replace('k([0-9]{3})\\.jpg','\\1t.jpg',$original));
}
closedir($d);
?>


EDIT:

1) The directory must only include these files.
2) vB seems to have problem rendering the program .:confused:

Ahmad
03-11-2002, 04:24 PM
Should be fine now, copy (backup) the files before you apply, I didn't have time to test it .. sorry.

BTW, you must have write permission to the files to be able to apply the changes. I suggest that you run it from shell with the PHP binary.

This is usually done by adding #!/path/to/php/interperter to the top of the file and chmoding it "u+x".

Tim Greer
03-11-2002, 04:46 PM
Originally posted by akaize
In my site I have many files and I need a command to rename it automatically, it goes like this

The files I have now are names..

k201.jpg
k202.jpg
k203.jpg
k204.jpg

I need to rename it...
201t.jpg
202t.jpg
203t.jpg
204t.jpg

Removing the k from the begining and adding a t at the last of every file name.

My server is a Linux, how can I do it from command prompt or by a PHP script?

Command line (in the directory you need to rename them, run this command):

This will change k(anything).jpg to (anything)t.jpg -- numbers, letters, etc. or any length:

for file in k*.jpg; do mv $file `echo $file | sed 's/^k\(.*\).jpg$/\1t.jpg/'`; done

If you only want to change any file names that are specifically k(numbers-here).jpg to (numbers-here)t.jpg, do this:

for file in k*.jpg; do mv $file `echo $file | sed 's/^k\([0-9]*\).jpg$/\1t.jpg/'`; done

This way, you don't have to worry about ownership issues, like you would with PHP (depending on how you run it). BTW, do you want to limit it to only match and replace anything's that's specifically k###.jpg, or do you want it to be able to match and rename k#abc.jpg or just k#######.jpg? I.e., if you have k####.jpg in the directory that you don't want renamed, but want to just rename all the k###.jpg (3 digit only)? If so, I'll post that option too. Cheers!

Tim Greer
03-11-2002, 04:58 PM
[QUOTE]Originally posted by Ahmad
[B]This is a PHP script that does that:



[SNIP]

rename($original,ereg_replace('k([0-9]{3})\\.jpg','\\1t.jpg',$original));
}


This user should be aware (even though you are), that the above example only changes numbers and only if it's kNNN.jpg -- i.e., it won't match kabc.jpg nor will it match k9002.jpg (which might be something they don't care about, or maybe it is -- which reminds me that I better edit my post to reflect some different ways to use sed. :-)

jahsh
03-11-2002, 08:17 PM
if there are only a few of them and you dont feel comfortable using these scripts (or if they dont work) just use the mv command:
mv file1 file2

akaize
03-12-2002, 04:42 PM
Thanks all for your help. I managed with the command prompt suggested by Tim_Greer