Page 1 of 1

Loop Thru and Rename Files

Posted: Thu Aug 08, 2002 7:20 am
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??

Posted: Thu Aug 08, 2002 7:50 am
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.

Posted: Fri Aug 09, 2002 4:57 am
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?

Posted: Fri Aug 09, 2002 5:29 am
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

Posted: Fri Aug 09, 2002 6:15 am
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.

Posted: Fri Aug 09, 2002 7:02 am
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.

Posted: Fri Aug 09, 2002 10:29 am
by Zeceer
This ain't supposed to be here. Sorry!

Posted: Fri Aug 09, 2002 10:30 am
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++;
}

?>