Deleting files with Net/ftp

Hi,
I’m having to go to a website every day to pull files for editors at my
company. Unfortunately, the web site isn’t maintained that well–they
keep old files. I have the power to, and I’d like to, delete any old
files that aren’t stamped with today’s date. Can someone help me? I’m
getting Net::FTPPermError errors when I run this.

I’ve modified the real names below to protect the innocent. But, the
filenames over there look like this:

lobbyreg-20071215-160004.xml

My code:
ftp = Net::FTP.open(‘ftpserver.zq.com’)
ftp.login(‘zna’,’&[email protected]’)
ftp.chdir("/data/data/zna")
t = Time.now
t = t.strftime("%Y%m%d")
if Dir.glob(".xml") then
xmlfiles = ftp.list(’
.xml’)
xmlfiles.each do |xmlfile|
if xmlfile.to_s.match(t.to_s) then
puts “Date matches!”
ftp.getbinaryfile(xmlfile.downcase)
else
puts “Dates don’t match”
ftp.delete(xmlfile)
end
end
end

Peter B. wrote:

Hi,
I’m having to go to a website every day to pull files for editors at my
company. Unfortunately, the web site isn’t maintained that well–they
keep old files. I have the power to, and I’d like to, delete any old
files that aren’t stamped with today’s date. Can someone help me? I’m
getting Net::FTPPermError errors when I run this.

I’ve modified the real names below to protect the innocent. But, the
filenames over there look like this:

lobbyreg-20071215-160004.xml

My code:
ftp = Net::FTP.open(‘ftpserver.zq.com’)
ftp.login(‘zna’,’&[email protected]’)
ftp.chdir("/data/data/zna")
t = Time.now
t = t.strftime("%Y%m%d")
if Dir.glob(".xml") then
xmlfiles = ftp.list(’
.xml’)
xmlfiles.each do |xmlfile|
if xmlfile.to_s.match(t.to_s) then
puts “Date matches!”
ftp.getbinaryfile(xmlfile.downcase)
else
puts “Dates don’t match”
ftp.delete(xmlfile)
end
end
end

Never mind. I figured it out. For some reason, when files are listed
with ftp, they include not just the filenames, but also the date and the
other stuff you see when you do a “dir.” So, I just had to delete all
that junk and make sure that I was deleting just the filename, not
everything.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs