http://qs321.pair.com?node_id=762972


in reply to Moving files and directories

Inside of double quotes, backslashes need to be escaped. Try my $inDir = "M:\\AllFiles" (and correcting other backslashes as well).

Note that if you had use warnings; at the top of the script, you'd see errors like "Unrecognized escape \A passed through scriptname.pl at line 8" which would be a flag indicating a potential problem.

Replies are listed 'Best First'.
Re^2: Moving files and directories
by perlNinny (Beadle) on May 09, 2009 at 00:12 UTC
    Thanks, but there is still problems:
    I get this output:
    indir= M:\AllFiles, outdir=n:\allFiles
    indir size=0 outdir size=0
    indir= M:\AllFiles, outdir=n:\allFiles
    indir size=0 outdir size=0
    indir= M:\AllFiles\*, outdir=n:\allFiles
    indir size=0 outdir size=0

    This tells me that the -s function isn't working
    which means it still isn't reading the directory information
    Eventually, I want to check to see if there is enought
    room, then execute a move function.
    use strict; use warnings; use File::Copy; # yes, it is Windows XP and a drive letters assigned to a USB and a NA +S drive. # Indir and outdir will be a parameter in this format. my $inDir = "M:\\AllFiles"; my $outDir = "n:\\allFiles"; # test 1 straight in out in DOS format. MoveFiles($inDir,$outDir); # test 2 Try within quotes. MoveFiles("$inDir","$outDir"); # test 3 Try selecting ALL files in the directory MoveFiles($inDir."\\*", $outDir); sub MoveFiles { my ( $source, $destination ) = @_; my $source_size = (-s $source) || 0; my $destination_size = (-s $destination) || 0; move( $source, $destination ) if ($destination_size > $source_size); }

      On win32 systems, stat often returns 0 for size when called for a directory. Some documentation suggests that it may sometimes return non-zero for directory size in some circumstances, but I haven't seen it myself. The '-s' file test is based on stat, so '-s' will often return 0 when used on a directory.

      It might help you solve your problem if you check the return from the move function. See File::Copy for details. Maybe something like the following:

      move( $source, $destination ) or die "move( $source, $destination ) fa +iled: $!";

      The error message should help you understand why the move failed. On the other hand, testing I have done on Windows XP suggests that the error messages are not very good: in my tests it returned "No such file or directory" regardless of what the actual problem was. Also, while move worked for directories within a file system, copy didn't. Neither move nor copy worked for directories when moving or copying from one drive to another but move and copy both worked when moving or copying a file from one drive to another. So it seems that File::Copy is somewhat broken, at least for Active State perl 5.10.0 on Windows XP.

        $!(errno) is equally as good on all platforms. On win32 you also get $^E.

        I am having similar trouble with Windows. I resorted to using the back tick operator and window's move command to move directories...

        $s = "C:/temp/$here"; $d = "C:/$there/"; `move $s $d`; -e "$d/$here" && !-e $s ? print "Move successfully\n" : print "Move er +ror $d/$here \n".$!;