[2005] Getting list of files from a folder (FTP) [Urgent :(]
Hello!
Im making a software for our Counter-Strike Source server.
I'd like to get all map files (.bsp files) from the map folder using the servers ftp adress.
I tried using this code:
VB Code:
Dim folder As New IO.DirectoryInfo(ftpAdress & "/srcds_l/cstrike/maps/")
Dim files As IO.FileInfo() = folder.GetFiles("*.bsp")
But it gives me the error "The given path's format is not supported."
So what should I do? Any suggestions?
Thanks in advance.
Re: [2005] Getting list of files from a folder (FTP)
DirectoryInfo doesn't in fact support ftp type addresses. If you're still on .NET 2003 you'll have either have to shell the windows ftp utility or write your own warpper to the windows calls. I think .NET 2005 has the ftpWebRequest class in the System.Net or System.Web namespace
Re: [2005] Getting list of files from a folder (FTP)
ftpWebRequest...yeah ive seen that when i was havin a look around.
Could you help me on how to use it?
Re: [2005] Getting list of files from a folder (FTP)
Quote:
Could you help me on how to use
Unfortunately no, I' still using VB.NET 2003 and haven't tried the ftpWebreqtest thing, I just read about its existence ..
Re: [2005] Getting list of files from a folder (FTP)
ok...thanks anyways, I'll give it a good look.
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
you need to use WebRequestMethods.Ftp.ListDirectoryDetails to list the contents of an ftp directory.
a quick example i put together ....
VB Code:
[COLOR=Blue]Private Sub[/COLOR] Button1_Click([COLOR=Blue]ByVal[/COLOR] sender [COLOR=Blue]As[/COLOR] System.Object, [COLOR=Blue]ByVal[/COLOR] e [COLOR=Blue]As[/COLOR] System.EventArgs) [COLOR=Blue]Handles[/COLOR] Button1.Click
[COLOR=Blue]Dim[/COLOR] ftp [COLOR=Blue]As[/COLOR] FtpWebRequest = [COLOR=Blue]DirectCast[/COLOR](FtpWebRequest.Create("ftp://mirror.nl/disk2/tucows/"), FtpWebRequest)
[COLOR=Green]'/// ************************
'///NOTE if you need to authenticate with username / password you would add the following 2 lines ...[/COLOR]
[COLOR=Green]'Dim cred As New NetworkCredential("username here", "password here")
'ftp.Credentials = cred
'/// ************************[/COLOR]
ftp.AuthenticationLevel = Security.AuthenticationLevel.MutualAuthRequested
ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails
ftp.Proxy = [COLOR=Blue]Nothing[/COLOR]
[COLOR=Blue]Dim[/COLOR] ftpresp [COLOR=Blue]As[/COLOR] FtpWebResponse = [COLOR=Blue]DirectCast[/COLOR](ftp.GetResponse, FtpWebResponse)
'/// **** these 3 lines are NOT needed, just shows you a bit of the server logon messages you would see ...
Console.WriteLine(ftp.Headers.ToString)
Console.WriteLine(ftpresp.BannerMessage)
Console.WriteLine(ftpresp.Headers.ToString)
'/// ****
[COLOR=Blue]Dim[/COLOR] sreader [COLOR=Blue]As New[/COLOR] IO.StreamReader(ftpresp.GetResponseStream)
[COLOR=Blue]While Not[/COLOR] sreader.Peek = -1
[COLOR=Blue]Dim[/COLOR] ftpList [COLOR=Blue]As String[/COLOR]() = sreader.ReadLine.Split(" ")
[COLOR=Blue]Dim[/COLOR] ftpfile [COLOR=Blue]As String[/COLOR] = ftpList(ftpList.GetUpperBound(0))
Console.WriteLine(ftpfile)
[COLOR=Blue]End While[/COLOR]
ftpresp.Close()
[COLOR=Blue]End Sub[/COLOR]
:)
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
Thank you ALOT mate! You cant imagine how much i needed that code :)
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
Just one more thing:
I had this code you gave me the click event of a button.
I tried clicking the button several times with a small pause between each click and then this error message came popping up:
The remote server returned an error: (500) Syntax error, command unrecognized.
On the line marked in red:
VB Code:
Dim ftp As FtpWebRequest = DirectCast(FtpWebRequest.Create(ftpAdress & "/srcds_l/cstrike/maps/"), FtpWebRequest)
'/// ************************
'///NOTE if you need to authenticate with username / password you would add the following 2 lines ...
'Dim cred As New NetworkCredential("username here", "password here")
'ftp.Credentials = cred
'/// ************************
ftp.AuthenticationLevel = Security.AuthenticationLevel.MutualAuthRequested
ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails
ftp.Proxy = Nothing
[COLOR=Red]Dim ftpresp As FtpWebResponse = DirectCast(ftp.GetResponse, FtpWebResponse)[/COLOR]
'/// **** these 3 lines are NOT needed, just shows you a bit of the server logon messages you would see ...
Console.WriteLine(ftp.Headers.ToString)
Console.WriteLine(ftpresp.BannerMessage)
Console.WriteLine(ftpresp.Headers.ToString)
'/// ****
Dim sreader As New IO.StreamReader(ftpresp.GetResponseStream)
While Not sreader.Peek = -1
Dim ftpList As String() = sreader.ReadLine.Split(" ")
Dim ftpfile As String = ftpList(ftpList.GetUpperBound(0))
Console.WriteLine(ftpfile)
If ftpfile.Contains(".bsp") And Not ftpfile.Contains(".ztmp") Then
lwMaps.Items.Add(ftpfile, 6)
End If
End While
ftpresp.Close()
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
well you should only click one time. but, is the ftp path remote? or local?
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
Its remote.
I have an option in a menu "Server maps", when its clicked it fires this code and lists all the maps, so it can be clicked several times :(
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
make sure your ftp path is correct , it must be ftp://server path.com , NOT ftp://ftp.server path.com
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
hmm I just changed the line:
VB Code:
Dim ftpresp As FtpWebResponse = DirectCast(ftp.GetResponse, FtpWebResponse)
to:
VB Code:
Dim ftpresp As FtpWebResponse
Try
ftpresp = DirectCast(ftp.GetResponse, FtpWebResponse)
Catch ex As Exception
MsgBox(ex.Message)
End Try
And now its working as it should. No error messages at all. :eek:
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
But I'd like to ask you one more thing while im still at it...
Do you know how to erase a file on the ftp server?
Very much appreciated. Thank you.
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
you would use WebRequestMethods.Ftp.DeleteFile when doing the FtpWebrequest
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
yeah i saw that there was something in there that looked good. But how do I use it?
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
specify the file you want in the request, eg
FtpWebRequest.Create(ftpAdress & "/srcds_l/cstrike/maps/somefile.exe")
the method ...
ftp.Method = WebRequestMethods.Ftp.DeleteFile
just use the same routine as getting the list.
but ...
you must have permission to delete the file.
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
aaah I understand. Yeah Ive got permission. Thank you alot youve made my day :thumb:
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
Still getting those error messages i mentioned in previous posts...It looks to me like its the server being a b*tch..
Re: [2005] Getting list of files from a folder (FTP) [Urgent :(]
I know this is an old thread, but I just ran across this while trying to do something along the same lines (I needed to make an app that would check this ftp site every day and make sure a certain file was there (because it gets automatically ftp'd)).
When looping through the contents of the FTP site (and using this code), it would throw an error after returning about 20 items in the folder, saying 550: File not Found.
If you cange:
VB Code:
While Not sreader.Peek = -1
Dim ftpList As String() = sreader.ReadLine.Split(" ")
Dim ftpfile As String = ftpList(ftpList.GetUpperBound(0))
Console.WriteLine(ftpfile)
End While
to:
VB Code:
Do Until sreader.EndOfStream
ftpList = sreader.ReadLine.Split(" ")
ftpfile = ftpList(ftpList.GetUpperBound(0))
Console.WriteLine(ftpfile)
Loop
Then it returns all items, atleast for me.
Just posting this, incase someone else comes across this thread and has the same problem I did. Pretty simple fix, actually.