in reply to Get contents in an FTP path using glob

Am I correct in assuming that you are using the Net::FTP module?

Replies are listed 'Best First'.
Re^2: Get contents in an FTP path using glob
by toastbread (Novice) on Feb 01, 2010 at 19:54 UTC

    Yes, I'm using Net::FTP module.

    I want to search a directory content of a path in ftp based on glob. I want it just what the DOS command 'dir /b' does but 'ls' command of ftp has weird behavior when using glob chars.

      I think you want to grep the list returned by $ftp->ls. For example, I case-insensitively get a file like this:
      ... my $guess = shift; my $file = (grep { /$guess/i } $ftp->ls )[0] or return "Couldn't find $guess.\n"; ...

        It is something like this:

        !#/usr/bin/perl -w use strict; use Net::FTP; my $hostname = "sample-host"; my $ftp = Net::FTP->new($hostname); $ftp->cwd("mypath"); my @cat1 $ftp->ls("h?_*"); my @cat2 $ftp->ls("*-zips"); print "category 1:\n", join "\n",@cat1; print "category 2:\n", join "\n",@cat2; <>;

        I think I must prefer globbing than regex since not all user that will use my program has knowledge in regex.

        I have tried converting all * and ? into .* and .? respectively and use regex but before I finish coding it I thought about the "+" character which is a valid char for naming file or directory. It will be a problem when a file/folder contains a "+" char. since it is a special char in regex. And there are many other special char in regex which i don't want to make a code just to escape them all.

        It seems that it will become complicated if i use regex. What should I do..?