Copy and change file extension

B

b.milbrandt

I am trying to convert a unix shell script to perl, and I have having a
problem with the portion that copies files based on extension and renaming
them in the process. I only trying to copy files with a non-zero length.
Here is what I have so far.

$target is a command line parameter
$source is a command line parameter

opendir DH, "/$target";
foreach $file (readdir DH)
{ printf " the file name is %s\n", $file;
next unless $file =~ \/.abc$/ and !-z $name;
my $newfile = /$target//$file;
$newfile =~ s/\.abc$/.xyz/;
$filecount += 1;
}
 
J

Jim Gibson

b.milbrandt said:
I am trying to convert a unix shell script to perl, and I have having a
problem with the portion that copies files based on extension and renaming
them in the process. I only trying to copy files with a non-zero length.
Here is what I have so far.

$target is a command line parameter
$source is a command line parameter

opendir DH, "/$target";
foreach $file (readdir DH)
{ printf " the file name is %s\n", $file;
next unless $file =~ \/.abc$/ and !-z $name;
my $newfile = /$target//$file;
$newfile =~ s/\.abc$/.xyz/;
$filecount += 1;
}

Is this the actual program you are trying to run? You are missing
characters and using the wrong characters in some places. You do not
say what this program is doing wrong. How can anybody help you?

Please post a complete, working, short-as-possible, cut-and-pasted
program that demonstrates the problem you are having, but do it in
comp.lang.perl.misc because this newsgroup is defunct.

Thanks.
 
B

b.milbrandt

Here is the whole program

#!/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;

# if !-d $target;
#{ # Create Target Directory
# mkdir $target, 0755;
#}

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


# chdir $source;
# rename -f

$filecount = 0;

opendir DH, "//$source";
foreach $file (readdir DH)
{ printf " the file name is %s\n", $file;
next unless $file =~ \/.abc$/ and !-z $name;
my $newfile = /$target//$file;
$newfile =~ s/\.abc$/.xyz/;
$filecount += 1;
}

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

The program runs but I get 0 files copied and no files are copied or
renamed.

Brian
 
J

Jim Gibson

[top posting fixed]

[ original program with bugs snipped]

You have ignored my advice to post your question to
comp.lang.perl.misc, I see. The only additional piece of advice I will
give you is to recommend you put the following:

use strict;
use warnings;

at the beginning of your program. You will then need to put 'my' in
front of your variable declarations. After doing that, you should be
able to find the source of your problems.
 
J

Joe Smith

b.milbrandt said:
opendir DH, "/$target";
foreach $file (readdir DH)
{ printf " the file name is %s\n", $file;

That's wrong.

foreach my $entry (readdir DH) {
my $file = "/$target/$entry";
print " the file name is $file\n";
}

You've got another error where you use @ARGV[0] instead of $ARGV[0].
A better way would be to change
$source = @ARGV[0];
$target = @ARGV[1];
to
die "Usage: ..." unless @ARGV == 2;
my $source = shift;
my $target = shift;

Any other questions should be posted to comp.lang.perl.misc and
not this newsgroup (comp.lang.perl). But don't post anything
until after you've read and understood the "Posting Guidelines for
comp.lang.perl.misc".
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top