CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
copying files

 



adp
newbie

May 7, 2001, 8:35 AM

Post #1 of 8 (1485 views)
copying files Can't Post

I am copying files from one directory to another, the problem : some file sizes are incorrect ?

here's my code:

opendir ( DIR_tools,"c:/apples/tools") or die "opendir dir_apples/tools failed ; $!";

while ($name = readdir(DIR_tools))
{
copy ("c:/apples/tools/$name", "h:/apples/${this_Name}.${item_apples}/tools/$name");
}

closedir(DIR_tools);

Should i be specifying a specific option?



rjoseph
Novice

May 7, 2001, 1:55 PM

Post #2 of 8 (1480 views)
Re: copying files [In reply to] Can't Post

Ok, woah, here we go.


In Reply To
opendir ( DIR_tools,"c:/apples/tools") or die "opendir dir_apples/tools failed ; $!";

Why not check out the File::Find module from CPAN - it usually is a much better idea than surfing through a directory yourself.


In Reply To
copy ("c:/apples/tools/$name", "h:/apples/${this_Name}.${item_apples}/tools/$name");

I have never heard of a copy function in Perl (and there is no mention in any of the docs, so unless this is a sub you created (in which case you should have defintely posted that code here because we can't help with copying files if you don't show the file copying subs) then you might want to check into using something else.

As for copying files, I find it easiest (and safest) to do this (supposing I am copying a.txt into b.txt):

Code
my $buffer; 
open (A,"a.txt") or die "a.txt: $!\n\n";
open (B,"+>b.txt") or die "b.txt: $!\n\n";
binmode(A);
binmode(B);


while (read(A,$buffer,1024)) { print B $buffer; }
close(A); close(B);

Noticed the bolded binmode functions - those are important on certain OSes (I am assuming you are on Windows because of your directory naming scheme) that distinguish between text and binary files so that you dont accidently transfer a binary file in ASCII mode (bad bad bad).

Hope this helps!

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


adp
newbie

May 8, 2001, 9:56 AM

Post #3 of 8 (1474 views)
Re: copying files [In reply to] Can't Post

silly question: how do I know if my version of perl supports or has the CPAN module?



rjoseph
Novice

May 8, 2001, 12:51 PM

Post #4 of 8 (1470 views)
Re: copying files [In reply to] Can't Post

If you are refering to File::Find, as long as you are using a fairly recent version (defintely any 5 version), it will have it. :)

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


rjoseph
Novice

May 8, 2001, 4:24 PM

Post #5 of 8 (1464 views)
Re: copying files [In reply to] Can't Post

However, to find all of the modules on your system, you could look through every path in the @INC array, but the amazing japhy has instead written a script that does all the work for you:

Japhy's Module Lister

Just run that, and it prints all your modules out. You can also redirect the output of the program to save the results in a text file with the '>' command, so:

Code
$ ./module_lister.pl > module_list.txt

in linux/UNIX would redirect the output to 'module_list.txt' - as far as I know, this works in Windoze, too.

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


adp
newbie

May 9, 2001, 3:22 PM

Post #6 of 8 (1456 views)
Binmode [In reply to] Can't Post

need some further help with Binmode.

opendir ( DIR_apples, "f:/_/apples.$item_apples" ) or die "opendir dir_apples failed ; $!";


binmode ("f:/apples/apples.img");
copy("f:/apples/apples.img", "f:/apples/test.img")
or die "copy failed ; $!";

I'm not getting the correct copy, am i not specifying correctly? ...sorry I'm a REAL beginner .....



rjoseph
Novice

May 9, 2001, 4:42 PM

Post #7 of 8 (1447 views)
Re: Binmode [In reply to] Can't Post

No, you need to binmode() the filehandle, not the file name:

Code
open (MY_FILE,"f:/apples/apples.img") or die "$!"; 
binmode(MY_FILE);

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


adp
newbie

May 15, 2001, 10:33 AM

Post #8 of 8 (1420 views)
Re: Binmode [In reply to] Can't Post

opendir ( DIR_apples, "f:/_/apples.$item_apples" ) or die "opendir dir_apples failed ; $!";
binmode (DIR_apples);

copy("f:/apples/apples.img", "f:/apples/test.img")
or die "copy failed ; $!";



.. I can't get it to work? I'm running on Unix, could this be why?

is there an alternative way of creating a copy of a file with another name?


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives