For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > renaming a group of files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author renaming a group of files
firewall1expert@gmail.com

2006-10-18, 6:57 pm

How can I rename the extention of a group of files?
eg:
text1.doc
text3.doc
text5.doc

These need to be renamed as :
text1.txt
text3.txt
text5.txt

Thanks
-Tom

kens

2006-10-18, 6:57 pm


firewall1expert@gmail.com wrote:
> How can I rename the extention of a group of files?
> eg:
> text1.doc
> text3.doc
> text5.doc
>
> These need to be renamed as :
> text1.txt
> text3.txt
> text5.txt
>
> Thanks
> -Tom


Look at the File::Basename module.

"perldoc File::Basename" for docs.

and the rename function.

"perldoc -f rename"

HTH,
Ken

nikne

2006-10-19, 3:59 am

Hi There...

Please Do it in this way
and you will get the output.

@array = ("text1.doc","text3.doc","text5.doc");

foreach $array(@array){
if($array =~ /.doc/i){
$array =~ s/.doc/.txt/g;
print $array;
}
}

Thanks and Regards,
NN

kens wrote:
> firewall1expert@gmail.com wrote:
>
> Look at the File::Basename module.
>
> "perldoc File::Basename" for docs.
>
> and the rename function.
>
> "perldoc -f rename"
>
> HTH,
> Ken


kens

2006-10-19, 7:56 am


nikne wrote:
> Hi There...
>
> Please Do it in this way
> and you will get the output.
>
> @array = ("text1.doc","text3.doc","text5.doc");
>
> foreach $array(@array){
> if($array =~ /.doc/i){
> $array =~ s/.doc/.txt/g;
> print $array;
> }
> }


Yes, there is more than one way to do it.
But try this method with a file named "adoc.doc" and see what happens.
You forgot to use a backslash in front the period.
Also you should "use strict" and "use warnings".
For clarification, the step of renaming the files is
not mentioned in this example.
Ken
[color=darkred]
>
> Thanks and Regards,
> NN
>
> kens wrote:

Uri Guttman

2006-10-19, 6:56 pm

>>>>> "k" == kens <kenslaterpa@hotmail.com> writes:

stop top posting.

k> nikne wrote:[color=darkred]

k> Yes, there is more than one way to do it.
k> But try this method with a file named "adoc.doc" and see what happens.
k> You forgot to use a backslash in front the period.
k> Also you should "use strict" and "use warnings".
k> For clarification, the step of renaming the files is
k> not mentioned in this example.

and he also needs to anchor the regex or foo.doc.doc will not work
either.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
firewall1expert@gmail.com

2006-10-19, 6:56 pm

Thank you for all the replies:

#!/usr/bin/perl
#Code to rename a group of files


use strict;
use warnings;
@array = ("adoc.doc");

foreach $array(@array){
if($array =~ /.doc/i){
$array =~ s/.doc/.txt/g;
print $array;
}

}


I am getting compilation error :

/rename1
Global symbol "@array" requires explicit package name at ./rename1 line
7.
Global symbol "$array" requires explicit package name at ./rename1 line
9.
Global symbol "@array" requires explicit package name at ./rename1 line
9.
Global symbol "$array" requires explicit package name at ./rename1 line
10.
Global symbol "$array" requires explicit package name at ./rename1 line
11.
Global symbol "$array" requires explicit package name at ./rename1 line
12.

Did I miss a backslash or something?

- Tom

Uri Guttman wrote:
>
> stop top posting.
>
> k> nikne wrote:
>
> k> Yes, there is more than one way to do it.
> k> But try this method with a file named "adoc.doc" and see what happens.
> k> You forgot to use a backslash in front the period.
> k> Also you should "use strict" and "use warnings".
> k> For clarification, the step of renaming the files is
> k> not mentioned in this example.
>
> and he also needs to anchor the regex or foo.doc.doc will not work
> either.
>
> uri
>
> --
> Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
> --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
> Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org


kens

2006-10-19, 6:56 pm


firewall1expert@gmail.com wrote:
> Thank you for all the replies:
>
> #!/usr/bin/perl
> #Code to rename a group of files
>
>
> use strict;
> use warnings;
> @array = ("adoc.doc");
>
> foreach $array(@array){
> if($array =~ /.doc/i){
> $array =~ s/.doc/.txt/g;
> print $array;
> }
>
> }
>
>
> I am getting compilation error :
>
> /rename1
> Global symbol "@array" requires explicit package name at ./rename1 line
> 7.
> Global symbol "$array" requires explicit package name at ./rename1 line
> 9.
> Global symbol "@array" requires explicit package name at ./rename1 line
> 9.
> Global symbol "$array" requires explicit package name at ./rename1 line
> 10.
> Global symbol "$array" requires explicit package name at ./rename1 line
> 11.
> Global symbol "$array" requires explicit package name at ./rename1 line
> 12.
>
> Did I miss a backslash or something?
>
> - Tom
>
> Uri Guttman wrote:

First, the errors are caused by using the "use strict" directove and
then not declaring your variables. "use strict" requires that all
variables be declared before use, in this case:

my @array = ("adoc.doc");

foreach my $array(@array){

would declare the variables. These prevents such stupid mistakes as
typing @arry instead of @array. Without "use strict", @arry would
indicate you want a new array by the name of @arry and the Perl
interpreter would be happy to use that new variable much to the coder's
dismay.

I had suggested using the "File::Basename" module, as I thought it
might be easier to understand than regular expressions. But, since we
seem to be going the regular expression route, here goes.
[color=darkred]
> if($array =~ /.doc/i){
> $array =~ s/.doc/.txt/g;


The "if" statement appears redundant to me. The substitution will
simply fail if the regular expression is not matched, so don't bother
checking twice.

As pointed out by Uri and myself, the line

$array =~ s/.doc/.txt/g;

is problematic. If you are planning to use regular expressions, I would
suggest reading some of the documentation (via the following commands):

perldoc perlrequick
perldoc perlretut
perldoc perlre
perldoc perlreref

Change this line to:

$array =~ s/\.doc$/.txt/;

This will force it to look for 'doc' following a period and being the
last characters in the file name. Try the old substitution on files
named "adoc.doc" and
"file.document" and see what happens.

Not again, that this code does nothing regarding the renaming of files,
if you want to actually rename the files look up the 'rename' function.

perldoc -f rename

Ken

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com