467,074 Members | 913 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,074 developers. It's quick & easy.

Copy and change file extension

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;
}
Jul 19 '05 #1
  • viewed: 20820
Share:
4 Replies
In article <ttf9e.2474$c24.1712@attbi_s72>, b.milbrandt
<b.*********@insightbb.com> wrote:
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.
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Jul 19 '05 #2
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

"Jim Gibson" <jg*****@mail.arc.nasa.gov> wrote in message
news:190420051608396664%jg*****@mail.arc.nasa.gov. ..
In article <ttf9e.2474$c24.1712@attbi_s72>, b.milbrandt
<b.*********@insightbb.com> wrote:
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.
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet
News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000
Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---

Jul 19 '05 #3
In article <Alg9e.2409$WI3.2174@attbi_s71>, b.milbrandt
<b.*********@insightbb.com> wrote:

[top posting fixed]

"Jim Gibson" <jg*****@mail.arc.nasa.gov> wrote in message
news:190420051608396664%jg*****@mail.arc.nasa.gov. ..
In article <ttf9e.2474$c24.1712@attbi_s72>, b.milbrandt
<b.*********@insightbb.com> wrote:

[ original program with bugs snipped]
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.


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.

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 19 '05 #4
b.milbrandt wrote:
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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Els | last post: by
1 post views Thread by Nicky | last post: by
4 posts views Thread by foker500 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.