Reputation: 69
I currently use Insert URL in order to download certain files from an external FTP location, which I have managed to get working perfectly using the following curl options
--user " & $usr & ":" & $pw & " -o Stock-Summary.csv"
This works as hoped and downloads the file from the FTP, however, the company who provides us with these files has "moved the goalposts" so to speak.
For whatever reason they are now prefixing and suffixing random numbers to said file, so I need to tweak this to somehow only get the latest version of this file which now looks something like this -
7818023-Stock_Summary-035831
Any advice or different approaches would be greatly appreciated as I am simply a novice trying to help out a family friend.
Upvotes: 0
Views: 378
Reputation: 71
Check out the previous answer, Retrieving the last modified file in a directory over FTP using a bash script with curl.
In particular, this line of Bash,
curl --user <<USERNAME>>:<<PASSWORD>> ftp://<<SERVERURL>> 2>/dev/null | grep "Stock_Summary" | awk -F\ '{print $9}' | sort -n -t- -k3,4 -k1,2 -k2,2 | tail -1
will return the name of the last modified file in the directory that matches the given grep pattern. Then you can take the result of that call and feed the file name (with the extra identifiers) into your existing AppleScript.
set theFileName to do shell script "<<SHELL LINE ABOVE>>"
You may have to play around with the file pattern for grep to get something that will work for you.
Then you'd go back to your existing code and make it something like,
--user " & $usr & ":" & $pw & " -o " & theFileName
NB: remove and fill in everything between and including the << and >>.
In AppleScript this might look like:
on GetFileNameToPull(server, username, password, filePattern)
set theScriptToRun to "curl --user " & username & ":" & password & " ftp://" & server & " 2>/dev/null | grep '" & filePattern & "' | awk -F\\ '{print $9}' | sort -n -t- -k3,4 -k1,2 -k2,2 | tail -1"
set theFileName to do shell script theScriptToRun
return theFileName
end GetFileNameToPull
on GetFileNameFromFTPServer(server, username, password, remoteFileName, localFileName)
set theScriptToRun to "curl --silent --show-error --user " & username & ":" & password & " ftp://" & server & "/" & remoteFileName & " -o " & localFileName
do shell script theScriptToRun
end GetFileNameFromFTPServer
on run
set username to "USERNAME"
set thePassword to "PASSWORD"
set server to "SERVERNAME"
set pathToDownload to "/path/to/download/to"
set theRemoteFileName to GetFileNameToPull(server, username, thePassword, "Stock_Summary")
set theLocalFileName to pathToDownload & "/" & theRemoteFileName
GetFileNameFromFTPServer(server, username, thePassword, theRemoteFileName, theLocalFileName)
end run
Upvotes: 2
Reputation: 96
If what you want is to extract "Stock_Summary" from the new file identifier, then this will do that:
on StripNumbers(FileLong)
set x to offset of "-" in FileLong
set temp to characters (x + 1) thru -1 of FileLong as string
set x to offset of "-" in temp
set temp to characters 1 thru (x - 1) of temp as string
return temp
end StripNumbers
on run
set FileLong to "7818023-Stock_Summary-035831"
set FileShort to my StripNumbers(FileLong)
end run
Then you can add the extension and run your FTP script.
Upvotes: 1