ScriptSpot

Forum topic · General Scripting

FTP Get Subfolder name

By Rodman · 2014-01-14

Description

Hi,

I have a FTP access (hostname, username, password).

I want to get the name of the unique folder (there is only one here) at the root like so:

ftp://hostname//folderName/

Depends on the username, the folder has a different name, that's why I want to get it to copy some files in this one.
There is no access to write anything at this level.

In Python it's simple with the ftplib class:


from ftplib import FTP
ftp = FTP('hostname')
ftp.login('userName','password')
ftp.retrlines('LIST')
ftp.quit()

It prints out the folder name and some other information.

In dotNet, we have the class WebClient, FtpWebRequest, and FtpWebResponse. I'm confused with all these methods.

All I have so far can chek if the connection has succeeded or not, plus the "StatusDescription".
But it's not at all what I am looking for because I have to know the folder name at the start.


clearlistener()

FTP = "ftp://hostname//folderName/"
userName = "username"
password = "password"

theRemote = FTP + "test.zip"

clsRequest = (dotNetClass "System.Net.FtpWebRequest").Create theRemote
clsRequest.Credentials = dotNetObject "System.Net.NetworkCredential" userName password
clsRequest.Method = "STOR" -- "LIST"

try
(
    response = clsRequest.GetResponse()
    print (response.StatusDescription)
    response.Close()
    response.Dispose()
    print "Connected"	
)
catch
(
    print "Error"
)
Comments (3)

I found the solution

Rodman · 2014-01-14


FTP = "ftp://hostname/"
userName = "username"
password = "password"

clsRequest = (dotNetClass "System.Net.FtpWebRequest").Create FTP
clsRequest.Credentials = dotNetObject "System.Net.NetworkCredential" userName password
clsRequest.Method = "LIST"

try
(
    response = clsRequest.GetResponse()
    reader = dotNetObject (dotNetClass "System.IO.StreamReader") (response.GetResponseStream())
    print (reader.ReadLine())
    reader.Close()

    response.Close()
    response.Dispose()
    print "Connected"   
)
catch
(
    print "Error"
)

barigazy · 2014-01-14

What cause error here and you need to use try-cache expression?

.

Rodman · 2014-01-14

It's a way to know that you couldn't connect to the server.
Maybe there is a better method but I don't know it yet.