Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

move files revisited

by grashoper (Monk)
on Apr 29, 2009 at 17:33 UTC ( [id://760926]=perlquestion: print w/replies, xml ) Need Help??

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

I finally managed to move my files. posting the code. this is dependent on being in the correct path of the sourcetree when it is ran, it will recurse directories grabbing text files and move them to the directory specified in $dir2. however something odd happens if there are no txt files, I get this as output. Output when there are no files within subdirectories in move.pl actually it doesn't seem to matter whether ther are files or not, what have I done, it worked yesterday today its toast and my deadline is tomorrow :( ./Documents and Settings/aanderso/Local Settings/Application Data/Microsoft/Inte rnet Explorer brndlog.txt this is c:\mlx\ this would be c:\mlx\./Documents and Settings/aanderso/Local Settings/Applicatio n Data/Microsoft/Internet Explorer this should be c:\mlx\/./Documents and Settings/aanderso/Local Settings/Applicat ion Data/Microsoft/Internet Explorer/brndlog.txt do you see the problem yet? Unable to move No such file or directory at c:\inetpub\performancetesting\output \new\1.pl line 66.
use DirHandle; use File::Find; use File::Copy; use Data::Dumper; use strict; use warnings; my $dir= "."; my $dir2="c:\\mlx\\"; lista_dirs($dir,$dir); #lista_dirs2($dir2,$dir2); sub lista_dirs{ my ($dir, $dirname) = @_; my (@dirs, @files); opendir DIR, "$dir"; my @dircontent = grep { /[^\.]/ } readdir(DIR); closedir DIR; # opendir DIR2, "$dir2"; #my @dircontent2 = grep { /[^\.]/ } readdir(DIR); #closedir DIR2; # @dirs2=(); #@files2=(); @dirs = (); @files = (); foreach(@dircontent) { if(-d "$dir/".$_) { push @dirs, $_; } else { push @files, $_ if($_ =~ /\.txt$/); } } #foreach(@dircontent2){ #if(-d "$dir2/".$_) { #push @dirs2, $_; #} else { # push @files2, $_ if($_ =~ /\.txt$/); #} #} foreach my $d(@dirs) { lista_dirs("$dir/" .$d, $d); } foreach my $file (@files) { $/ = ''; print $dir, "\n"; print $file, "\n"; print "this is $dir2 \n"; print "this would be $dir2$dir \n"; print "this should be $dir2/$dir/$file \n"; print "do you see the problem yet? \n"; move ("$dir/$file","$dir2/$dir/$file") or die "Unable to move $!"; } return; }

Replies are listed 'Best First'.
Re: move files revisited
by ramrod (Curate) on Apr 29, 2009 at 17:57 UTC
Re[2]: move files revisited
by tokpela (Chaplain) on May 01, 2009 at 08:36 UTC

    This seems like a job for the dirmove command in the File::Copy::Recursive module.

    # (untested) use strict; use warnings; use File::Copy::Recursive qw(dirmove); my $dir= "c:/startdir"; my $dir2="c:/mlx"; if (dirmove($dir1,$dir2)) { print "MOVED DIRECTORY [$dir1] TO [$dir2]\n"; } else { print "[Error] UNABLE TO MOVE DIRECTORY [$dir1] TO [$dir2] - [$!]\n" +; }
    BTW: I would always use forward slashes in filepaths on Windows. It works the same and you never have to worry about the escaping issues of backslashes. Not to mention that your code is more portable too :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-28 10:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found