Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

How can i change the file extension?

by littlemonk (Sexton)
on Sep 23, 2012 at 19:06 UTC ( #995234=perlquestion: print w/replies, xml ) Need Help??
littlemonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, How can i change file extension of a file using perl ..suppose i have some files with ".txt" extension in a directory i wanna change these file to ".html" how can i do that in perl?

Replies are listed 'Best First'.
Re: How can i change the file extension?
by roboticus (Chancellor) on Sep 23, 2012 at 19:19 UTC
Re: How can i change the file extension?
by Anonymous Monk on Sep 23, 2012 at 19:17 UTC
    Take a look at the 'rename' function (perldoc -f rename).
    foreach my $file (glob '/path/*.txt') { (my $new_file = $file) =~ s{txt\z}{html}; rename $file => $new_file; }
Re: How can i change the file extension?
by pvaldes (Chaplain) on Sep 23, 2012 at 22:12 UTC

    To change the file extension is easy, either renaming or i.e:

    use File::Copy; copy("text.txt","text.html")

    But note that this is not the same as to change the type of file.

    So you probably want instead to convert your files to html files, and this is a different problem. Take a look to the module TextToHTML

    If you use GNU the file extension is not really relevant, you could have a jpg named file.txt or file.pdf and will be perfectly recognised as image

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995234]
Approved by Corion
[Corion]: :) I had to reconnect my DVD drive to actually burn the CD-R - I think I last spun it up three years ago or something...
Discipulus must burn phot cd to send to mother in law..
[Corion]: I wonder, maybe the time is right to offer a CD burn service - you send the files, I burn them onto CD-R and mail it to whoever you specified. A printed CD costs extra ;)
[Corion]: Just because there are things that you can't do/share via Facebook. Or even better, you mail me the iPhoto / Facebook page and I create and mail a Photo CD from them ;)

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (15)
As of 2017-11-20 18:47 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (291 votes). Check out past polls.