rename function not working ???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yannick Benoit

    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 <> ".")&&($filenam e <> "..")){
    $pic_ctr++;
    rename("dump/".$filename , "dump/".$pic_ctr.".jp g");
    }
    }

    thanx for your help !


  • David Haynes

    #2
    Re: rename function not working ???

    Yannick Benoit wrote:[color=blue]
    > 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 <> ".")&&($filenam e <> "..")){
    > $pic_ctr++;
    > rename("dump/".$filename , "dump/".$pic_ctr.".jp g");
    > }
    > }
    >
    > thanx for your help !
    >
    >[/color]

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

    Comment

    • noone

      #3
      Re: rename function not working ???

      Yannick Benoit wrote:[color=blue]
      > 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 <> ".")&&($filenam e <> "..")){
      > $pic_ctr++;
      > rename("dump/".$filename , "dump/".$pic_ctr.".jp g");
      > }
      > }
      >
      > thanx for your help !
      >
      >[/color]

      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.".jp g");
      }
      }

      closedir($dir);

      Comment

      • Jerry Stuckle

        #4
        Re: rename function not working ???

        Yannick Benoit wrote:[color=blue]
        > 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 <> ".")&&($filenam e <> "..")){
        > $pic_ctr++;
        > rename("dump/".$filename , "dump/".$pic_ctr.".jp g");
        > }
        > }
        >
        > thanx for your help !
        >
        >[/color]

        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.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        Working...