Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Net::FTP file type and attributes

by fedelman (Novice)
on Jan 22, 2004 at 19:07 UTC ( #323291=perlquestion: print w/replies, xml ) Need Help??

fedelman has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,
I'm developing a script for different synchronization tasks (remote2local, local2remote, remote2remote, local2local).

I've got the following HASH:


$path = full path for an archive or directory
type = 'f' or 'd'. File or directory.
size = The size for the file file.
mtime = Modification time for the file.

When a I read the %src I need to know if the $path exist into the other side. In other words, I need to know if the $path into the remote side.

I've got a problem, when the source is local and the destination is an FTP.
Why? Simple, Both, Net:FTP and FTP protocol, doesn't provide the way to know if a $path is a 'directory' or 'file'.

I don't know what is the best way to fix.

Can anybody give me your opinion about what I would to do.

Thanks very much.

Replies are listed 'Best First'.
Re: Net::FTP file type and attributes
by pg (Canon) on Jan 22, 2004 at 20:30 UTC

    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',
      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:


      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.

      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.
Re: Net::FTP file type and attributes
by halley (Prior) on Jan 22, 2004 at 19:45 UTC
    If you can't change the working directory, it's not a directory.
    print "file" if not $ftp->cwd($filename);
    If you can change the working directory, you may have to change back to the parent or starting directory at some point after your query.

    [ e d @ h a l l e y . c c ]

      Hi, thanks for your reply.

      If the $filename is a file (with the same name of SRC directory) or if the $filename is a directory and his permissions is not suitable, the $ftp->cwd($filename) will be fail.
Re: Net::FTP file type and attributes
by mce (Curate) on Jan 23, 2004 at 13:30 UTC

    Have a look at Net::FTP::Recursive.

    This supports functions as rdir and rls to recursively get directy listings.

    So, you could do a rdir on a directory you know exists on the ftp server, and 'grep' your file from it.

    I hope this helps,

    Dr. Mark Ceulemans
    Senior Consultant
    BMC, Belgium

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://323291]
Approved by monktim
Front-paged by blue_cowdawg
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2023-05-31 00:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found