473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

rename function not working ???

Hi guys.
I have like hundreds of pictures in a folder
and then I use this function to rename them
all to a following number. but very often I
noticed that some pictures get lost and I
dont know where ? anyone can tell me
whats wrong ?

$dir = getcwd()."/dump/";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if(($filename <> ".")&&($filename <> "..")){
$pic_ctr++;
rename("dump/".$filename, "dump/".$pic_ctr.".jpg");
}
}

thanx for your help !
Feb 23 '06 #1
3 5338
Yannick Benoit wrote:
Hi guys.
I have like hundreds of pictures in a folder
and then I use this function to rename them
all to a following number. but very often I
noticed that some pictures get lost and I
dont know where ? anyone can tell me
whats wrong ?

$dir = getcwd()."/dump/";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if(($filename <> ".")&&($filename <> "..")){
$pic_ctr++;
rename("dump/".$filename, "dump/".$pic_ctr.".jpg");
}
}

thanx for your help !


Idea: what's the value of $pic_ctr *before* the ++?

Feb 23 '06 #2
Yannick Benoit wrote:
Hi guys.
I have like hundreds of pictures in a folder
and then I use this function to rename them
all to a following number. but very often I
noticed that some pictures get lost and I
dont know where ? anyone can tell me
whats wrong ?

$dir = getcwd()."/dump/";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if(($filename <> ".")&&($filename <> "..")){
$pic_ctr++;
rename("dump/".$filename, "dump/".$pic_ctr.".jpg");
}
}

thanx for your help !


not tested, but should work.

$pic_ctr=0; //initialize the picture counter

// if we successully open the dir then continue
if ($dir = @opendir("./dump"))
// we are using relative path
//or you can specify full pathname.
// for example "/home/mydir/dump"
{
while($filename = readdir($dir))

//if filename is a .jpg then rename it...
//sometimes better to check what the name is
// as opposed to what the name is not (your code)

if (eregi("^[a-zA-Z0-9_]+\.jpg", $filename))
{$pic_ctr++;
rename("./dump/".$filename, "./dump/".$pic_ctr.".jpg");
}
}

closedir($dir);
Feb 23 '06 #3
Yannick Benoit wrote:
Hi guys.
I have like hundreds of pictures in a folder
and then I use this function to rename them
all to a following number. but very often I
noticed that some pictures get lost and I
dont know where ? anyone can tell me
whats wrong ?

$dir = getcwd()."/dump/";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if(($filename <> ".")&&($filename <> "..")){
$pic_ctr++;
rename("dump/".$filename, "dump/".$pic_ctr.".jpg");
}
}

thanx for your help !


Do some of the files already have numbers? If you try to rename a file,
to, i.e. 2.jpg and there already is a 2.jpg, the rename will fail.

Also, as others have pointed out - did you initialize $pic_ctr before
using it?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jerry Orr | last post by:
I'm writing a simple JNI C++ function on a z/OS 1.4 system. I am attempting to rename a dataset; however, when I use the rename() function, I get a return code of -1. All of the return codes listed...
16
by: dudufigueiredo | last post by:
I have one folder containing mp3 files, the folder is: C:\My Shared Folder\Rubber Soul And the files are: 01 drive my car.mp3 02 norwegian wood.mp3 03 you won't see me.mp3 04 nowhere man.mp3...
2
by: Bill Stock | last post by:
I've been renaming the ALL high level objects in a database and searching out the dependent objects to change the references. This was all going fairly well, until DoCmd.Rename stopped working on...
1
by: Jay at SCA | last post by:
I've been having the following issue with a windows service I've created in vb.net (.net fw 1.1): Basically, my service monitors a folder for the creation of any new files of a particular type...
5
by: Rothariger | last post by:
Hello.... i want to know if its posible to rename multiple files like windows does.. example: file zzzzzzz.doc file asdasd.doc file esfsefse.doc
5
by: Tony Meyer | last post by:
On Windows, if I do os.rename(old, new) where old is a file that is in-use (e.g. python itself, or a dll that is loaded), I would expect that an error would be raised (e.g. as when os.remove is...
8
by: Merlin | last post by:
Ok.... I feel really dumb on this one, because I had previously figured it out, and now don't have a clue. I'm trying to get the user to input a new filename (via an integer variable) and...
12
by: mantrid | last post by:
Im trying to move a file but am having luck my code is below. The temp and target paths are valid as they echo correctly. but I cant get the copy() function to work, or the rename() function ...
1
by: Alien | last post by:
Hi, I am new to PHP and trying to move a zip file from one directory to another. I researched on the web on this and all fingers pointed to this rename() function. I tried writing a PHP script...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.