Solved

Script to download and delete all files

Posted on 2010-11-29
5
1,188 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Citrix 6.5 / Receiver 12.x / MAC OS 10.x 9 108
app store blank on mac 3 60
cscript to activate Windows and Office? 2 163
c++  placing data into a form and an editbox 5 50
Set up iPhone and iPad email signatures to always send in high-quality HTML with this step-by step guide.
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

710 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