Solved

Script to download and delete all files

Posted on 2010-11-29
5
1,189 Views
Last Modified: 2013-12-26
I have been working on a script that will log in to a remote FTP directory, download all of the files in the directory, and then delete all of the remote files.

My plan was to use curl for this, but I have found that the file names are not consistent at all, so it's not too easy to assign a matching pattern.

Can anyone give me a hand with a simple script for this?
0
Comment
Question by:djudd77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
JESii earned 500 total points
ID: 34235601
Here is a Ruby script that will do what you want, I think...

You'll need to replace the ftp server name with your site and any directory name with what you want (if any), and then run it as a test.

Once you're satisfied with it, you can simply remove the "#" front the front of the "ftp.delete..." statement and you're good to go.

Mac has Ruby pre-installed so this should work from Terminal.
require 'net/ftp'

ftp = Net::FTP.new('ftp-server-name.com')
  ftp.login
  puts "Logged in..."
  puts "Directory set..."
  files = ftp.chdir('dir/name')
  files = ftp.nlst('*')
  puts "File list obtained... #{files}"
  files.each do |fname|
	puts "Downloading file #{fname}"
  	ftp.getbinaryfile(fname, fname)
	puts "Deleting file #{fname}"
	#ftp.delete(fname)
	end
  ftp.close

Open in new window

0
 
LVL 1

Author Comment

by:djudd77
ID: 34235627
I'm totally not familiar with Ruby. Is there a way to pass the username and password in this script?

I very much appreciate the help.
0
 
LVL 12

Expert Comment

by:JESii
ID: 34236328
Sure... Change line 4 to:
  ftp.login(user=ARGV[0], ARGV[1])
and then call it like so:
  ruby ftp_process.rb username password
0
 
LVL 1

Author Comment

by:djudd77
ID: 34236800
Very awesome, thank you for all your help.
0
 
LVL 12

Expert Comment

by:JESii
ID: 34238338
You're welcome; thank you for the points.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
iCloud Drive was introduced after iOS 8 was launched last year. This drive is Apple’s online storage device that lets users sync their files and access them from all their Apple devices.   There is a lot of data that is not automatically backed up…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question