Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

ftp directories

by mbond (Beadle)
on Sep 14, 2001 at 20:00 UTC ( [id://112452]=perlquestion: print w/replies, xml ) Need Help??

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

I wrote a script to connect to 3 different servers via net::ftp. The program checks to see wether an entry in the home directory is a file or a directory.

If it is a directory, deletes the directory and puts a file on the server with the same name.

This is to fix a bug with another program that we have to use (and for the time being have to live with the bug).

The 3 servers are running on 3 different OS's (NT, Solaris, Linux) and my question is:

Is there an easy way to determine if an entry in the returned list from $ftp->dir; is a directory or file?

Right now i recursively check based on the first character of each line returned if it is a unix box OR for the <DIR> tag if it is on the windows box.

It was a quick hack and i want to do something more ellegant than this, so that if one of the OS's changes my script doesn't need to be modified.

Thanks

Mbond

Replies are listed 'Best First'.
How about this?
by Rhose (Priest) on Sep 14, 2001 at 22:47 UTC
    Since you want to remove the directory anyway (and I am going to assume you know the directories will always be empty) maybe you could use the following:

    use strict; use Net::FTP; use constant SERVER => 'server.somewhere.com'; use constant USERID => 'anonymous'; use constant PASSWORD => 'my.mail@somewhere.com'; use constant RMTDIR => '/tmp'; my $mFTP; #-- Open the connection $mFTP = Net::FTP->new(SERVER, Debug => 0); $mFTP->login(USERID,PASSWORD); #-- Process the contents $mFTP->cwd(RMTDIR); foreach ($mFTP->ls) { $mFTP->rmdir($_); if ($mFTP->ls($_)) { print $_," is a file...\n"; } else { print $_," *was* a directory...\n"; #-- Add file create code here... } } #-- Close the connection $mFTP->quit;
Re: ftp directories
by suaveant (Parson) on Sep 14, 2001 at 20:49 UTC
    Well... a true and reliable way would be to do a list on that dir/. (I think) if it came back with stuff, it's a dir... otherwise you could cd in and back out if it succeeds... either way isn't terribly efficient, but will let you know beyond a shadow of a doubt. :)

                    - Ant
                    - Some of my best work - Fish Dinner

Re: ftp directories
by earthboundmisfit (Chaplain) on Sep 14, 2001 at 20:17 UTC
    update: Darn it. I just realized this won't work for you. LIST still seems the best way to do this

    stat() and File::stat should be able to help in this regard.

    $mode gives type as well as permissions.

      I thought of that, but haven't been able to find a way to run it over the ftp connection.

      if i run it locally (@list = stat(FILEHANDLE);)its gonna give me an error because the file is on the remort server. i would have to run it like @list = stat($ftp->get($filename)); .. but that would cause me to retrieve the file, not what i am looking for.

      According the the Net:FTP docs Stat is the FTP stat command, which returns the server status, not of a specific file. (and its not implimented yet from what i can tell)

      Mbond
        >its gonna give me an error because the file is on the remort server.

        Yes, I realized that too late. Could you try a telnet session to each box instead of FTP?

        update: Assuming that's not feasible, you could do $ftp->cwd($path,$filename) If that succeeds, delete it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://112452]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2024-04-18 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found