473,698 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Renaming File Extensions

11 New Member
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 6509
savanm
85 New Member
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 Recognized Expert Specialist
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 New Member
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 Recognized Expert Top Contributor
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
1976
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 new name gets displayed through refreshing of my loop and display function. The problem is that when, if I rename the last file entry, everything appears to display fine. If I rename a file above the last entry (bottom entry), two of the new file...
6
5938
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 name, but it truncates and renames it in the 8.3 format. If the newly formed name is "LongFilename.ext", the file gets renamed to "LONGFILE.EXT". I'm currently using the following statements to rename the file: sprintf( buf, "ren %s %s",...
0
1346
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 email: sNewFileName = fPath + "Front" + fInfo.Extension; fInfo.CopyTo(sNewFileName, true); fInfo.Delete(); oMail.Attachments.Add(new MailAttachment(sNewFileName));
2
6414
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 = ConfigurationSettings.AppSettings; Thanks!
3
3163
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 I find a list of all the types which can be returned? For example, if it is a Word document will it return "document/doc".
5
4711
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 attempts: OldName = "c:\albums\061203\email\DSC07272.JPG" NewName = "c:\albums\061203\email\Lalala.JPG"
2
151650
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 application, which contains a button and a label. When the button is clicked, some message is shown on the label. That's it. The AJAX works great on my development machine, but on the production server, I got the typical error as follows:
6
2171
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 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
38
5067
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
1775
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 have to open all the files with a set of extensions( .txt,.doc etc) files in a directory using only the extensions as the actual name of the files is not available ..can that be done ????
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.