Changing the extensions of multiple files using Perl Regex?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Davo1977
    New Member
    • Jun 2008
    • 3

    Changing the extensions of multiple files using Perl Regex?

    Does anyone know a regular expression that will rename multiple files that have different extensions to have the same extension. For example, you could use this code when several text files exist in a directory but have slightly different extensions such as .txt,. TXT, or text. This expression should show how to rename them to all have .txt extensions.


    I understand i will nedd [] the character class so it contains a hypen, dot, slash and w+ d+ to show the filename to be of any charcter and digit
    What else do i need to include in the expression?

    Regards, David.
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    If you want the match to be case-insensitive the you can use the i flag.

    There are several perl docs that cover regular expression.
    perlretut
    perlrequick
    perlfaq6
    perlop

    Also check out the perl section in the "Howtos" for some additional tutorials.

    --Kevin
    Last edited by eWish; Aug 4 '08, 09:01 PM. Reason: Removed Code Samples

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Originally posted by Davo1977
      Does anyone know a regular expression that will rename multiple files that have different extensions to have the same extension. For example, you could use this code when several text files exist in a directory but have slightly different extensions such as .txt,. TXT, or text. This expression should show how to rename them to all have .txt extensions.


      I understand i will nedd [] the character class so it contains a hypen, dot, slash and w+ d+ to show the filename to be of any charcter and digit
      What else do i need to include in the expression?

      Regards, David.

      No doubt in my mind this is school or class work.

      Regular expressions can not rename files, only find the files you are looking for. To rename a file you have to use the rename() function.

      Comment

      • eWish
        Recognized Expert Contributor
        • Jul 2007
        • 973

        #4
        As Kevin has pointed out you will need the rename() function. In addition to that I would suggest using File::Find or File::Find::Wan ted modules to located the files that you are after. I prefer the latter of the two.

        --Kevin

        Comment

        Working...