Re: Moving Files
by toolic (Bishop) on Aug 07, 2008 at 16:23 UTC
|
| [reply] |
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
| [reply] [d/l] |
|
|
The advantage is that on (modern) systems, mv works if the target location is on a different device than the original file. rename fails.
| [reply] |
|
|
| [reply] [d/l] [select] |
|
|
|
|
| [reply] [d/l] |
Re: Moving Files
by Lawliet (Curate) on Aug 07, 2008 at 16:10 UTC
|
1. chdir(); or just system(mv ...);
2. system(perl script.pl);
3. Use crontab if under *nix, and there are schedulers for windows.
<(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
| [reply] [d/l] |
Re: Moving Files
by Bloodnok (Vicar) on Aug 07, 2008 at 16:12 UTC
|
| [reply] [d/l] [select] |
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. | [reply] [d/l] |
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:
system("/export/home/perl.exe myScript.pl");
b. Use the eval command:
open (F, '<', "test2.pl");
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 :) | [reply] [d/l] [select] |
Re: Moving Files
by tptass (Sexton) on Aug 08, 2008 at 00:33 UTC
|
| [reply] |
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;
}
| [reply] [d/l] |