Solved

Script to download and delete all files

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best sites for Apple Games 6 123
AWK: Pytthagoras bp script Part deux 22 43
Replacing multiple embedded commas in a CSV file with sed 10 25
Understanding ping command in Ubuntu 5 30
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
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.

749 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