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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on how to a specific group of files from source directory to destination directory
  • Download Code

Replies are listed 'Best First'.
Re: copying each and every file from source directory to destination directory
by shmem (Chancellor) on Oct 20, 2007 at 09:10 UTC
    Welcome to The Monastery.

    Perl comes with a wealth of information: manual pages for the perl core, FAQ compilations and manual pages for every module. How many pages of those have you read so far?

    To read the manual page for File::Copy type

    perldoc File::Copy

    at the command line prompt. Or click on the above link.

    Show yourself round a bit in the Monatsery, browse the Tutorials section, the PerlMonks FAQ, play with Super Search.

    If you want to be spoonfed with information, don't post nodes, but ask in the chatterbox. Thank you.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: how to copy each and every file from a source directory to destination directory
by bart (Canon) on Oct 20, 2007 at 06:34 UTC
    Your loop body is empty. In it, you can put this code:
    copy "$source/$file", "$dest$file" or die "Can't copy file $file: $!";
    Also, I think you should check first to see if the inputted string is indeed a directory. Perhaps a check in the opendir call will work.

    Plus, you code contains some stray code. Change this

    opendir(DIR, $sech $file(@files){
    to
    opendir(DIR, $source) or die "Cannot opend the directory '$source': $!";
Re: copying each and every file from source directory to destination directory
by weismat (Friar) on Oct 20, 2007 at 06:55 UTC
    You may use File:Util as well. The code may look like this (taken from the docu).
    use File::Util; use File::Copy; my($f) = File::Util->new(); my(@files) = $f->list_dir($dir,'--files-only'); foreach $file (@files) { copy ($file, "$targetDir/$file); }
      hi thanks for the reply. i did excecute your code as given and did give source and target directory name i got an error as below.
      Can't locate File/Util.pm in @INC (@INC contains: /usr/lib/perl5/5.8.5 +/i386-linux-thread-multi /usr/lib/perl5/5.8.5 /usr/lib/perl5/site_per +l/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.4/i386-l +inux-thread-multi /usr/lib/perl5/site_perl/5.8.3/i386-linux-thread-mu +lti /usr/lib/perl5/site_perl/5.8.2/i386-linux-thread-multi /usr/lib/p +erl5/site_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/site_perl +/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5 /usr/li +b/perl5/site_perl/5.8.4 /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5 +/site_perl/5.8.2 /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_p +erl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.5/i +386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.4/i386-linux-th +read-multi /usr/lib/perl5/vendor_perl/5.8.3/i386-linux-thread-multi / +usr/lib/perl5/vendor_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl +5/vendor_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/vendor_per +l/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5 /usr +/lib/perl5/vendor_perl/5.8.4 /usr/lib/perl5/vendor_perl/5.8.3 /usr/li +b/perl5/vendor_perl/5.8.2 /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/p +erl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at ./org line 2. BEGIN failed--compilation aborted at ./org line 2.
Re: how to a specific group of files from source directory to destination directory
by ahmad (Hermit) on Oct 20, 2007 at 05:53 UTC

    i cannot see the copy function anywhere in your code

    anyway you properly needs something like

    #!/usr/bin/perl -w use strict; use File::Copy; # Desitnation directory my $DesDir = ''; # Source dir name print "Enter Source dir: "; chomp(my $Source = <STDIN>); if (!-d $Source ) { print "Dir : $Source doesn't exists\n"; exit; } opendir(DIR,$Source); while (my $F = readdir(DIR)) { next if ($F!~m/\.log$/); copy("$Source/$F","DesDir/$F"); } closedir(DIR); print "Copy Complete\n";

    HTH

Re: how to a specific group of files from source directory to destination directory
by rminner (Chaplain) on Oct 20, 2007 at 08:03 UTC

    Remark: Posted this in reply to an unformated dupe (Reason for the code tags comment)

    Hi,
    You should put your code inside <code> tags - it makes the code more readable. If not one has to look up the source of the webpage to get a readable format. You might consider reading Writeup Formatting Tips.

    Trying to make sense of your code (put inside <code> tags):
    #!/usr/bin/perl use File::Copy; $dest="/home/dpavu2/users1/perl/sra/"; print "the source direcotry name:\n"; chomp(my $source = <STDIN>); opendir(DIR, $sech $file(@files){ @files = grep { /\.log$/ } readdir (DIR); foreach $file(@files){ } closedir (DIR);
    i guess you were trying to do the following:
    #!/usr/bin/perl use strict; use warnings; use File::Copy; use File::Spec; my $dest="/home/dpavu2/users1/perl/sra/"; print "the source direcotry name: \n"; my $source = ''; chomp($source = <STDIN>); if (opendir(my $DIR, $source)){ my @files = grep { /\.log$/ } readdir ($DIR); foreach my $f (@files){ my $src = File::Spec->catfile($source , $f); my $dst = File::Spec->catfile($dest , $f); copy($src , $dst) or die "Failed to copy $src to $dest ($!)\n" +; } closedir ($DIR); } else { warn "Failed to open '$source ' for reading ($!)\n"; }
    The Module File::Copy::Recursive is also quite practical when copying files. You should also always include
    use strict; use warnings;
    directives at the beginning of your perl code. If you don't understand an error message then
    use diagnostics;
    might create error messages which make more sense to you.
    Cheers Roland
      i am very thank ful for the help extended same thing i its working how can i reverse the content of each file?? i.e if a file contains lee jones can i have it as jones lee.

      20071110 Edited by Co-Rion: Restored content

        asdfghijkl:

        You could get a hint from the documentation:

        perldoc -f reverse

        but I have this on the shelf:

        #!/usr/bin/perl -w print join(" ", reverse split /\s/), "\n" for (reverse <STDIN>);
        ...roboticus
        i am very thank ful for the help extended same thing i its working how can i reverse the content of each file?? i.e if a file contains lee jones can i have it as jones lee.

        I personally believe that since this is a wholly different question, then you should start a new thread. Notice: one new thread, not a hundred like the list time. Before you do so, please pause a little, take a deep breath and review your requirements: your specs as of now are far too poor - are your files expected to only contain strings like "lee jones"? In the IMHO unlikely case that is actually so, you may be content with

        print reverse split /(\s+)/;

        in conjunction with the $^I special variable. If you only want to replace the fixed string "lee jones" with "jones lee" everywere, then

        s/lee jones/jones lee/g;

        may be enough. But things may become trickier if spacing is not uniform, case does matter and you want to preserve it, and so on. In any case, you have to tell us.

      instead of prompting for a directory names how can i give the source and dest names from the command line itself i.e ./programname source dest

      20071110 Edited by Co-Rion: Restored content