Removing non empty directory from ftp, ruby, Net :: FTP

I have a project where I need to delete non-empty folders from ftp server. I tried with ftp.rmdir()

, but I got the error

The folder is not empty

Then I tried to just move the directory using the method ftp.rename()

, but I got an error there too.

Does anyone know a good way to do this?

+3


source to share


2 answers


Apparently FTP requires that you delete all files recursively.

This is a good example of how you can do this:

https://github.com/dsabanin/BetterFTP



def rm_r(path)
 return if @deleted_paths_cache.include?(path)
 @deleted_paths_cache << path
 if directory?(path) 
  chdir path

  begin
    files = nlst
    files.each {|file| rm_r "#{path}/#{file}"}
  rescue Net::FTPTempError
    # maybe all files were deleted already
  end

  rmdir path
else
  rm(path)
end

      

end

0


source


First you need to delete all files in this directory using mdelete

mdelete folder_name/*

then you should be able to remove the directory using rmdir



rmdir folder_name

Source

0


source







All Articles