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).

test _ |_subdir1 | |_lowersub1 | |_lowertest1.txt | |_subdir2 | |_lowersub2 | | |_anotherlower2 | | | |_anotherlowertest2.txt | | |_lowersubtest2.txt | |_subtest2.txt | |_subdir3 |_test1.txt
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.)

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.

In reply to Dir recursion by AcidHawk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.