copying files

Y

yaduraj

I have a simple question,pardon me..

if I have to copy a file, I can use something like this:

copy ("$DIR/test.c" ,"$INP_DIR/");

this copies test.c from one location to another..but
if i have to copy all the files with .c extension like, what is the
method to this...
can i not use *.c in some way..
please help me on this..

thanks in advance
 
S

Steffen G.

yaduraj said:
[...]
if i have to copy all the files with .c extension like, what is the
method to this...
can i not use *.c in some way..
please help me on this..

thanks in advance

Try this:

~~~

my $source='source'; # insert the source path (without trailing /)
my $target='target'; # insert the target path
my $select='^.*\.c$'; # this is the RegExpr equivalent to "*.c"

opendir(DIR,$source);
my @strings=readdir(DIR);
closedir(DIR);

map { system("cp $source/$_ $target") } grep(/$select/,@strings);

~~~

I'm afraid it's not the easiest solution, but it works fine ;)

HTH
Steffen
 
A

A. Sinan Unur

yaduraj said:
[...]
if i have to copy all the files with .c extension like, what is the
method to this...
....

my $source='source'; # insert the source path (without trailing /)
my $target='target'; # insert the target path
my $select='^.*\.c$'; # this is the RegExpr equivalent to "*.c"

opendir(DIR,$source);
my @strings=readdir(DIR);
closedir(DIR);

map { system("cp $source/$_ $target") } grep(/$select/,@strings);

There isn't much of a point to going through all of that if all you are
going to do is call system and not even check it's return value.

cp is not generally available on Win32 systems.

If you are going to go that route, why not just use:

system('cp', "$source/*.c", $target) == 0
or die "System failed: $?;


If there are a lot of entries in the directory, building the @strings array
can be expensive.

Here is one way to do it in a more portable way (other alternatives include
using File::Find, File::Finder or File::Find:Rule).

#! perl

use strict;
use warnings;

use File::Copy;
use File::Spec::Functions qw/catfile/;

my $source_dir = catfile 'D:', 'Home', 'Dload';
my $target_dir = catfile $source_dir, 'Jpeg';

opendir my $dir, $source_dir
or die "Cannot open directory $source_dir: $!";

while(defined (my $f = readdir $dir)) {
next unless $f =~ /.+\.jpg$/i;
my $target = catfile $target_dir, $f;
if(-e $target) {
# maybe ask the user if it is OK to overwrite?
}
my $source = catfile $source_dir, $f;
next unless -f $source;
if(copy $source, $target) {
print "$source copied to $target\n";
} else {
print STDERR "Failed to copy $source to $target\n";
}
}

closedir $dir or die "Cannot close directory: $!";
__END__

Sinan.
 
A

A. Sinan Unur

....

If you are going to go that route, why not just use:

system('cp', "$source/*.c", $target) == 0
or die "System failed: $?;

Ooops, sorry for the typo above.

Sinan.
 
P

Peter J. Acklam

I have a simple question,pardon me..

if I have to copy a file, I can use something like this:

copy ("$DIR/test.c" ,"$INP_DIR/");

this copies test.c from one location to another..but if i have
to copy all the files with .c extension like, what is the method
to this... can i not use *.c in some way..

Some thing like this (untested). You get the idea. You can also
get the files with opendir(), readdir(), grep(), and closedir().

for my $file (glob "$DIR/*.c") {
copy ($file ,"$INP_DIR/")
or die "$0: $file: copy failed: $!";
}

Peter
 
W

wana

I have a simple question,pardon me..

if I have to copy a file, I can use something like this:

copy ("$DIR/test.c" ,"$INP_DIR/");

this copies test.c from one location to another..but
if i have to copy all the files with .c extension like, what is the
method to this...
can i not use *.c in some way..
please help me on this..

thanks in advance

maybe this would work:

system("mv $DIR/*.c $INP_DIR/");

I'm sure there is something in the File module that is better though.
By the way, 'Perl Cookbook' is awsome. I just bought it and it is an
incredible reference and would have saved me a lot of trouble if I had
bought it earlier.

wana
 
B

Ben Morrow

Quoth "A. Sinan Unur said:
system('cp', "$source/*.c", $target) == 0
or die "System failed: $?;

Err... this may work on Win32, but any system with C exec conventions
will require a shell to expand the wildcard.

system("cp $source/*.c $target") and die "cp failed: $?";

and pray $source doesn't need quoting...

Ben
 
A

A. Sinan Unur

(e-mail address removed) (wana) wrote in
(e-mail address removed) (yaduraj) wrote in message

....

maybe this would work:

system("mv $DIR/*.c $INP_DIR/");

Ahem, the OP said copy, note move.

Sinan.
 
A

A. Sinan Unur

Err... this may work on Win32, but any system with C exec conventions
will require a shell to expand the wildcard.

You are right. I only tried it on Windows 98 with copy instead of cp. Since
copy is a built-in with command.com, it did not have a problem expanding
the wildcard. However, I just noticed another problem with it: Since I had
forgotten to create the target directory, it appended all the files (in
text mode) to create one file with the directory name I had in $target.

I think this justifies my preference for not shelling out for wholesale
file operations, but using File::Copy.

Sinan.
 
G

greymaus

maybe this would work:

system("mv $DIR/*.c $INP_DIR/");

I'm sure there is something in the File module that is better though.
By the way, 'Perl Cookbook' is awsome. I just bought it and it is an
incredible reference and would have saved me a lot of trouble if I had
bought it earlier.

wana

Agreed. Best Perl Book.
 
B

Ben Morrow

Quoth "A. Sinan Unur said:
You are right. I only tried it on Windows 98 with copy instead of cp. Since
copy is a built-in with command.com, it did not have a problem expanding
the wildcard. However, I just noticed another problem with it: Since I had
forgotten to create the target directory, it appended all the files (in
text mode) to create one file with the directory name I had in $target.

I think this justifies my preference for not shelling out for wholesale
file operations, but using File::Copy.

s/for.*operations/ever/
s/(?=using)/ or writing/
s/File::Copy/an appropriate module/

:)

Ben
 
A

A. Sinan Unur

A. Sinan Unur ([email protected]) wrote on MMMMLXXII September
MCMXCIII in <URL:news:[email protected]>:

Is the date supposed to be wrong above? :)
!! I think this justifies my preference for not shelling out for
!! wholesale file operations, but using File::Copy.
I, OTOH have issues with File::Copy, and never use it, and always
"shell" out. (Well, I always use "system cp", but that doesn't always
involve a shell). Issues I have with File::Copy:

[ issues snipped for brevity ]

Thanks, Abigail, for pointing those out. I must have not realized those
limitations for the two simple reasons that I don't do much copying of
files in the kind of stuff I write, and I spend most of my time on Win32.
File::Copy offers me nothing over 'system cp' (No, I don't care for
portability to non-POSIX systems - if you can't be POSIX, screw you).

Thanks for the advice.

Sinan.
 
B

Ben Morrow

Quoth (e-mail address removed):
Ben Morrow ([email protected]) wrote on MMMMLXXII September MCMXCIII in
<URL:%%
%% Quoth "A. Sinan Unur" <[email protected]>:
%% >
%% > I think this justifies my preference for not shelling out for wholesale
%% > file operations, but using File::Copy.
%%
%% s/for.*operations/ever/
%% s/(?=using)/ or writing/
%% s/File::Copy/an appropriate module/


Giving us:

I think this justifies my preference for ever, but or writingusing
an appropriate module

GAH!!! :)
Perhaps you want to say that you prefer using an appropriate module
instead of shelling out. I fully agree with that. That's why I
shell out to copy a file - because File::Copy isn't appropriate. ;-)

OK... I would certainly accept your arguments for using cp given
xthread. I would still be inclined to write a *useful* File::Copy
module, though; if only because a fair bit of my code needs to run on
random Win32 machines without the benefit of cp (yes, I do know about
(and use, when I can) the ports).

Ben
 

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

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top