For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > copy and change extension









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 copy and change extension
Brian Milbrandt

2005-04-20, 8:56 pm

I am trying to get a script to copy and change the extension for files with
a non-zero file size. I have tried everything I can think of but I can't
seem to get the copy files part right. All I get is the output 0 files
copied. Here is the code if anyone can help. The print function in the
loop is working and displays all the file names.

Thanks

#!/bin/perl
# Check to be sure exactly 2 arguments passed to script
die "Must pass exactly 2 arguments to script" if @ARGV != 2;

# assign meaningfull names to variables
$source = @ARGV[0];
$target = @ARGV[1];

# Check to see if First argument is a directory
# if not exit
die "$source is not a directory \n" if !-d $source;


mkdir $target, 0755;

#copy all files with a .abc extension to the target directory
# renaming the extension to xyz


$filecount = 0;
#foreach my $file (glob "*.abc")
#{

opendir SOMEDIR, $source;
while ($file = readdir SOMEDIR)
{ printf " the file name is %s\n", $file;
if (-s > 0)
{
printf " non zero file \n";
foreach my $file (glob "*.abc")
{ my $newfile = $file;
$newfile =~ s/\.abc$/.new/;
$newfile = $target."/".$newfile;
rename $file, $newfile;
$filecount += 1;
}
}
}


# Print the number of files copied
printf "The number of files copied is: %d\n", $filecount;

Jay Savage

2005-04-21, 3:56 pm

On 4/20/05, Brian Milbrandt <tigger@btm.net> wrote:
> I am trying to get a script to copy and change the extension for files wi=

th
> a non-zero file size. I have tried everything I can think of but I can't
> seem to get the copy files part right. All I get is the output 0 files
> copied. Here is the code if anyone can help. The print function in the
> loop is working and displays all the file names.
>=20
> Thanks
>=20
> #!/bin/perl
> # Check to be sure exactly 2 arguments passed to script
> die "Must pass exactly 2 arguments to script" if @ARGV !=3D 2;
>=20
> # assign meaningfull names to variables
> $source =3D @ARGV[0];
> $target =3D @ARGV[1];
>=20
> # Check to see if First argument is a directory
> # if not exit
> die "$source is not a directory \n" if !-d $source;
>=20
> mkdir $target, 0755;
>=20
> #copy all files with a .abc extension to the target directory
> # renaming the extension to xyz
>=20
> $filecount =3D 0;
> #foreach my $file (glob "*.abc")
> #{
>=20
> opendir SOMEDIR, $source;
> while ($file =3D readdir SOMEDIR)
> { printf " the file name is %s\n", $file;
> if (-s > 0)
> {
> printf " non zero file \n";
> foreach my $file (glob "*.abc")
> { my $newfile =3D $file;
> $newfile =3D~ s/\.abc$/.new/;
> $newfile =3D $target."/".$newfile;
> rename $file, $newfile;
> $filecount +=3D 1;
> }
> }
> }
>=20
> # Print the number of files copied
> printf "The number of files copied is: %d\n", $filecount;
>=20


Don't reinvent the wheel here; check out File::Rename from cpan. As
for what's going on here, if you'd bothered to turn on warnings and
strict, you'd be getting errors about use of uninitialized values at
this line:

if (-s > 0)

Since you don't specify anything to test against, perl tries to test
against $_. But since you've defined $file as the loop variable for
while, $_ isn't defined. Also, -s > 0 is redundant: -s means "file
exists and has a nonzero size."

if ( -s $file)=20

Should be a step in the right direction. But really, somebody else
has done all this work for you already: use File::Rename.

HTH,

--jay
Sponsored Links







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

Copyright 2009 codecomments.com