Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Script to download and delete all files

Posted on 2010-11-29
5
Medium Priority
?
1,191 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 2000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…

688 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