I am trying to old delete files from an FTP using Ruby net/sftp
, but I keep getting an error saying the file does not exist.
:error=>"Net::SFTP::StatusException (2, \"no such file\")"
I can manually delete files when logging in using the same creds, so I know I have permission.
require 'net/sftp'
ftp = Net::SFTP.start(@ftp_url, @ftp_user, :password => @ftp_pwd)
ftp.dir.entries('somePath').each do |entry|
begin
age_days = (Time.now.to_i - entry.attributes.atime) / 86400
if(age_days > ftp_max_file_age_days)
ftp.remove!(entry.name)
end
rescue Exception => e
# log error here
end
end
I prefer remove!
so everything happens synchronously in this case, but I have also tried remove
.
I also tried giving it the full path of the file instead of just the entry name (like 'somePath' + entry.name
instead of just entry.name
). I was thinking perhaps it was because I needed to change the working directory, which apparently net/sftp
does not allow.
Thanks in advance!