473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Renaming File Extensions

11
i am a total n00b at perl. i just started learning yesterday. I wanted to make a script to rename extentions. this is what i've come up with. i can't figure out why it doesn't work. any help would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. print "What ending would you like to change?";
  4. $ending = <STDIN>;
  5. chomp($ending);
  6.  
  7. print "What would you like to change it to?";
  8. $new_ending = <STDIN>;
  9. chomp($new_ending);
  10.  
  11. print "What is the directory you would like to change this in?";
  12. $directory = <STDIN>;
  13. chomp($directory);
  14.  
  15. print $ending, " ", $new_ending, " ", $directory, "\n";
  16.  
  17. chdir $directory;
  18. @list = `ls *.$ending`;
  19.  
  20. foreach (@list) {
  21.     chop;
  22.     rename (".$ending", ".$new_ending") || die "Cannot delete $ending";
  23. };
  24. print "\n";
  25.  
  26.  
Apr 10 '07 #1
4 6487
savanm
85
Hi,

Expand|Select|Wrap|Line Numbers
  1. use Cwd;
  2.  
  3. $path=getcwd();
  4. $path=~s/\\/\//sg;
  5. print $path;
  6.  
  7. opendir(DIR,$path) || die("Cannot open thedirectry");
  8. @storage = grep(/\.xml/,readdir(DIR));
  9. close(DIR);
  10.  
  11. foreach $fil(@storage) {
  12.     submain($path."\/".$fil);
  13. }
  14.  
  15. sub submain() {
  16.     $xml = shift;
  17.     $txt = $xml;
  18.     $txt=~s/\.xml/\.txt/sgi;
  19.     local $/;
  20.     open(FILE,$xml);
  21.     while(<FILE>) {
  22.         $temp=$_;
  23.     }
  24.     open(OUT,">$txt");
  25.     print OUT $temp;
  26.     close(OUT);
  27.  
  28. }
  29.  

Try this this is used to change the extension of the file .xml to .txt

then tell ur need briefly
Apr 10 '07 #2
KevinADC
4,059 Expert 2GB
You need the full filename:

Expand|Select|Wrap|Line Numbers
  1. rename ("fullname.$ending", "fullname.$new_ending") || die "Cannot delete $ending";

also, probably safer to use "chomp" instead of "chop" athough it may not matter in this case. But chomp is really the correct function for removing the record seperator, chop blindy removes the last charater on the end of the string regardless of what the character is.
Apr 10 '07 #3
bdan44
11
hi,
what I want the code to do is prompt the user for the extensions they would like to change (if they wanted to change from .mpg to .mpeg they could or from .jpg to .jpeg they could, all with the same script).
Apr 10 '07 #4
miller
1,089 Expert 1GB
Hi bdan44,

As Kevin pointed out, you need to give an explicit file name to the rename function, not just the extension.

Also, your rename won't work as you desire unless you extract the non extention part of the file. You can use either a regex, substr, or a core library for that. I'd suggest a core library.

Here is your code fixed and cleaned up by adding "use strict;".

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use File::Basename qw(fileparse);
  4.  
  5. use strict;
  6.  
  7. print "What ending would you like to change?";
  8. my $old_ending = <STDIN>;
  9. chomp($old_ending);
  10.  
  11. print "What would you like to change it to?";
  12. my $new_ending = <STDIN>;
  13. chomp($new_ending);
  14.  
  15. print "What is the directory you would like to change this in?";
  16. my $directory = <STDIN>;
  17. chomp($directory);
  18.  
  19. print $ending, " ", $new_ending, " ", $directory, "\n";
  20.  
  21. chdir $directory;
  22. my @list = `ls *.$ending`;
  23.  
  24. foreach (@list) {
  25.     chomp;
  26.     my $name = fileparse($_, ".$old_ending");
  27.     rename("$name.$old_ending", "$name.$new_ending") or die "Cannot delete $ending";
  28. };
  29. print "\n";
  30.  
  31.  
(Not tested)

- Miller
Apr 10 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: MikeY | last post by:
Hopefully someone can help, I have a listview box where I display my desired files. I single click on the desired file to be renamed and I rename it with a new name. My problem arises when the...
6
by: Pegboy | last post by:
I am trying to create a DOS utility that will extract data from a file and use it to form a new filename for that same file. I can successfully open the file, get the data I need and form the new...
0
by: Andy | last post by:
Hello: I am using System.Web.Mail.MailMessage. Currently: when I am attaching a file to the object, I am renaming the file using the FileInfo.Copy method and attaching the new file to the...
2
by: cloudx | last post by:
Hi there, it is driving me crazy. Aftering renaming app.config the following code I always get null on myApp. It works if I keep the name app.config. Why? string myApp =...
3
by: Shapper | last post by:
Hello, I created a script to upload a file. To determine the file type I am using userPostedFile.ContentType. For example, for a png image I get "image/png". My questions are: 1. Where can...
5
by: bulldog8 | last post by:
I've read numerous posts and have tried multiple approaches that I found, but just cannot get a file renamed. I am using VB.Net 2002 ... Here is what I have tried: 1) Code common to all...
2
by: antonyliu2002 | last post by:
I am testing AJAX. I've downloaded the AJAX Extension and the CTP December package and installed on BOTH my development machine and the production server. Then I created a very very simple web...
6
by: laredotornado | last post by:
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...
38
by: ted | last post by:
I have an old link that was widely distributed. I would now like to put a link on that old page that will go to a new page without displaying anything.
3
by: mohi | last post by:
hello every one , i want to open a file and then do some processing on it and finally change its name or append something to its name and then save it back . can anyone please help! and also i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.