Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
P is for Practical
 
PerlMonks  

moving files

by Anonymous Monk
on Oct 16, 2012 at 06:59 UTC ( #999231=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

system ("mv $fil_nm ./reports"); $filnm is present inside foreach loop at the end i'm using system command finally i want to mmove all the $fil_nm to a directory reports.but instead only the last executed $fil_nm is moving to a directory reports can u help me how to move all the contents in the $fil_nm to reports directory

Comment on moving files
Re: moving files
by tobyink (Abbot) on Oct 16, 2012 at 07:19 UTC

    Is your system command inside the loop? (That is, before the closing }.) It should be.

    Also you seem to inconsistently refer to $fil_nm and $filnm; these are different variable names, so if you mix them up randomly in your code, that will cause odd results.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      by mistake i've typed $fil_nm in two ways my variable is $fil_nm only
      no my system command is outside the loop and i want to move the $fil_nm files to reports directory only after completing the foreach loop outside that system command is present . i'm facing this problem help me over this
Re: moving files
by Athanasius (Prior) on Oct 16, 2012 at 07:33 UTC

    It’s hard to say what’s going wrong when you don’t show either the loop code or the contents of $fil_nm.

    However, it’s not necessary to use system — the core module File::Copy has a move function. Here’s a guess at what you’re trying to do:

    use File::Copy; my @files_to_move = split(/\s+/, $fil_nm); # assuming $fil_nm contain +s a space-separated list of files to move move($_, './reports') for @files_to_move;

    Hope that helps,

    Athanasius <°(((><contra mundum

Re: moving files
by 2teez (Priest) on Oct 16, 2012 at 08:17 UTC

    You could use File::Copy with File::Find instead of

    system ("mv $fil_nm ./reports")
    like so:
    use warnings; use strict; use File::Copy qw(move); use File::Spec; use Cwd qw(abs_path); use File::Find; my $path = $ARGV[0]; my $final_dir = File::Spec->rel2abs("./report"); $path = abs_path($path); find( sub { chomp; return if $_ eq '.' or $_ eq '..'; move( $_, $final_dir ); }, $path );

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (22)
As of 2014-04-17 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (453 votes), past polls