perlnewbie9292
Programmer
Hello Perl experts, I am not sure if I am going about this the best way and hoping that someone here can point me in the right direction. What I am trying to do with no success is:
I have a .txt file which gets entries inserted into it with directory paths. Now what I would like to do is read that file and process each path one at a time (running a copy of the path and moving the file in that path to another directory) then remove the entry which I just process from the .txt file.
Now two questions I have:
With the code that I have so far I am not sure how to remove the line which I just finished processing from the .txt file. Then have the script reread the file that way when/if new lines gets added I am sure that I am reading from an updated file.
So once again in simpler terms this is what I am trying to do.
*read files with paths
*remove the /local/ from the path in the .txt file.
*copy or move from the entry in the .txt file to new location without the /local
*update the .txt file so the line which I just processed is *removed.
This is code that I have any help would be greatly appreciated as I am kinda stuck.
I have a .txt file which gets entries inserted into it with directory paths. Now what I would like to do is read that file and process each path one at a time (running a copy of the path and moving the file in that path to another directory) then remove the entry which I just process from the .txt file.
Now two questions I have:
With the code that I have so far I am not sure how to remove the line which I just finished processing from the .txt file. Then have the script reread the file that way when/if new lines gets added I am sure that I am reading from an updated file.
So once again in simpler terms this is what I am trying to do.
*read files with paths
*remove the /local/ from the path in the .txt file.
*copy or move from the entry in the .txt file to new location without the /local
*update the .txt file so the line which I just processed is *removed.
This is code that I have any help would be greatly appreciated as I am kinda stuck.
Code:
#!/usr/bin/perl
use strict;
use warnings;
use File::Path;
my $workingFile = "/tmp/test.txt"; #(This file contains fully qualified paths)
open(TXT,$workingFile)|| die("Cannot Open File $workingFile");
while (<TXT>) {
my $line = $_;
foreach ($line) {
chomp $line;
my $copyTo = $line;
$copyTo =~ s/\/local//;
chomp $copyTo;
if ( -d "$copyTo" ) {
print "$copyTo exists\n";
} else {
mkpath("$copyTo") or die "Could not create $copyTo using mkpath command $!\n";
print "$copyTo does not exist creating now\n";
}
print "Going to run the Copy Command to copy: $line to --> $copyTo\n";
system("/bin/cp", "-rf", "$line", "$copyTo");
print "Copy was successfull sleeping\n";
sleep 100;
}
}
close(TXT);