AcidHawk has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I am fighting with dir recursion. I have the following dir structure on machine A and want to ftp it to machine B.
I only want the files that are NOT on machine B to be put there (I.e. if they do not exist).
I am able to handle the dirs but I keep missing the files in subdir2 (I.e. the directories get created but not the files.) If I run this again (I.e. the dirs are already there the files get put in their respective folders.)test _ |_subdir1 | |_lowersub1 | |_lowertest1.txt | |_subdir2 | |_lowersub2 | | |_anotherlower2 | | | |_anotherlowertest2.txt | | |_lowersubtest2.txt | |_subtest2.txt | |_subdir3 |_test1.txt
This is what I have ...
#! /usr/bin/perl use strict; use warnings; use Net::FTP; my $test_exists = 0; my $ftp = Net::FTP->new("machineB", Debug => 0) or die "Cannot connect + to machineB: $@"; $ftp->login('usr','pass') or die "Cannot login ", $ftp->message; my $result = $ftp->ls("/myincomming") or die "ls failed ", $ftp->messa +ge; foreach (@{$result}) { print "$_\n"; if ($_ =~ /(.)myincomming(.)test/i) { print "Dir Exists\n"; $test_exists = 1; last; } } unless ($test_exists) { $ftp->mkdir("\\myincomming\\test") or die "mkd +ir failed ", $ftp->message; } my $firstdir = "./Test"; &ProcessStruct("$firstdir"); sub ProcessStruct { my $dir = shift; my $ftp_dir_exists = 0; my $file; print "DIR = $dir\n"; opendir DIR, $dir || die "cannot check dir $dir : $!\n"; my @files = readdir DIR; closedir DIR; foreach my $file(@files) { next if $file =~/^\./; if (-f "$dir/$file") { print "SEND $dir/$file to ftp server\n"; &Put_File($dir, $file); } else { $file = "$dir/$file"; my @ret = $ftp->ls("/Cubes/$file/.."); foreach (@ret) { my @subdirs = split /\\/, $_; if ($file =~ /$subdirs[$#subdirs]/i) { $ftp_dir_exists = 1; last; } } unless ($ftp_dir_exists) { $ftp->mkdir("/myincomming/$file +") or die " next mkdir failed ", $ftp->message; } $ftp_dir_exists = 0; &ProcessStruct("$file"); } } close DIR; } sub Put_File { my $dir = shift; my $file = shift; my @ret = $ftp->ls("/myincomming/$dir"); foreach (@ret) { if ($_ =~ /$file/i) { print "DONT PUT FILES = $_\n"; } else { $ftp->put("$dir/$file", "/myincomming/$dir/$file") or die +"Cannot put file ", $ftp->message; } } }
-----
Of all the things I've lost in my life, its my mind I miss the most.
Of all the things I've lost in my life, its my mind I miss the most.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Dir recursion
by kvale (Monsignor) on May 07, 2004 at 07:33 UTC | |
Re: Dir recursion
by Hena (Friar) on May 07, 2004 at 09:34 UTC | |
Re: Dir recursion
by ozone (Friar) on May 07, 2004 at 08:17 UTC | |
by AcidHawk (Vicar) on May 07, 2004 at 08:39 UTC |
Back to
Seekers of Perl Wisdom