How to issue multiple FTP commands with libURLftpCommand?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
davidayresBUSlFpm
Posts: 4
Joined: Thu Jan 24, 2013 1:18 am

How to issue multiple FTP commands with libURLftpCommand?

Post by davidayresBUSlFpm » Fri Sep 02, 2016 5:49 pm

Hello everyone,

I have the following code, which works successfully, but I need to be able to do a bit more than just change the directory on my FTP server. I need to

1) change to the directory I need
2) get a list of files in that directory
3) inspect those files to see if they have a file extension
4) delete any files that have a file extension

Code: Select all

on mouseUp
   
   put "" into field "txtResult"
   
   put libURLftpCommand("cwd /manu/dev/appsrv/tm78/data", "MyFTPServerAddressHere", "userID", "password") & return into field "txtResult"
   
end mouseUp
I've gone through the LiveCode dictionary as well as the user guide, tried to use the URL approach but that give me what I need. Any suggestions on how I can complete the above 4 steps with LiveCode? I've already completed this task with Python quite easily, but I would really like to use LiveCode more often if possible to see if it's worth purchasing the Indy license and so I can make an executable for whatever platform is needed (mostly for Windows).

Kind regards,

David

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: How to issue multiple FTP commands with libURLftpCommand

Post by shaosean » Sat Sep 03, 2016 3:01 am

1) change to the directory I need
2) get a list of files in that directory
You would just need to run the libURLftpCommand twice with the FTP commands you need (CWD and LIST)
3) inspect those files to see if they have a file extension
This would depend on how the list is returned (different servers return different LIST formats)..
4) delete any files that have a file extension
Another libURLftpCommand (DEL)


I haven't used LiveCode since it was called Runtime Revolution, but I remember libFTP would open and close the connection with every libURLftpCommand that was sent, so if this is something that is going to be done a lot, you will take a performance hit..

davidayresBUSlFpm
Posts: 4
Joined: Thu Jan 24, 2013 1:18 am

Re: How to issue multiple FTP commands with libURLftpCommand

Post by davidayresBUSlFpm » Sat Sep 03, 2016 1:35 pm

Hi shaosean,

Thanks for your reply, but I already tried running the libURLftpCommand twice, and it doesn't work. My thoughts are as yours, that the libURLftpCommand opens then closes the connection each time which is why the approach to run it multiple times won't work. Because if you run it once to change directories, then it closes, you're right back to where you started if you run it again, so if I try running the LIST command, or nlst actually (that's the one that works), it tries to list from the root directory, because I haven't changed directories yet...that was the last command I did and it doesn't remember that. It's as if the libURLftpCommand doesn't remember the state of the last thing I did, so when I use it again...well you get the point.

No worries, though, because I was just trying a few things with LiveCode to see if it would be something useful for the things I need to do. I come from years of programming with C# and using Python, but would really like to get into more cross platform development. LiveCode is an awesome tool, quite amazing actually, but I'm nervous about purchasing the Indy license because of things like this where I have to fall back on something like Python to get things done.

Kind regards,

David

wsamples
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 264
Joined: Mon May 18, 2009 4:12 am

Re: How to issue multiple FTP commands with libURLftpCommand

Post by wsamples » Sat Sep 03, 2016 3:01 pm

Which version of LiveCode are you using at the moment?

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

Re: How to issue multiple FTP commands with libURLftpCommand

Post by FourthWorld » Sat Sep 03, 2016 4:30 pm

I haven't used FTP in years because of its security limitations (any protocol that sends passwords in plain text is best limited to use only on local networks), but if memory serves you can get a list of files from an FTP server in one line in LC:

Code: Select all

get url "ftp://user:pass@domain.com/path/to/files/"
if the result is not empty then -- handle connection error:
   answer "Whoops!" && the result
else
   -- do whatever you like with the list of files
end if 
The trick there is that the URL path ends with "/", letting libURL know that you're asking for a list of directory contents rather than the contents of a specific file.

Most FTP commands in LiveCode will automatically handle directory changing, and when writing a file via FTP it'll also automatically handle the creation of any new directories needed to satisfy the specified path.

Python's a great language, but for many tasks I think you'll find some of the conveniences in LiveCode quite nice. :)


Tip: The FTP spec is notorious for allowing a wide range of inconsistencies with how file listings are delivered. For example, date/time stamps may sometimes only include the month and date (if within the previous 12 months), while otherwise using a wide range of allowable date formats. There are other allowances as well, perhaps easy to account for if you're dealing with only one server, but potentially troublesome if you need to write a tool that can work with any FTP server.

If this is a server you control, you can get more consistent file listings using a CGI called via HTTP/HTTPS instead. This will usually also be much faster, and if the filenames are sensitive can also provide security by using HTTPS so everything is encrypted over the wire.

If using LiveCode Server as the CGI, you get get a list of files with creation dates, mod dates, and other useful details with:

Code: Select all

set the directory to tSomeSpecifiedPath
put the detailed files into tFileList
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

davidayresBUSlFpm
Posts: 4
Joined: Thu Jan 24, 2013 1:18 am

Re: How to issue multiple FTP commands with libURLftpCommand

Post by davidayresBUSlFpm » Sat Sep 03, 2016 9:43 pm

Hi wsamples, I'm using version 8 of LiveCode right now.

And thanks for the info Richard, but it seems that I can't use the URL command in LiveCode to my FTP server, which doesn't appear to be the fault of LiveCode. I think it's not set up properly by our network administrator, or actually it might be set up properly just with a lot of security. If I try the FTP URL through a browser I get an error when trying to access the folder I need, but I can access the root of the FTP server just fine. I'm able to access the secure folder with the libURLftpCommand, too, just not able to do multiple things with that command, and as I mentioned the URL code doesn't work at all.

I can't remember the error I get through the browser off hand, it's on my work laptop which I don't have with me at the moment, but I think it had something to do with security rights. Normally I use FileZilla with the credentials to manually do stuff on the FTP server so I'm just trying to automate some things for my department.

Thanks anyways, I appreciate all of the responses.

Cheers,

David

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: How to issue multiple FTP commands with libURLftpCommand

Post by shaosean » Sun Sep 04, 2016 2:34 am

My bad.. you don't need to change the working directory to get the list..

Code: Select all

libURLftpCommand("nlist /the/path/to/the/directory/", whatever, else)

Post Reply