in reply to Net::FTP file type and attributes

Not really.

Net::FTP supports a command called dir() which can give you a detailed list of remote entities, that clearly indicate whether the entity is a directory or a file. I tested with the following code, local win2k, remote unix (trust that you will get a similar list if remote is non-unix):

use Data::Dumper; use Net::FTP; $ftp = Net::FTP->new("blah", Debug => 0) or die "Cannot connect: $@"; + $ftp->login("foo",'bar') or die "Cannot login ", $ftp->message; my @list = $ftp->dir(); print Dumper(\@list); $ftp->quit;

I got something like (only a portion):

'drwxr-xr-x 7 leinbd aagdev 512 Oct 17 18:06 leinb +d', 'drwxr-xr-x 11 lois aagdev 1024 Nov 14 14:04 lois' +, 'drwx------ 2 root system 512 Nov 10 15:41 lost+ +found', 'drwxr-xr-x 2 maynec aagdev 512 Nov 20 10:05 mayne +c', 'drwxr-xr-x 10 nanayn aagdev 512 Oct 07 09:52 nanay +n', 'drwxr-xr-x 5 root system 512 Nov 10 15:41 netin +st',

Replies are listed 'Best First'.
Re: Re: Net::FTP file type and attributes
by fedelman (Novice) on Jan 22, 2004 at 21:17 UTC
    Hi, thanks for your reply.

    The $filename should be a simple entry like a file or directory. I need check only for one entry, not for all directory content.

    Futhermore, the $ftp->dir return different content according to FTP server. For example, the following commands return the same result:

    $ftp->dir("/a/real/empty/directory");
    $ftp->dir("/unexistent/directory/");

    I think the solution is more complex. :(

    Thank you anyway.

      No it's not more complex, you just won't listen to reason :)

      If you start checking those directories from an FTP root, you will be able to check by descending that directory root whether /nonexistant/directory actually exists. Namely, "nonexistant" will not exist in the directory root. I'm speaking algorithmically here, as you have an algorithmic problem, not a Perl module problem.

      As for the dir format being inconsistant between servers, any server should include enough for you to regex the beast and tell whether you have a directory or a file. Such is a limitation in FTP, not neccessarily in the Perl module. Dir is only a command that returns text, by definition of FTP. If this really bugs you, try an SFTP module.

Re: Re: Net::FTP file type and attributes
by Anonymous Monk on Feb 21, 2004 at 02:49 UTC
    I had the same problem not knowing another option I wrote a small function to tell if it is a directory or not given the dir() output.
    sub is_dir { + my @list = split(//, $_); if( $list[0] eq 'd' ) { return 1; } else { return 0; } + }
    Worked for me. Let me let me know if there is a better way.