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

I would like to delete all the file in a directory using only "rmdir" in perl script. I'm trying to clean up a directory first and then trying to write file. I know i can just use rmtree (" directory path"); but i'm unable to use that for FTP server (use Net::FTP;). and rmdir looks for empty directory. i have tried "remove_tree" and "rm -rf". i do have read/write access to the server, but i'm unable to delete files. Perl script:
finddepth (\&remove_dir, "$path"); rmdir ( "$path" ) or die ("Could not remove $path"); sub remove_dir { # for a path, this will be 0 if ( ! (stat("$File::Find::name"))[7] ) { $ftp->rmdir("$File::Find::name"); } else { $ftp->unlink("$File::Find::name"); } }

Replies are listed 'Best First'.
Re: Recursive delete files using perl script
by x-lours (Sexton) on Jun 30, 2014 at 14:14 UTC
    you could find everything in

    http://perldoc.perl.org/Net/FTP.html

    you have to use the ls ( [ DIR ] ) to know if the directory is empty and then loop for the files of your directory to delete ( FILENAME ) all the files before using rmdir ( DIR )

    or you can try to use the RECURSE option of rmdir : rmdir ( DIR , RECURSE ) Remove the directory with the name DIR . If RECURSE is true then rmdir will attempt to delete everything inside the directory.

Re: Recursive delete files using perl script
by Corion (Patriarch) on Jun 30, 2014 at 13:23 UTC

    stat does not work over FTP. I don't know how to learn about the status of a remote file/directory using ftp.