Skip to content

Instantly share code, notes, and snippets.

@chaserx
Created July 19, 2013 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaserx/6039440 to your computer and use it in GitHub Desktop.
Save chaserx/6039440 to your computer and use it in GitHub Desktop.
various failed attempts to remove non-empty folder via FTP cobbled together from example code and internal methods from various stackoverflow answers and gems.
REGEX = /^(?<type>.{1})(?<mode>\S+)\s+(?<number>\d+)\s+(?<owner>\S+)\s+(?<group>\S+)\s+(?<size>\d+)\s+(?<mod_time>.{12})\s+(?<filename>.+)$/
def delete_directory_contents(path, verbose = true)
# files = @ftp_conn.nlst(path)
# files.each do |file|
# if directory?("#{path}/#{file}")
# delete_directory_contents "#{path}/#{file}"
# @ftp_conn.rmdir("#{path}/#{file}")
# else
# @ftp_conn.delete("#{path}/#{file}")
# end
# end
# @ftp_conn.rmdir(path)
###########
# recurse = []
# @ftp_conn.list(path) do |e|
# entry = Net::FTP::List.parse(e)
# match_data = e.match(REGEX)
# @paths = "#{path}/#{entry.basename}".gsub(/\/+/, '/')
# if entry.dir?
# recurse << @paths
# elsif entry.file?
# @ftp_conn.delete("#{path}/#{match_data[:filename]}")
# else
# #
# end
# recurse.each do |r|
# delete_directory_contents(r)
# end
# end
###########
# begin
# puts "begin"
# flist = @ftp_conn.nlst(path)
# flist.each do |f|
# # Try to delete the file. If we fail then it's a directory
# begin
# puts "internal begin"
# if directory?(File.join(path,f))
# rmrf_dir(File.join(path,f))
# else
# @ftp_conn.delete(File.join(path,f))
# puts "rm #{f}" if verbose
# end
# rescue Exception => e
# rmrf_dir(File.join(path,f), verbose)
# end
# end
# rescue Exception => e
# # If the directory is empty, error silently and continue execution
# end
# @ftp_conn.rmdir(path)
# puts "rm -rf #{path}" if verbose
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment