Deleting files with Net/ftp

P

Peter Bailey

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','&n@c0ks')
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
 
P

Peter Bailey

Peter said:
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','&n@c0ks')
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top