Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

How do you add .RTF to a file

by skyler (Beadle)
on Jun 05, 2003 at 13:40 UTC ( #263322=perlquestion: print w/replies, xml ) Need Help??

skyler has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I don't know how to add an extension to a file that doesn't have one. I tried pattern matching but it doesn't do anything. Do you have any ideas? For example: "file1" needs to change to "file1.rtf"
$newfile =~ s/\^$/.rtf/;

Replies are listed 'Best First'.
Re: How do you add .RTF to a file
by hmerrill (Friar) on Jun 05, 2003 at 13:54 UTC
    Try to be more clear - do you actually want to rename an existing file from "file1" to "file1.rtf"? If you do, than perl's 'rename' function should work.

    perldoc -f rename
    at a command prompt to read the perldocs on how to use 'rename' - it's pretty much what you would expect.

    Also, you are not using the substitution operator properly - do
    perldoc perlop
    at a command prompt and search (using the forward slash "/") for 'subst' - you'll see all the info you need.

Re: How do you add .RTF to a file
by hardburn (Abbot) on Jun 05, 2003 at 13:51 UTC

    You append the string:

    my $oldfile = 'file1'; my $newfile = $oldfile . '.rtf';

    No pattern matching required.

    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: How do you add .RTF to a file
by arthas (Hermit) on Jun 05, 2003 at 14:32 UTC

    If you need to rename a file with no extension you just need to say:

    rename $myfile, $myfile.'.rtf';

    If you just need to append .rtf to a string containing a filename then write:

    $myfile = $myfile.'.rtf';


Re: How do you add .RTF to a file
by RollyGuy (Chaplain) on Jun 05, 2003 at 13:53 UTC
    You simply need concatination, not pattern matching. To concat two strings, you can use the "." operator as follows:
    $file = "File1"; $file .= ".rtf"; or something more like $basename = "File1"; $file = $basename . ".rtf";
Re: How do you add .RTF to a file
by Missing Words (Scribe) on Jun 05, 2003 at 14:47 UTC
    Do you want to change just one specific file? If that is so just use the methods already explained to you. However, if you want to look through a directory and then change any files that don't have an extension on them you could try something like this:
    #!perl use strict; my @file = glob "*"; foreach(@file){ if(/\./){next;} else{ rename $_ , $_ . ".rtf"; } }
    If you need to change the files in different directories you could simply recieve a command line argument that would contain the directory you need to evaluate and the do a chdir $ARGV[0], or the equivalent.
      Sorry, it's my first time posting here. Forgot to put the code tags around the chdir suggestion it should look like this:
      chdir "$ARGV[0]";
Re: How do you add .RTF to a file
by jonnyfolk (Vicar) on Jun 05, 2003 at 14:00 UTC

    Addenda: Hmmm - said (0 replies)!

Re: How do you add .RTF to a file
by young_david (Friar) on Jun 05, 2003 at 14:30 UTC
    If you wanted to do pattern matching:

    my $oldfile = 'file1'; my $newfile =~ s/$/.rtf/; # Search for the end of a string and # replace it with the extension
      UPDATE: Sorry, I just realized I made a mistake, should have written like this:
      my $file = 'file1'; $file =~ s/$/.rtf/; # $file will now contain the .rtf extension

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://263322]
Approved by Corion
Front-paged by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2023-03-31 15:34 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (76 votes). Check out past polls.