FTP Downloading Script: Ready to Use

Hi All,
The programmers and people on this forum have been helpful with this code, so I thought I would share it because it has helped me in my tasks. It also has helped me understand accessing FTP sites and such.

I have needed something to download files off the ftp because our internet is slow and we have a Voice Over FTP site where people record and save. If an editor needs to download 40MB it might take over an hour. So, I made this to check the FTP every morning and then download anything new on the ftp site. I know there are easier ways, but I thought I would share. Thanks again.

Anywhere “CHANGE” is written is where you will need to customize the data.

FTP Compare and Download
This Script will:

  1. Check an FTP folder
  2. Compare this with what has been already downloaded and only download what is new on the FTP

I schedule mine in iCal to run everyday. I could use Cron but don’t really understand it.


--Check to see if the initial file exists
--Add removing spaces from the files on the FTP site
--set an try catch and if it cannot download the file, send a message and continue to the next in the list.

--CHANGE
set lPath to "/Users/boyd/Desktop/" --USE YOUR OWN LOCATION
set PPath to POSIX file lPath


--This section checks to see if there is an ftpOld.txt file to compare with if there is, it will skip and move forward.  If not, it will write a new blank one to your chosen location
--if the file exists, run the script to download the folder on the ftp site
tell application "Finder"
	--CHANGE
	set filePath to POSIX file "/Users/boyd/ftpOld.txt" --USE YOUR OWN LOATION
	log filePath
	if exists filePath then
		set msg to true
	else
		set msg to false
	end if
end tell
if msg = false then
	try
		set fileRef to open for access filePath with write permission
		set newText to ""
		write newText to fileRef
		close access fileRef
	on error
		try
			close access file filePath
		end try
	end try
end if

--CHANGE
--This Section is your FTP Data
--This section also reads the FTP site and downloads a list of files and folders to the ftpNew.txt
set myUser to "USER"
set myPass to "Password"
set ftpURL to "ftp://yourFTP.com"
set ftpURLClean to "yourFTP.com"
--This following command will send the file to your Users root drive, so for me, that would be the same as my lPath
set ftpCommand to "curl " & quoted form of ftpURL & " --user " & quoted form of (myUser & ":" & myPass) & " --list-only > ~/ftpNew.txt"
do shell script ftpCommand

--Should put a log here

set theOutput to paragraphs of (do shell script "sort ~/ftpOld.txt ~/ftpNew.txt | uniq -u")
log theOutput

set n to 1

if the (count of theOutput) = 0 then
	display dialog "There are no files to be downloaded"
	return
end if

repeat while n ≤ (count theOutput)
	--repeat while n ≤ 3
	
	set thisItem to item n of theOutput
	set thisItemPath to (thisItem's POSIX path's text)'s quoted form
	
	log thisItem
	log thisItemPath
	
	--I have had trouble here with UNIX definitions of spaces in filenames, so I just put a Text Item Delimiter in to remove those spaces
	--Removing spaces and adding a "_"
	
	set previousDelimiter to AppleScript's text item delimiters
	set potentialName to thisItem as text
	set legalName to {}
	set illegalCharacters to {" ", "'"} --Whatever you want to eliminate.
	set legalcharacter to {"_"}
	--Now iterate through the characters checking them.
	repeat with thisCharacter in the characters of potentialName
		set thisCharacter to thisCharacter as text
		if thisCharacter is not in illegalCharacters then
			set the end of legalName to thisCharacter
		else if thisCharacter is in illegalCharacters then
			set the end of legalName to legalcharacter
		end if
		
	end repeat
	
	
	
	--Restore the current TIDs. 
	set AppleScript's text item delimiters to previousDelimiter
	log legalName
	set finalName to legalName as text
	log "FinalName is " & finalName
	
	
	if thisItem ≠ finalName then
		--Renames the files with Spaces in them
		
		set ftpURL1 to "ftp://" & myUser & ":" & myPass & "@" & ftpURLClean
		set theOriginal to "RNFR HEC/" & thisItem
		log theOriginal
		set theRename to "RNTO HEC/" & finalName
		log theRename
		
		set ftpCommand to "curl -Q " & quoted form of theOriginal & " -Q " & quoted form of theRename & " " & quoted form of ftpURL1
		log ftpCommand
		try
			do shell script ftpCommand
		on error
			display dialog "Unable to rename " & thisItem & " to " & finalName & "." buttons {"Continue"} with icon caution with title "Error"
		end try
	end if
	
	
	--Download Script
	--CHANGE this is where you will need to put your Download folder
	set DLLocation to "/Volumes/Media_Drive_#3/VO_Files/" --Change this to your settings
	set ftpURL2 to ftpURL & finalName
	set ftpCommand to "curl " & quoted form of ftpURL & finalName & " --user " & quoted form of (myUser & ":" & myPass) & " -o " & DLLocation & finalName
	
	
	try
		do shell script ftpCommand
	on error
		display dialog "Unable to download file:  " & finalName & "." buttons {"Continue"} with icon caution with title "Error"
		set theTry to false
	end try
	log theTry
	if theTry ≠ false then
		
		--reWrite the oldFTP Data
		
		tell application "Finder"
			set filePath to POSIX file "/Users/boyd/ftpOld.txt"
			log filePath
			
		end tell
		try
			set fileRef to open for access filePath with write permission
			set newText to return & finalName
			write newText to fileRef starting at eof
			close access fileRef
		on error
			try
				close access file filePath
			end try
		end try
		log "The file " & finalName & " has been downloaded and written to the file."
		
		
	end if
	
	
	set n to n + 1
	
	
end repeat

Nice. I’d like to move it to Code Exchange (which is what that topic is for). OK?

Hi Adam,
Sure, I did not realize that is where is goes. Thanks and sorry about that.

Boyd

I want to do this, except I just want the Applescript to tell me whether there are new files or not. I am still cutting out everything I don’t need… I also do not want the script to do anything to the files on the FTP, just read their names. Actually if I could download any new files (essentially SYNC the folder I want to check) then that would be great, but I don’t want to be renaming the files and making directories on the FTP with the script and stuff. It’s a client’s FTP.

When I try implementing this script, I always get the “There are not files to be downloaded” message and the ftpOld.txt is always empty. Can anyone help me out or offer a simpler way of doing what I want? I know AppleScript pretty well, but I don’t know shell script at all.

EDIT:
This may be part of my issue. The FTP folder I want to check is ALL subdirectories.

From the cUrl manual:

This is more relevant to all other FTP request on java. This code is very familiar with the normal developers. This type of post is very beneficial .

I see a problem with this script. Since the password is defined in the script itself, some creepy guy in a trenchcoat could look at the script and figure out your password. This could be solved by either saving it as a run-only script or giving a password prompt. :wink:

Seems like a security misinterpretation to me. People like to save files secured etc… while the biggest security leak is that someone can access your machine and/or the ftp protocol is clear and not encrypted (SFTP). This last means that the username and password is send clear over the network; a ‘man in the middle attack’ is much easier to resolve someone’s password than hacking into someone’s computer.