Renaming a file but preserving the extension

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    Renaming a file but preserving the extension

    Hi,

    A question for those of you who always seem to know the one line way
    of doing things. I'm using php 4.4.4 and I want to rename the first
    part of the file to a particular string. For example, if my input
    string were

    $input = "/path/to/my/file/12345.jpg";

    I would want my result to be "/path/to/my/file/header.jpg", assuming
    "header" was my fixed string.

    Any slick ways to do this?

    Thanks, - Dave

  • Toby A Inkster

    #2
    Re: Renaming a file but preserving the extension

    laredotornado@z ipmail.com wrote:
    Renaming a file but preserving the extension
    How do you define "extension" ? In the file "nedit-5.5-src.tar.gz" which of
    these parts is the "extension" ?

    .gz
    .tar.gz
    .5-src.tar.gz

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
    Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

    * = I'm getting there!

    Comment

    • laredotornado@zipmail.com

      #3
      Re: Renaming a file but preserving the extension

      On Feb 23, 1:56 pm, Toby A Inkster <usenet200...@t obyinkster.co.u k>
      wrote:
      laredotorn...@z ipmail.com wrote:
      Renaming a file but preserving the extension
      >
      How do you define "extension" ? In the file "nedit-5.5-src.tar.gz" which of
      these parts is the "extension" ?
      >
      .gz
      .tar.gz
      .5-src.tar.gz
      >
      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~http://tobyinkster.co.uk/contact
      Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
      >
      * = I'm getting there!
      Good question. You can assume for the purposes of this question that
      extension is anything following the "." in which there will be exactly
      one "." in the file name.

      - Dave

      Comment

      • Toby A Inkster

        #4
        Re: Renaming a file but preserving the extension

        laredotornado@z ipmail.com wrote:
        Good question. You can assume for the purposes of this question that
        extension is anything following the "." in which there will be exactly
        one "." in the file name.
        $x = pathinfo('/path/to/file.txt');
        $X = $x['dirname'] . '/' . 'foo' . '.' . $x['extension'];
        print $X;

        --
        Toby A Inkster BSc (Hons) ARCS
        Contact Me ~ http://tobyinkster.co.uk/contact
        Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

        * = I'm getting there!

        Comment

        • Curtis

          #5
          Re: Renaming a file but preserving the extension

          Toby A Inkster wrote:
          laredotornado@z ipmail.com wrote:
          >
          >Good question. You can assume for the purposes of this question that
          >extension is anything following the "." in which there will be exactly
          >one "." in the file name.
          >
          $x = pathinfo('/path/to/file.txt');
          $X = $x['dirname'] . '/' . 'foo' . '.' . $x['extension'];
          print $X;
          >
          A little more involved, but this will allow for extensions like tar.gz
          or tar.bz2, etc. It gets the position in the file name, of the first
          ".", and keeps everything after.

          $p = '/path/to/archive.tar.gz' ;
          $file = basename($p);
          $p2 = dirname($p) . '/newname' . substr($file,st rpos($file,'.') );
          echo $p2;

          --
          Curtis, http://dyersweb.com

          Comment

          • Toby A Inkster

            #6
            Re: Renaming a file but preserving the extension

            Curtis wrote:
            A little more involved, but this will allow for extensions like tar.gz
            or tar.bz2, etc. It gets the position in the file name, of the first
            ".", and keeps everything after.
            [...]
            What about "nedit-5.5-src.tar.gz"? Your code will rename it to
            "newname.5-src.tar.gz" whereas ideally it would be named "newname.tar.gz ".

            --
            Toby A Inkster BSc (Hons) ARCS
            Contact Me ~ http://tobyinkster.co.uk/contact
            Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

            * = I'm getting there!

            Comment

            • Curtis

              #7
              Re: Renaming a file but preserving the extension

              Toby A Inkster wrote:
              Curtis wrote:
              >
              >A little more involved, but this will allow for extensions like tar.gz
              >or tar.bz2, etc. It gets the position in the file name, of the first
              >".", and keeps everything after.
              >[...]
              >
              What about "nedit-5.5-src.tar.gz"? Your code will rename it to
              "newname.5-src.tar.gz" whereas ideally it would be named "newname.tar.gz ".
              >
              Oh, I see what you meant now. I guess, if needed, one could store some
              file extensions in an array to aid with more accurate renaming. Hehe,
              I thought I had it. :P

              --
              Curtis, http://dyersweb.com

              Comment

              Working...