Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

copy files from one directory to another

by luckysing (Novice)
on Aug 02, 2010 at 20:55 UTC ( #852541=perlquestion: print w/ replies, xml ) Need Help??
luckysing has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks I have written a script to copy files from one directory to another.What I want to do is that I have a tar file which in turn has many tar files inside it .I am extracting the main tar file and then after iterating through all the tar files inside of the big tar file i create folder name based on the tar file for example :for a file named FILE1.tar I create a directory named FILE1.After this I want to copy the FILE1.tar to its respective directory and extract it there.The problem is I am not able to use the move command and my program only creates the directory.Forgot to mention that I get a message file exists when i compile my program.Thanks in advance

#!/usr/bin/perl -w use File::Copy; use strict; use Archive::Tar; my (@files,$i,$name,$ext,@file,$oldlocation,$tobe); my $tar = Archive::Tar->new(); $tar->read('some.tar'); $tar->extract(); @files=<*.tgz>; #print @files; for $i(0..$#files) { $file[$i]=do{local(@ARGV,$/)=$files[$i];<>}; ($name,$ext)=split(/\./,$files[$i]); mkdir("$name",0777)||print $!; $oldlocation="/home/newbie/ta/$file[$i]"; $tobe="/home/newbie/ta/$name/"; move ($oldlocation,$tobe); } exit;

Comment on copy files from one directory to another
Download Code
Replies are listed 'Best First'.
Re: copy files from one directory to another
by morgon (Deacon) on Aug 02, 2010 at 22:11 UTC
    Just a few comments that may or may not be helpful..
    What is this? Why do you localize @ARGV? Do you want to slurp the file? If so why?

    mkdir("$name",0777)||print $!;
    This creates a directory in the current working directory. Is that where you want it to be created?
    Are you sure you want $file[$i] (not sure what that contains) and not $files[$i] (the current filename).
      All of these are really great questions.I can see your depth of Perl knowledge. Yes i need to slurp the file I need to get some values from the files without going to the extent of getting data line by line and saving memory.Also I want the directory to be created in the present directory I am working in.I just needed to extract the current file which is $files$i that is the name of the file to extract them and run Perl scripts on the data.
Re: copy files from one directory to another
by ahmad (Hermit) on Aug 02, 2010 at 21:10 UTC

    I think the move command expect a file name (full path actually) not a directory as a second argument

    Update: Please ignore, apparently this is not true

      I figured it out the path is fine but i need to write $files$i instead of $file$i.Sorry for the inconvenience.

    Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (4)
    As of 2015-11-28 04:53 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (737 votes), past polls