Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found