the_muffin_man has asked for the wisdom of the Perl Monks concerning the following question:

hello monks!

i have a little script that's supposed to copy some files from one windows box to another windows box on our network. i'd like to make it so that if the files already exist in the new location, they get over-written with new copies.

File::Copy seems to be copying the files such that the permissions are set to read-only. the manpage doesn't say much about permissions, unless i've blatantly misread it. the permissions on the original file are not set to read-only. why would File::Copy be changing them?

thanks!

Replies are listed 'Best First'.
Re: clobbering files with File::Copy
by sweetblood (Prior) on Jul 02, 2004 at 15:11 UTC
    You can use stat to get the original files mode and the after you overwrite the file you can do chmod to set the "new file's" mode to what the originals was.

    Note: also check out sprintf and oct

    Update:
    You might also check out umask. This will change the mode for all files your process creates. You can set it to what ever mode you'd like but, it won't maintain the original files mode.

    HTH

    Sweetblood

Re: clobbering files with File::Copy
by BrowserUk (Patriarch) on Jul 02, 2004 at 16:00 UTC

    Take a look at the CopyFile( $src, $dst, $fileIfExists ) api in Win32API::File. The third parameter decides whether it will overwrite or fail if the file exists.

    That probably won't help if the existing file is set readonly, but if you use the native api your likely to get more rational behaviour with respect to file permissions. Ie. If the source is not readonly, the destination won't.

    You can use Win32::File to obtain and change the files attributes.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re: clobbering files with File::Copy
by blueAdept (Beadle) on Jul 02, 2004 at 17:47 UTC
    I don't know about efficiency or scalability(how large of files it will handle) but I've used this for some time within a backup script I have that copies files onto a network drive. Perl supports UNC paths, so I don't see any real barriers to your using it for copying between machines. My backup script wasn't written for or tested to the level of doing anything serious - but it has served me well thus far.(and it was a fun excercise of 'wonder if I can do this')
    open(SOURCE, "<$source") open(DEST, ">$file") binmode SOURCE; binmode DEST; print DEST <SOURCE>;