Loop Thru and Rename Files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Loop Thru and Rename Files

Post by Zeceer »

Hi,

I'm having a script that includes all the files in a directory. The files are calles file1, file2, file3 and so on. So the include script is just a loop starting at 1 and goes on untill theres no files left. But that's not the problem.

The problem is that i'm making an online admin section where I should be able to delete a file. One of the file1, file2, file3 and so on. But if I delete on of this then the include script want loop thru the all. Let's say I delete file2. Then the include script will loop only once and out put file1. But in the directory there are 15 files. Now called file1, file3, file4 and so on because the file file2 is deleted and the include script stops when a file doesn't exist.

I nees to make something that will loop thru and rename alle the files to file1, file2, file3, file4 and so on. So that the include script will work.

One solution i've though of is to set the include script to count have many files there are in the directory and the loop that many times, but i haven't got that to work either.

Could someone please help me??
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

My algorithm (in pseudocode) might look like this:

Code: Select all

$file = 'file1';
while(is_file($file)) {
  increment file number;
}
while(is_file(increment file number)) {
  $temp = decrement file number;
  rename($temp);
}
If you're sure that you're only going to be deleting one file at a time, this is a nice simple algorithm. If not, you'll have to get fancier.
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Parse error: parse error, unexpected T_STRING in D:\inc shop\produkter\test.php on line 4

i what i get. I've added some PHP tags so it's line four.

Is it possible to make a script that count's the files inside a folder? And makes a number out of it?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

llimllib's code is an algorithm which uses pseudocode which means it isn't a working example but you should be able to make a start using it.

For functions that can be used when working with files and folders:
http://www.php.net/manual/en/ref.filesystem.php
http://www.php.net/manual/en/ref.dir.php

Mac
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

where i say 'increment file number', I left that undefined because it sounded like (a) you had figured out how to do it already and (b) that's something I figured you'd want exact control of. In the example system you mentioned, it means turning 'file1' into 'file2', while 'decrement file number' means turning file2 into file1. Another reason I didn't include it is that it probably makes sense to turn these into functions, that way you can tweak it easily.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Why not just use the opendir(), readdir(), and closedir() functions to loop through the files? It wouldn't matter what the file was called, and it would always loop through ALL the files.
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

This ain't supposed to be here. Sorry!
Last edited by Zeceer on Fri Aug 09, 2002 10:31 am, edited 1 time in total.
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

ok, so I can use the readdir() function and make it loop thru every file. And then include the file to the browser? I've never used but readdir() to include the files into the browser before, just to print the name og the file. The anoying thing about readdir() is that you get

.
..

on the top. Don't know if they get there when I'm including the files in the folder, but I'll try that now. Thanx for the tip :-).

Heres the include the files code:

<?php

$x = "1";

while ( $x )
{
$includefile = "produkter/produkt$x".".php";

if( file_exists("$includefile") )
{
echo("<br>");
include("$includefile");
echo("<hr noshade>");
}
else
{
break;
}


$x++;
}

?>
Post Reply