Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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;

Replies are listed 'Best First'.
Re: copy files from one directory to another
by morgon (Curate) 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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (7)
    As of 2018-06-19 02:03 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (111 votes). Check out past polls.