CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to replace filename?

 



geek.shrek
Novice

Aug 11, 2009, 5:29 PM

Post #1 of 18 (1362 views)
How to replace filename? Can't Post

Hi,

I would like to replace for example "abc_def_ghi.wmv" into "abc_def_ghi.xml"

how can I do this?

Thanks,


FishMonger
Veteran / Moderator

Aug 11, 2009, 5:32 PM

Post #2 of 18 (1361 views)
Re: [geek.shrek] How to replace filename? [In reply to] Can't Post

File::Copy
http://search.cpan.org/~nwclark/perl-5.8.9/lib/File/Copy.pm


geek.shrek
Novice

Aug 11, 2009, 5:39 PM

Post #3 of 18 (1359 views)
Re: [FishMonger] How to replace filename? [In reply to] Can't Post

Thanks ,

but I don't want to copy the file.
I'm just need the filename and add ".xml" at the end.

Any suggestions?


FishMonger
Veteran / Moderator

Aug 11, 2009, 5:42 PM

Post #4 of 18 (1358 views)
Re: [geek.shrek] How to replace filename? [In reply to] Can't Post

There are several ways I can suggest, but what have you tried?


KevinR
Veteran


Aug 11, 2009, 5:45 PM

Post #5 of 18 (1356 views)
Re: [FishMonger] How to replace filename? [In reply to] Can't Post


In Reply To
There are several ways I can suggest, but what have you tried?


I second the motion. I already gave you one answer, this time I would like to see some effort on your part.
-------------------------------------------------


geek.shrek
Novice

Aug 11, 2009, 6:28 PM

Post #6 of 18 (1350 views)
Re: [KevinR] How to replace filename? [In reply to] Can't Post

Yes,
Thank you for the answer.

It's still doesn't work though :)

Here what I have


Code
sub textParse 
{
my($text) = @_;
my $temp = $text;
$temp =~ s/(\.wmv)$/(\.xml)/;

print $temp;

}


Thanks,


ichi
User

Aug 11, 2009, 7:21 PM

Post #7 of 18 (1346 views)
Re: [geek.shrek] How to replace filename? [In reply to] Can't Post

we are not psychics... if it doesn't work, show why it doesn't work. show how you execute your script. From what you posted and nothing else, one tends to see that its only a subroutine. so where do you call it in your main script? show any output errors if there is.....


spider
User


Aug 11, 2009, 10:56 PM

Post #8 of 18 (1342 views)
Re: [geek.shrek] How to replace filename? [In reply to] Can't Post

So you have a subroutine that changes that prints the new name.
I would change that to using return in sted of print, so that the new name is returned back to the function call.
Then you could use it with a function call like this:

Code
my($new_filename) = textParse($old_filename) ;

or just

Code
$filename = textParse($filename);


This does not do anything with the files on disk.
Have you looked at file::copy as FishMonger sugested?


vikas.deep
User

Aug 12, 2009, 3:13 AM

Post #9 of 18 (1338 views)
Re: [geek.shrek] How to replace filename? [In reply to] Can't Post

Dear you


Code
$temp =~ s/(\.wmv)$/(\.xml)/;

Why are you using brackets and making things difficult here
Can't you just use

Code
$temp =~ s/(\.wmv)$/\.xml/;

Now as you were doing it

Code
$mystring = 'abc_def_ghi.wmv';  
$mystring =~ s/(\.wmv)$/\.xml/;
print $mystring;
print "\n", $1

The out put is

Code
[root@localhost Documents]# perl infernal.pl 
abc_def_ghi.xml
.wmv[root@localhost Documents]#

-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


spider
User


Aug 12, 2009, 5:48 AM

Post #10 of 18 (1333 views)
Re: [vikas.deep] How to replace filename? [In reply to] Can't Post

Good point vikas.deep, but why did you keep the $1?
In the original subroutine he don't use it.
By the way it should not be nessecary to escape the dot in the replaced string.

So it could just as well be:

Code
sub textParse  
{
my($text) = @_;
$text =~ s/\.wmv$/.xml/;
print $text;
}



KevinR
Veteran


Aug 12, 2009, 8:36 AM

Post #11 of 18 (1330 views)
Re: [vikas.deep] How to replace filename? [In reply to] Can't Post


In Reply To
Dear you


Code
$temp =~ s/(\.wmv)$/(\.xml)/;

Why are you using brackets and making things difficult here
Can't you just use

Code
$temp =~ s/(\.wmv)$/\.xml/;

Now as you were doing it

Code
$mystring = 'abc_def_ghi.wmv';  
$mystring =~ s/(\.wmv)$/\.xml/;
print $mystring;
print "\n", $1

The out put is

Code
[root@localhost Documents]# perl infernal.pl 
abc_def_ghi.xml
.wmv[root@localhost Documents]#



The parentheses in the right side (replacement side) of a substitution regexp are literal parentheses so if it worked it would literally change .wmv to (.xml)


Code
$temp = 'test.wmv'; 
$temp =~ s/(\.wmv)$/(\.xml)/;
print $temp; # prints -> test(.xml)

-------------------------------------------------


vikas.deep
User

Aug 12, 2009, 9:09 PM

Post #12 of 18 (1323 views)
Re: [spider] How to replace filename? [In reply to] Can't Post

I used $1 to explain things (in a better manner) so that the person could really understand the significance of parenthesis/brackets that he is using. I thought that this person needs to be explained what the first pair of brackets was capturing so I printed it out using $1.

Anyway now that Kevin has (kindly) posted a response over here, (which I have not yet read), hopefully the matter would/should (English is not my mother tongue) have been settled.
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


ichi
User

Aug 12, 2009, 10:22 PM

Post #13 of 18 (1320 views)
Re: [geek.shrek] How to replace filename? [In reply to] Can't Post


In Reply To
Hi,

I would like to replace for example "abc_def_ghi.wmv" into "abc_def_ghi.xml"

how can I do this?

Thanks,



Code
while(<*.wmv>){ 
s/\.???$/.xml/;
print $_ ;
}

use rename() or File::Copy's move() to rename your file


spider
User


Aug 12, 2009, 11:06 PM

Post #14 of 18 (1318 views)
Re: [vikas.deep] How to replace filename? [In reply to] Can't Post

I think you are correct. It seems like this code is not something he has come up with on his own, and are still way of his target of changing the filename.
By the way english is not mother language either.....


vikas.deep
User

Aug 13, 2009, 1:23 AM

Post #15 of 18 (1314 views)
Re: [ichi] How to replace filename? [In reply to] Can't Post

Sir (Ichi)
I am not an experienced perl programmer so I was not surprised by the fact that (at first glance) I could not understand your code.
In order to understand your writing I copy-pasted it and was planning to print the $_ so that I can understand what is happening but Not only me my computer is also not able to understand and I am getting the following error

Code
[root@localhost Documents]# perl infernal.pl 
Nested quantifiers in regex; marked by <-- HERE in m/\.??? <-- HERE $/ at infernal.pl line 8.

Please could you explain

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


ichi
User

Aug 13, 2009, 1:57 AM

Post #16 of 18 (1310 views)
Re: [vikas.deep] How to replace filename? [In reply to] Can't Post


In Reply To
Sir (Ichi)
I am not an experienced perl programmer so I was not surprised by the fact that (at first glance) I could not understand your code.
In order to understand your writing I copy-pasted it and was planning to print the $_ so that I can understand what is happening but Not only me my computer is also not able to understand and I am getting the following error

Code
[root@localhost Documents]# perl infernal.pl 
Nested quantifiers in regex; marked by <-- HERE in m/\.??? <-- HERE $/ at infernal.pl line 8.

Please could you explain

Quote


the ??? should be tripe dots. been using ? too much on the shell.

Code
while(<*.wmv>){ 
$o = $_;
s/\....$/.xml/;
print "rename $o ,$_\n";
}



(This post was edited by ichi on Aug 13, 2009, 1:58 AM)


vikas.deep
User

Aug 13, 2009, 2:12 AM

Post #17 of 18 (1308 views)
Re: [ichi] How to replace filename? [In reply to] Can't Post


Quote
while(<*.wmv>){
$o = $_;
s/\....$/.xml/;
print "rename $o ,$_\n";
}


Sir,
The problem continues now I am not getting anything the output is as follows

Code
[root@localhost Documents]# perl infernal.pl xyz.wmv 
[root@localhost Documents]# perl infernal.pl xyz.wmv
[root@localhost Documents]# perl infernal.pl xyz.wmv
[root@localhost Documents]# perl infernal.pl xyz.wmv

As you can see I tried 4 times and every time there is no output

Am I executing it the wrong way

I thought the output will be

Code
 rename xyz.wmv, xyz.xml


Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

(This post was edited by vikas.deep on Aug 13, 2009, 2:16 AM)


ichi
User

Aug 13, 2009, 2:15 AM

Post #18 of 18 (1306 views)
Re: [vikas.deep] How to replace filename? [In reply to] Can't Post


In Reply To
Am I executing it the wrong way

Quote

yes you are. as you can see from my code, the while loop is supposed to loop over all wmv files in current directory. So the script is supposed to run with no arguments.
If you are going to pass a filename into the perl script from command line, then the while loop have to change.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives