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. -
#!/usr/bin/perl
-
-
print "What ending would you like to change?";
-
$ending = <STDIN>;
-
chomp($ending);
-
-
print "What would you like to change it to?";
-
$new_ending = <STDIN>;
-
chomp($new_ending);
-
-
print "What is the directory you would like to change this in?";
-
$directory = <STDIN>;
-
chomp($directory);
-
-
print $ending, " ", $new_ending, " ", $directory, "\n";
-
-
chdir $directory;
-
@list = `ls *.$ending`;
-
-
foreach (@list) {
-
chop;
-
rename (".$ending", ".$new_ending") || die "Cannot delete $ending";
-
};
-
print "\n";
-
-
4 6509
Hi, -
use Cwd;
-
-
$path=getcwd();
-
$path=~s/\\/\//sg;
-
print $path;
-
-
opendir(DIR,$path) || die("Cannot open thedirectry");
-
@storage = grep(/\.xml/,readdir(DIR));
-
close(DIR);
-
-
foreach $fil(@storage) {
-
submain($path."\/".$fil);
-
}
-
-
sub submain() {
-
$xml = shift;
-
$txt = $xml;
-
$txt=~s/\.xml/\.txt/sgi;
-
local $/;
-
open(FILE,$xml);
-
while(<FILE>) {
-
$temp=$_;
-
}
-
open(OUT,">$txt");
-
print OUT $temp;
-
close(OUT);
-
-
}
-
Try this this is used to change the extension of the file .xml to .txt
then tell ur need briefly
KevinADC 4,059
Recognized Expert Specialist
You need the full filename: - 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.
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).
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;". -
#!/usr/bin/perl
-
-
use File::Basename qw(fileparse);
-
-
use strict;
-
-
print "What ending would you like to change?";
-
my $old_ending = <STDIN>;
-
chomp($old_ending);
-
-
print "What would you like to change it to?";
-
my $new_ending = <STDIN>;
-
chomp($new_ending);
-
-
print "What is the directory you would like to change this in?";
-
my $directory = <STDIN>;
-
chomp($directory);
-
-
print $ending, " ", $new_ending, " ", $directory, "\n";
-
-
chdir $directory;
-
my @list = `ls *.$ending`;
-
-
foreach (@list) {
-
chomp;
-
my $name = fileparse($_, ".$old_ending");
-
rename("$name.$old_ending", "$name.$new_ending") or die "Cannot delete $ending";
-
};
-
print "\n";
-
-
(Not tested)
- Miller
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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",...
|
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));
|
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!
|
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".
| |
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"
|
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:
|
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
|
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.
|
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 ????
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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();...
| |
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
|
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...
| |