Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Moving Files

by Anonymous Monk
on Aug 07, 2008 at 16:02 UTC ( #702913=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks!

Two questions for you all!

1. How do I move things from one directory to another in a perl script. SOO for example, I have the following directory structure:


----- DIR 1

----- DIR 2

---------- DIR 2 SUB 1

I have a perl script which executes in DIR 1 and generates a file, but I want the file to be placed in DIR 2. As far as I know, this isn't easy. It's easy to say place a file from DIR 2 into DIR 2 Sub. I could of course physical move the files but I would rather know if I could do this using a perl script.

2. How do I make a perl script that executes several other perl scripts?

My last sneaky question is, say I want to make a script that updates certain files once a month (i.e. it will FTP them) is there I way I could automate this? Or would I have to manually tell the update script to run each month?

Any help would be great : )

Replies are listed 'Best First'.
Re: Moving Files
by toolic (Bishop) on Aug 07, 2008 at 16:23 UTC
Re: Moving Files
by cdarke (Prior) on Aug 07, 2008 at 16:27 UTC
    1. rename
    Please don't execute another program to do that (system(mv))- a kitten dies each time you run a child process (honest).

    Other questions already answered
      The advantage is that on (modern) systems, mv works if the target location is on a different device than the original file. rename fails.

      Ouch. Low blow -.-


      <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
Re: Moving Files
by Lawliet (Curate) on Aug 07, 2008 at 16:10 UTC

    1. chdir(); or just system(mv ...);
    2. system(perl;
    3. Use crontab if under *nix, and there are schedulers for windows.

    <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
Re: Moving Files
by Bloodnok (Vicar) on Aug 07, 2008 at 16:12 UTC
    1. File::Copy
    2. Write a script that invokes them e.g. using system or using the backticks notation
    3. You don't say which OS you're on, but either man crontab (for *NIX) or create a scheduled task (for Windoze)
    HTH ,

    A user level that continues to overstate my experience :-))
Re: Moving Files
by JavaFan (Canon) on Aug 07, 2008 at 16:11 UTC
    Well, it's as easy to place a file in DIR1 as it is in DIR2. Just use the path name (either absolute or relative) when opening the file you generate. There's no point in putting the file in DIR1 first and then move it into DIR2, unless there are some non-perl reasons for it.
    open my $fh, ">", "/whatever/path/you/need/Main/DIR2/file" or die; # +Absolute open my $fh, ">", "../DIR2" or die; # Relative
    As for question 2, I'd most likely use "system", although opening a pipe or a fork/exec might be appropriate as well.
Re: Moving Files
by leonidlm (Pilgrim) on Aug 07, 2008 at 16:17 UTC
    Hi, 1. Please explain in more details what you want to perform
    2. You can do it in several ways:
    a. Run it as another program:
    b. Use the eval command:
    open (F, '<', ""); eval <F>; close (F);
    This one will evaluate only one line, but you can read the whole file into one scalar and run it
    c. What operation system you are using?
    Hope I helped :)
Re: Moving Files
by tptass (Sexton) on Aug 08, 2008 at 00:33 UTC

    Moving Files:

    • Simply use the Perl rename function.
    • Why can't you simply just use open to redirect the output to the correct place in the first place? But yet again you aren't probably just writing out information...

    Executing other perl scripts:

    • You can use the perl system command, likes others have mentioned above.
    • Or, if you implement the sub-perl script as subroutines , you could use do. You basically pass the name of the file to be processed as an argument to the subroutine, rather than a command-line argument. Make sure there are no global variables shared between the scripts. Then, it should be easy to execute 'do ""' in, which will make the subroutine(s) in available to the statements in

    Updates once a month:

    • Use a cronjob or scheduler to perform this.

Re: Moving Files
by eosbuddy (Scribe) on Aug 07, 2008 at 17:17 UTC
    Learning Perl ... Randal et al: exercise: 5, chap12. rename is your friend. You could also use File::Base - holy ones would be able to ratify this better :-). I had written the following code as a solution to the exercise... the code requires that you work from your dir1 (i.e. the folder in which you have all the files to be moved) - it is buggy - please be warned (as I'd written this as a n00b and haven't had time to debug it since).
    #!/usr/bin/perl use strict; use warnings; if ($#ARGV > 1) { print "ARGV greater than 1\n"; unless (-e $ARGV[$#ARGV]) { mkdir $ARGV[$#ARGV], 0755 or die "Cannot create directory: + $!"; } my $dir = $ARGV[$#ARGV]; my $last=$#ARGV-1; foreach (0..$last) { if (-f $ARGV[$_]) { if (-e "$ARGV[$#ARGV]/$ARGV[$_]") { print "$ARGV[$#ARGV]/$ARGV[$_]\n"; print "File $ARGV[$_] already exists in $AR +GV[$#ARGV]\n"; print "Overwrite?:(y/n)\n"; chomp (my $line = <STDIN>); next if ($line =~ /^(\s.*|n)/i); } my $old = "$ARGV[$_]"; my $new = "$dir/$ARGV[$_]"; rename $old, $new; } } } if ($#ARGV == 1) { print "ARGV equal to 1\n"; if (-f $ARGV[0]) { rename $ARGV[0], $ARGV[1]; } if (-d $ARGV[0]) { unless (-e $ARGV[1]) { mkdir $ARGV[1], 0755 or die "Cannot create directory: $!", } if (-e "$ARGV[1]/$ARGV[0]") { print "File $ARGV[0] already exists in $ARGV[1]\n"; print "Overwrite?:(y/n)\n"; chomp (my $line = <STDIN>); last if ($line =~ /^(\s.*|n)/i); } foreach my $file (glob "$ARGV[0]/*") { my $oldfile = $file; $file =~ s/$ARGV[0]/$ARGV[1]/; rename $oldfile, $file; } } } if ($#ARGV < 1) { print "ARGV less than 1\n"; print "There should be atleast two arguments for this to work\n"; last; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://702913]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2017-10-21 13:02 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (270 votes). Check out past polls.