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:
- Check an FTP folder
- 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