How to get a filelist of an ftp server?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
MasterchiefJB
Posts: 76
Joined: Sat Nov 07, 2009 7:43 pm

How to get a filelist of an ftp server?

Post by MasterchiefJB » Sat Dec 05, 2009 5:04 pm

Hi everybody,

I would like to know how I can get a list of the files in a directory on a FTP Server, display this list in a Field and provide the user the ability to select the file and download it.

I know how to connect to the FTP server but I fail in getting the list and processing it so when the User selects the displayed Filename and he hits the downloadbutton the DownloadURL of the displayed file should be processed.

Help would be appreciated,
Masterchief

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: How to get a filelist of an ftp server?

Post by FourthWorld » Sat Dec 05, 2009 5:20 pm

This works well here for getting the list (note the trailing slash, needed to distinguish from a file request:

put url "ftp://user:pass@domain.com/folder/" into fld "MyField"
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Re: How to get a filelist of an ftp server?

Post by Janschenkel » Mon Dec 07, 2009 4:44 pm

Depending on the FTP server, you may have to use libURLSetFTPListCommand first to toggle between "LIST" and "NLST" in order to obtain a readable list.

HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

MasterchiefJB
Posts: 76
Joined: Sat Nov 07, 2009 7:43 pm

Re: How to get a filelist of an ftp server?

Post by MasterchiefJB » Mon Dec 07, 2009 7:47 pm

Thank you. Is working but the only message I get in my Field is: "error invalid host adress"

Can´t find the actuall error because I set up a FTP Server on my localhost with the login data

Login: root
pass: xxxx

Code: Select all

on mouseUp
   libURLSetFTPListCommand "NLST"
   open URL "ftp://root:xxxx@localhost/" -- Open the FTP Connection
  put libURLftpCommand("LIST","ftp://localhost/") into field "ModList" -- List the Mods in the Direcotry
end mouseUp
Do you know why this happens?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: How to get a filelist of an ftp server?

Post by FourthWorld » Mon Dec 07, 2009 8:34 pm

It's extremely rare that you'd use root to log in via FTP. Double-check the settings for your FTP account, and try this to get a detailed description of the transactions with the server:

1. Make a new stack named "FTP Transcript" with a field named "log"
2. Run this in the Message Box:

libURLSetLogField the long id of fld "log" of stack "FTP Transcript"

Now when you do anything with libURL you'll see all of the transactions logged to that field. If there's a login error you should see a 503 error in that transcript.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Re: How to get a filelist of an ftp server?

Post by Janschenkel » Mon Dec 07, 2009 8:46 pm

There's no call to 'open' the URL, just use the get or put command to fetch the information.

Code: Select all

on mouseUp
   libURLSetFTPListCommand "NLST"
   put URL "ftp://root:xxxx@localhost/" into field "ModList" -- List the Mods in the Direcotry
end mouseUp
HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

MasterchiefJB
Posts: 76
Joined: Sat Nov 07, 2009 7:43 pm

Re: How to get a filelist of an ftp server?

Post by MasterchiefJB » Tue Dec 08, 2009 4:20 pm

Thank you Janschenkel

that´s working smart^^

Could you also tell me how I could get the selection in my Scrolling List field and use it to download the selected file via a download button?
I don´t have an idea how to do that.
I know how to specify a file to be downloaded but how can I use the Scrolling List field were the files are listened to download one of them or more?

Thanks,
Masterchief

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Re: How to get a filelist of an ftp server?

Post by Janschenkel » Wed Dec 09, 2009 7:02 am

Use the hilitedLine property of the list field:

Code: Select all

put the hilitedLine of field "ModList" into tHilitedLine
put line tHilitedLine of field "ModList" into tFileName
HTH,

Jan Schenkel
Quartam Reports & PDF Library for LiveCode
www.quartam.com

MasterchiefJB
Posts: 76
Joined: Sat Nov 07, 2009 7:43 pm

Re: How to get a filelist of an ftp server?

Post by MasterchiefJB » Wed Dec 09, 2009 3:18 pm

Thanks. This helps a lot but for the download I need also the FTP Path. I tried to include the Variable tFileName in the Download URL but it doesn´t work that easy. Do you know how I can add the filename I got through the selection to the end of the downloadpath or how to get the downloadpath of the selected File?

Thanks,
Masterchief

MasterchiefJB
Posts: 76
Joined: Sat Nov 07, 2009 7:43 pm

Re: How to get a filelist of an ftp server?

Post by MasterchiefJB » Wed Dec 09, 2009 3:40 pm

Ah it seems I got it working (was quite easy^^)

The code I am unsing now is:

Code: Select all

  -- put the selection of the ModListFile into the Variable tFileToDownload
  put the hilitedLine of field "ModList" into tHilitedLine
  put line tHilitedLine of field "ModList" into tFileToDownload
    -- put the address of the file to download into a variable
    put "ftp://root:xxx@localhost/mods/" into tDownloadLink 
    put tFileToDownload after tDownloadLink
    
    -- put tDownloadLink & tFileToDownload into tDownloadLink
The last line is commented out because it´s working but in case it wouldn´t I already wrote another way I thought it could work;)

Could you please tell me what you think of my solution? I am new to revolution and would like to get some advises if this code works but is buggy in your eyes^^.

PS: Now I am working on Zipping the files to upload and Unzip the files that are downloaded but if I get problems there I will tell you :D

Regards,
Masterchief

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Re: How to get a filelist of an ftp server?

Post by Janschenkel » Thu Dec 10, 2009 7:07 pm

Well, you can either

Code: Select all

put URL tDownloadLink into URL (binfile:" & tLocalPath)
or

Code: Select all

libUrlDownloadToFile tDownloadLink, tLocalPath
The libUrlDownloadToFile command has an optional third parameter, the callbackMessage which will be sent to your control aftet the download is finished. Also check out the UrlStatus function if you wish to provide updates while the download progresses.

HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply