Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Moving .doc Files

by bkiahg (Pilgrim)
on Jun 23, 2004 at 19:31 UTC ( [id://369148]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I think I'm missing something simple. I'm trying to move a file before I delete it. And the way I'm trying it gives me a blank word doc in the deleted folder.
#! C:\perl\bin\perl.exe use strict; use CGI qw(fatalsToBrowser); my $file = "C:\\web\\policies\\folder\\file.doc"; my ($bytesread, $buffer); print "Content-type: text/html\n\n"; open (DOC, $file) or die "cannot open: $!"; my @doc = <DOC>; close DOC; open (NEWDOC, ">C:\\web\\policies\\folder\\deleted\\file.doc") or die +"Cannot open new: $!"; binmode (NEWDOC); while ($bytesread=read(@doc,$buffer,1024)) { print OUTFILE $buffer; } close NEWDOC; print "completed!"; foreach (@doc) {print}
Any help is very appreciated.

Replies are listed 'Best First'.
Re: Moving .doc Files
by amw1 (Friar) on Jun 23, 2004 at 19:37 UTC
    Why not just move the file?
    perldoc File::Copy
      That worked!! Thanks!!!
Re: Moving .doc Files
by kelan (Deacon) on Jun 23, 2004 at 21:08 UTC

    How about this:

    # open them my $old = "something.doc"; my $new = "somethingelse.doc"; open OLD, '<', $old or die "Can't open old: $!\n"; open NEW, '>', $new or die "Can't open new: $!\n"; binmode OLD; # only need these on windows binmode NEW; # copy print NEW <OLD>; # close them close OLD; close NEW;

      That worked also! File::Copy will suffice for this particular project, but its always good to know how to do it with less code. Thanks!
Re: Moving .doc Files
by NetWallah (Canon) on Jun 23, 2004 at 19:38 UTC
    You are trying to read and array!

    • Delete the lines starting "my @doc" and "close DOC"
    • Change "read(@doc" to "read(DOC"
    • Add "close DOC" before you close NEWDOC
    • You will probably need "binmode" on DOC as well

        Earth first! (We'll rob the other planets later)

      That didn't work, but File::Copy does what I was looking for.
Re: Moving .doc Files
by gawatkins (Monsignor) on Jun 23, 2004 at 20:42 UTC

    In addition to the suggestion for File::Copy you could use the system move command:

    opendir (DIRHANDLE, $OLDDIR); while ($oldname = readdir(DIRHANDLE)) { chomp $oldname; $filename = "$OLDDIR\\$oldname"; $newname = "$DIR\\$oldname"; if (-f $filename) { system("move", $filename, $newname); } } closedir (DIRHANDLE);

    Just a diffrent way to do it.
    Greg W

Re: Moving .doc Files
by ercparker (Hermit) on Jun 26, 2004 at 17:00 UTC
    #I would use perl function rename : #can check $! for success #1 for success rename "/path/to/oldfilename", "/path/to/the/newfilename";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://369148]
Approved by bassplayer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-19 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found