IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Perl Discussion :

LECTURE des repertoires sur un ftp


Sujet :

Langage Perl

  1. #1
    Membre régulier Avatar de fripette
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    241
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 241
    Points : 71
    Points
    71
    Par défaut LECTURE des repertoires sur un ftp
    Bonjour a tous,

    Je suis desolee de tres certainement reposer cette question mais ca fait deux jours que je bloque dessus et je n'ai trouve de reponse adequate nulle part.

    Et d'ailleurs si Djibril parce par mon post et qu'a l'avenir mon script marche j'aimerais le soumettre parce que je ne l'ai trouve ni dans http://www.developpez.net/forums/d65...ponibles-perl/
    non plus ici :
    http://perl.developpez.com/sources/


    Mon probleme est simple, j'aimerai lister tous les directory et subdirectory presents dans un site ftp.

    J'etais partie sur ceci:http://cpansearch.perl.org/src/JDLEE...4/samples/rget

    mais je me suis rendue compte qu'il y avait effectivement un listing de ceux ci MAIS avec une recuperation en local de l'ensemble de l'architecture et des fichiers provenant du ftp.

    Donc j'ai abandonne cette piste.

    J'ai ensuite trouve ce bout de script:
    http://www.go4expert.com/forums/showthread.php?t=2348

    Et donc voila j'ai cru trouver mon bonheur mais la recursivite ne se fait "pas bien".

    Voici l'adresse ftp que je parse: ftp://ftp.ncbi.nlm.nih.gov/genbank/genomes/A_thaliana/

    Voici mon code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
     
    # required modules
    use Net::FTP;
    use Net::FTP::File;
    use Data::Dumper;
    use File::Listing qw(parse_dir);
     
     
    print("Connecting to FTP ... ");
    # create a new instance of the FTP connection
    $ftp = Net::FTP->new( "ftp.ncbi.nlm.nih.gov", Debug => 0 )
      or die("Cannot connect $!");
     
    # login to the server
    $ftp->login( "anonymous", "anonymous" ) or die("Login failed $!");
    print(" Logged in ... \n");
    @y = getRecursiveDirListing("/genbank/genomes/A_thaliana/");
    print "the join \n";
    print join( "\n", @y );
     
    # close the FTP connection
    $ftp->quit();
     
    sub getRecursiveDirListing {
     
    	# create an array to hold directories, it should be a local variable
    	local @dirs = ();
     
    	# directory parameter passed to the sub-routine
    	my $dir = $_[0];
     
      # if the directory was passed onto the sub-routin, change the remote directory
    	$ftp->cwd($dir) if ($dir);
     
     
    	# get the file listing
    	@ls = $ftp->ls('-lR');
     
    	# the current working directory on the remote server
    	my $cur_dir = $ftp->pwd();
    	print "cur_dir: ",$cur_dir,"\n";
    	# parse and loop through the directory listing
    	foreach my $name (@ls) {
    		my $isdir = $ftp->isdir($name);
    		next if(!$isdir);
     
       		push( @dirs, "$cur_dir/$name" );    # push the directory into the array   	
       		# recursive call to get the entries in the entry, and get an array of return values	
       		@xx = getRecursiveDirListing("$cur_dir/$name");	
    	}
    	# merge the array returned from the recursive call with the current directory listing
     
    	return (@dirs, @xx);
    }
    et le resultat de celui ci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    Connecting to FTP ...  Logged in ... 
    cur_dir: /genbank/genomes/A_thaliana
    cur_dir: /genbank/genomes/A_thaliana/OLD
    cur_dir: /genbank/genomes/A_thaliana/OLD/CHR_I
    the join 
    /genbank/genomes/A_thaliana/OLD
    /genbank/genomes/A_thaliana/OLD/CHR_I
    Donc comme vous pouvez le voir la recursivite se fait bien du repertoire OLD vers CHR_I, et malheureusement le foreach est foireux lors du OLD (ou plutot je ne comprends pas pourquoi les autres repertoires dans le @ls ne sont pas parses) car il y a aussi les sous repertoires CHRII a V qui ne sont pas traites


    Merci beaucoup d'avance pour votre aide precieuse (!!!),

    Sinon j'ai essaye d'adapter un bout de code de Djibril sur le parsing de dossier mais je ne vous le mets par parce qu'il ne marche pas non plus

  2. #2
    Expert confirmé

    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2009
    Messages
    3 577
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Avril 2009
    Messages : 3 577
    Points : 5 753
    Points
    5 753
    Par défaut
    J'ai ajouté "use strict", modifié quelques déclaration de variables, mais surtout, corrigé le bug : il faut faire cwd("..") à chaque retour de récursion :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    use strict;
    use Net::FTP;
    use Net::FTP::File;
    use Data::Dumper;
    use File::Listing qw(parse_dir);
     
     
    print("Connecting to FTP ... ");
    # create a new instance of the FTP connection
    my $ftp = Net::FTP->new( "ftp.ncbi.nlm.nih.gov", Debug => 0 )
      or die("Cannot connect $!");
     
    # login to the server
    $ftp->login( "anonymous", "anonymous" ) or die("Login failed $!");
    print(" Logged in ... \n");
    my @y = getRecursiveDirListing("/genbank/genomes/A_thaliana/");
    print "the join \n";
    print join( "\n", @y );
     
    # close the FTP connection
    $ftp->quit();
     
    sub getRecursiveDirListing {
     
    	# create an array to hold directories, it should be a local variable
    	my @dirs = ();
     
    	# directory parameter passed to the sub-routine
    	my $dir = $_[0];
     
      # if the directory was passed onto the sub-routin, change the remote directory
    	$ftp->cwd($dir) if ($dir);
     
     
    	# get the file listing
    	my @ls = $ftp->ls('-lR');
     
    	# the current working directory on the remote server
    	my $cur_dir = $ftp->pwd();
    	print "cur_dir: ",$cur_dir,"\n";
    	# parse and loop through the directory listing
    	foreach my $name (@ls) {
    		my $isdir = $ftp->isdir($name);
    		next if(!$isdir);
     
       		push( @dirs, "$cur_dir/$name" );    # push the directory into the array   	
       		# recursive call to get the entries in the entry, and get an array of return values	
       		push( @dirs,  getRecursiveDirListing("$cur_dir/$name"));	
    	}
    	# merge the array returned from the recursive call with the current directory listing
     
            $ftp->cwd("..");
    	return (@dirs);
    }
    Plus j'apprends, et plus je mesure mon ignorance (philou67430)
    Toute technologie suffisamment avancée est indiscernable d'un script Perl (Llama book)
    Partagez vos problèmes pour que l'on partage ensemble nos solutions : je ne réponds pas aux questions techniques par message privé
    Si c'est utile, say

  3. #3
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 498 771
    Points
    498 771
    Par défaut
    Voici ton code très simplifié, plus compact et propre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    #!/usr/bin/perl
    use warnings;
    use strict;
    use Carp;
     
    # required modules
    use Net::FTP;
    use Net::FTP::File;
     
    print "Connecting to FTP ... \n";
     
    # create a new instance of the FTP connection
    my $ftp = Net::FTP->new( "ftp.ncbi.nlm.nih.gov", Debug => 0 ) or die("Cannot connect $!");
     
    # login to the server
    $ftp->login( "anonymous", "anonymous" ) or die("Login failed $!");
    print " Logged in ... \n";
    getRecursiveDirListing( $ftp, "/genbank/genomes/A_thaliana" );
     
    # close the FTP connection
    $ftp->quit();
     
    sub getRecursiveDirListing {
      my ( $ftp, $repertoire_distant ) = @_;
     
      print "$repertoire_distant\n";
      $ftp->cwd($repertoire_distant) or die "CWD impossible : $repertoire_distant\n", $ftp->message;
     
      # parse and loop through the directory listing
      foreach my $filerep ( $ftp->ls() ) {
        my $nom_complet = "$repertoire_distant/$filerep";
        if ( $ftp->isdir($filerep) ) {
          getRecursiveDirListing( $ftp, $nom_complet );
        }
        else {
          print "$nom_complet\n";
        }
      }
     
      $ftp->cwd('..') or die "Retour arrier impossible\n", $ftp->message;
     
      return;
    }

  4. #4
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 498 771
    Points
    498 771
    Par défaut
    argffff: Philou m'a grillé . Bon en même temps, il a repris ton code, moi je l'ai modifié

  5. #5
    Expert confirmé

    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2009
    Messages
    3 577
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Avril 2009
    Messages : 3 577
    Points : 5 753
    Points
    5 753
    Par défaut
    Ton code est effectivement mieux écrit, Djibril. Je regrette simplement la suppression de l'usage d'un tableau des répertoires, remplacé par un print. En effet, l'usage d'un tableau est plus souple pour qui veut paramétrer la sortie de ces données (fichier, STDOUT, Data::Dumper, autre traitement).
    Plus j'apprends, et plus je mesure mon ignorance (philou67430)
    Toute technologie suffisamment avancée est indiscernable d'un script Perl (Llama book)
    Partagez vos problèmes pour que l'on partage ensemble nos solutions : je ne réponds pas aux questions techniques par message privé
    Si c'est utile, say

  6. #6
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 498 771
    Points
    498 771
    Par défaut
    Oui c'est vrai. En fait, j'ai mis des print pour qu'il puisse voir ce que ça donne dans un affichage progressif. Mais on aurait effectivement pu tout stocker dans un tableau que le code ci-dessous. A noter que je stocke tout (répertoires et fichiers) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    #!/usr/bin/perl
    use warnings;
    use strict;
    use Carp;
     
    # required modules
    use Net::FTP;
    use Net::FTP::File;
     
    print "Connecting to FTP ... \n";
     
    # create a new instance of the FTP connection
    my $ftp = Net::FTP->new( "ftp.ncbi.nlm.nih.gov", Debug => 0 ) or die("Cannot connect $!");
     
    # login to the server
    $ftp->login( "anonymous", "anonymous" ) or die("Login failed $!");
    print "Logged in ... \n";
     
    my @data= ();
    getRecursiveDirListing( $ftp, "/genbank/genomes/A_thaliana", \@data );
    foreach ( @data ) {
      print "$_\n";
    }
     
    # close the FTP connection
    $ftp->quit();
     
    sub getRecursiveDirListing {
      my ( $ftp, $repertoire_distant, $ref_data ) = @_;
     
      push ( @{$ref_data}, $repertoire_distant);
      $ftp->cwd($repertoire_distant) or die "CWD impossible : $repertoire_distant\n", $ftp->message;
     
      # parse and loop through the directory listing
      foreach my $filerep ( $ftp->ls() ) {
        my $nom_complet = "$repertoire_distant/$filerep";
        if ( $ftp->isdir($filerep) ) {
          getRecursiveDirListing( $ftp, $nom_complet, $ref_data );
        }
        else {
          push ( @{$ref_data}, $nom_complet);
        }
      }
     
      $ftp->cwd('..') or die "Retour arrier impossible\n", $ftp->message;
     
      return;
    }
    Je pense que le code est assez simple pour l'adapter à tout besoin.

  7. #7
    Membre régulier Avatar de fripette
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    241
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 241
    Points : 71
    Points
    71
    Par défaut
    Merci beaucoup a vous deux pour votre reponse rapide et efficace !

    Je vais garder precieusement vos bouts de code.

  8. #8
    Membre régulier Avatar de fripette
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    241
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 241
    Points : 71
    Points
    71
    Par défaut
    Bon,
    C'est beaucoup trop long.

    Je ne voudrais pas vous demander la lune, mais apres avoir cible mon repertoire d'interet j'aimerais que le parseur ne descends pas autant en profondeur dans les repertoires.
    Est ce que c'est realisable ?

  9. #9
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 498 771
    Points
    498 771
    Par défaut
    Tout est faisable à part le thé . Non sincèrement, c'est faisable, mais faut clarifier tes besoins. Si tu connais le répertoire d'intérêt, pourquoi chercher à tout lister alors ?

  10. #10
    Membre régulier Avatar de fripette
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    241
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 241
    Points : 71
    Points
    71
    Par défaut
    Je connais la structure ACTUELLE du repertoire d'interet mais je voudrais rendre mon script viable en cas de changement de structure et donc ne pas mettre de sous repertoire en dur.

    Regarde le repertoire que je vais appeler "Header":ftp://ftp.ncbi.nlm.nih.gov/genbank/genomes/Eukaryotes/


    Il semble qu'il y a 5 categories bien definies qui ensuite vont referencer des especes et en fait je voudrais qu'il s'arrete a par exemple:
    /Eukaryotes/types_Eukaryotes/nom_espece. (ou un dernier niveau en dessous)

    et qu'il aille pas forcement dans le trou du cul du monde pour chaque espece
    comme par exemple pour Acyrthosiphon_pisum:
    Eukaryotes/invertebrates/Acyrthosiphon_pisum/Acyr_2.0/Primary_Assembly/unplaced_scaffolds/FASTA/

  11. #11
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 498 771
    Points
    498 771
    Par défaut
    Là il faut se creuser les méninges .
    Il faut trouver une astuce pour garder en mémoire la profondeur du répertoires.

  12. #12
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 498 771
    Points
    498 771
    Par défaut
    Allez cadeau, lance le programme et essaye de le comprendre.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    #!/usr/bin/perl
    use warnings;
    use strict;
    use Carp;
     
    # required modules
    use Net::FTP;
    use Net::FTP::File;
     
    print "Connecting to FTP ... \n";
     
    # create a new instance of the FTP connection
    my $ftp = Net::FTP->new( "ftp.ncbi.nlm.nih.gov", Debug => 0 ) or die("Cannot connect $!");
     
    # login to the server
    $ftp->login( "anonymous", "anonymous" ) or die("Login failed $!");
    print "Logged in ... \n";
     
    my @data= ();
     
    my $profondeur_repertoire;
    # Limite profondeur
    my $limite = 4;
    print <<"RESUME";
      ===============================================================================
      Parcours repertoire distant via FTP avec un niveau de profondeur de $limite
      ===============================================================================
     
    RESUME
     
    getRecursiveDirListing( $ftp, "/genbank/genomes/Eukaryotes", \@data, \$profondeur_repertoire, $limite );
    foreach ( @data ) {
      print "$_\n";
    }
     
    # close the FTP connection
    $ftp->quit();
     
    # Parcours recursif FTP avec profondeur définie
    sub getRecursiveDirListing {
      my ( $ftp, $repertoire_distant, $ref_data, $ref_profondeur_repertoire ) = @_;
     
      push ( @{$ref_data}, $repertoire_distant);
      print "$repertoire_distant\n";
      $ftp->cwd($repertoire_distant) or die "CWD impossible : $repertoire_distant\n", $ftp->message;
      ${$ref_profondeur_repertoire}++;
     
      # parse and loop through the directory listing
      foreach my $filerep ( $ftp->ls() ) {
        my $nom_complet = "$repertoire_distant/$filerep";
        if ( $ftp->isdir($filerep) ) {
          if ( ${$ref_profondeur_repertoire} < $limite ) {
            print "Profondeur : ${$ref_profondeur_repertoire} OK\n";
            getRecursiveDirListing( $ftp, $nom_complet, $ref_data, $ref_profondeur_repertoire, $limite);
          }
          else {
            print "Profondeur depassee : ${$ref_profondeur_repertoire} - suivant\n";
          }
     
        }
        else {
          push ( @{$ref_data}, $nom_complet);
          print "$nom_complet\n";
        }
      }
     
      $ftp->cwd('..') or die "Retour arrier impossible\n", $ftp->message;
      ${$ref_profondeur_repertoire}--;
     
      return;
    }

  13. #13
    Expert confirmé

    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2009
    Messages
    3 577
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Avril 2009
    Messages : 3 577
    Points : 5 753
    Points
    5 753
    Par défaut
    @djbril : il n'est généralement pas nécessaire de passer récursivement des variables par référence. Exemple pour la profondeur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    function_recursive(0);
     
    sub fonction_recursive {
      my ($profondeur) = @_;
     
      while (...) {
        fonction_recursive($profondeur+1);
      }
    }
    Il n'est alors jamais nécessaire de décrémenter la profondeur au retour de la fonction récursive, puisque l'on se retrouve avec la valeur de $profondeur de l'appel récursif précédent.
    C'est aussi de cette manière que fonctionne les implémentations de suites récursives, ou autre calcul de factorielle :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    sub factorielle {
      my ($fact) = @_;
      return $fact ? factorielle($fact-1) * $fact : 1;
    }
     
    print "fact(10)=".factorielle(10);
    Plus j'apprends, et plus je mesure mon ignorance (philou67430)
    Toute technologie suffisamment avancée est indiscernable d'un script Perl (Llama book)
    Partagez vos problèmes pour que l'on partage ensemble nos solutions : je ne réponds pas aux questions techniques par message privé
    Si c'est utile, say

  14. #14
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 498 771
    Points
    498 771
    Par défaut
    Merci philou pour cette remarque. J'en prendrais acte dans mes futurs scripts .

  15. #15
    Membre régulier Avatar de fripette
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    241
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 241
    Points : 71
    Points
    71
    Par défaut
    Merci beaucoup !!!
    Je vais prendre le temps de lire et de comprendre ton bout de script et aussi la remarque de Philou !

    Merci encore pour vos reponses rapides !

  16. #16
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 498 771
    Points
    498 771
    Par défaut
    Code amélioré :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    #!/usr/bin/perl
    use warnings;
    use strict;
    use Carp;
     
    use Net::FTP;
    use Net::FTP::File;
     
    print "Connecting to FTP ... \n";
     
    # create a new instance of the FTP connection
    my $ftp = Net::FTP->new( "ftp.ncbi.nlm.nih.gov", Debug => 0 ) or die("Cannot connect $!");
     
    # login to the server
    $ftp->login( "anonymous", "anonymous" ) or die("Login failed $!");
    print "Logged in ... \n";
     
    my @data= ();
    # Limite profondeur
    my $limite = 4;
    print <<"RESUME";
      ===============================================================================
      Parcours repertoire distant via FTP avec un niveau de profondeur de $limite
      ===============================================================================
     
    RESUME
     
    getRecursiveDirListing( $ftp, "/genbank/genomes/Eukaryotes", \@data, 0, $limite );
    foreach ( @data ) {
      print "$_\n";
    }
     
    # close the FTP connection
    $ftp->quit();
     
    # Parcours recursif FTP avec profondeur définie
    sub getRecursiveDirListing {
      my ( $ftp, $repertoire_distant, $ref_data, $profondeur, $limite ) = @_;
     
      push ( @{$ref_data}, $repertoire_distant);
      print "$repertoire_distant\n";
      $ftp->cwd($repertoire_distant) or die "CWD impossible : $repertoire_distant\n", $ftp->message;
     
      # parse and loop through the directory listing
      foreach my $filerep ( $ftp->ls() ) {
        my $nom_complet = "$repertoire_distant/$filerep";
        if ( $ftp->isdir($filerep) ) {
          if ( $profondeur < $limite ) {
            print "Profondeur : $profondeur OK\n";
            getRecursiveDirListing( $ftp, $nom_complet, $ref_data, $profondeur+1, $limite);
          }
          else {
            print "Profondeur depassee : $profondeur - listing interdit\n";
          }
     
        }
        else {
          push ( @{$ref_data}, $nom_complet);
          print "$nom_complet\n";
        }
      }
     
      $ftp->cwd('..') or die "Retour arrier impossible\n", $ftp->message;
     
      return;
    }

  17. #17
    Expert confirmé

    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2009
    Messages
    3 577
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Avril 2009
    Messages : 3 577
    Points : 5 753
    Points
    5 753
    Par défaut
    Code encore amélioré :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
     
    #!/usr/bin/perl
    use warnings;
    use strict;
    use Carp;
     
    use Net::FTP;
    use Net::FTP::File;
     
    print "Connecting to FTP ... \n";
     
    # create a new instance of the FTP connection
    my $ftp = Net::FTP->new( "ftp.ncbi.nlm.nih.gov", Debug => 0 ) or die("Cannot connect $!");
     
    # login to the server
    $ftp->login( "anonymous", "anonymous" ) or die("Login failed $!");
    print "Logged in ... \n";
     
    my @data= ();
    # Limite profondeur
    my $limite = 4;
    print <<"RESUME";
      ===============================================================================
      Parcours repertoire distant via FTP avec un niveau de profondeur de $limite
      ===============================================================================
     
    RESUME
     
    @data = getRecursiveDirListing( $ftp, "/genbank/genomes/Eukaryotes", 0, $limite );
    foreach ( @data ) {
      print "$_\n";
    }
     
    # close the FTP connection
    $ftp->quit();
     
    # Parcours recursif FTP avec profondeur définie
    sub getRecursiveDirListing {
      my ( $ftp, $repertoire_distant, $profondeur, $limite ) = @_;
      my @data;
     
      push ( @data, $repertoire_distant);
      print "$repertoire_distant\n";
      $ftp->cwd($repertoire_distant) or die "CWD impossible : $repertoire_distant\n", $ftp->message;
     
      # parse and loop through the directory listing
      foreach my $filerep ( $ftp->ls() ) {
        my $nom_complet = "$repertoire_distant/$filerep";
        if ( $ftp->isdir($filerep) ) {
          if ( $profondeur < $limite ) {
            print "Profondeur : $profondeur OK\n";
            push @data, getRecursiveDirListing( $ftp, $nom_complet,  $profondeur+1, $limite);
          }
          else {
            print "Profondeur depassee : $profondeur - listing interdit\n";
          }      
        }
        else {
          push ( @data, $nom_complet);
          print "$nom_complet\n";
        }
      }
     
      $ftp->cwd('..') or die "Retour arrier impossible\n", $ftp->message;
     
      return @data;
    }
    (pas de référence de données à passer).
    Plus j'apprends, et plus je mesure mon ignorance (philou67430)
    Toute technologie suffisamment avancée est indiscernable d'un script Perl (Llama book)
    Partagez vos problèmes pour que l'on partage ensemble nos solutions : je ne réponds pas aux questions techniques par message privé
    Si c'est utile, say

  18. #18
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 498 771
    Points
    498 771
    Par défaut
    , je l'avais zappé cette référence. Merci philou.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Lecture des fichiers sur un serveur unix
    Par ben.83 dans le forum Développement de jobs
    Réponses: 1
    Dernier message: 15/10/2009, 15h03
  2. Ecriture et lecture des ressources sur une .exe
    Par LeRoi dans le forum Delphi
    Réponses: 8
    Dernier message: 06/10/2006, 22h46
  3. arborescence des repertoires sur XP
    Par Nemesys dans le forum Windows XP
    Réponses: 11
    Dernier message: 18/05/2006, 23h17
  4. [] [Réseau] Liste des répertoires d'un FTP
    Par Maitre Kanter dans le forum VB 6 et antérieur
    Réponses: 5
    Dernier message: 12/03/2003, 16h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo