Hey i would like to know how i can read a file in an ftp server and if a string that i want is available in the file that i just read to execute a function.
Basically lets say that i have a file named "hello.txt"
In that "hello.txt" file there are strings like -
frwe234
erer2345
serwetgt3242
fgerg325423
gwer234ef3
rt4w3534
So i need to know how i can read this file which is in the server and if for example the string - "erer2345" is there in that file i need the code to execute a function.
How can i get this done?
Read file in ftp server and execute a fuction
Page 1 of 16 Replies - 638 Views - Last Post: 16 July 2012 - 08:57 AM
Replies To: Read file in ftp server and execute a fuction
#3
Re: Read file in ftp server and execute a fuction
Posted 16 July 2012 - 02:09 AM
Ionut, on 15 July 2012 - 05:21 AM, said:
Here you can find a method for reading the file. If you want to check if a string is part of another string, you can use Contains method.
dim str as String if str.Contains(someValue) then 'Execute function end if
Could you tell me what seems to be wrong here
Dim request As FtpWebRequest = DirectCast(WebRequest.Create("ftp://bluezap.com/public_html/about.html"), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile
request.Credentials = New NetworkCredential("", "")
Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
Dim responseStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(responseStream)
If reader.ReadLine.Contains("attributes") Then
'Execute function
End If
Console.WriteLine(reader.ReadToEnd())
reader.Close()
response.Close()
#4
Re: Read file in ftp server and execute a fuction
Posted 16 July 2012 - 06:13 AM
You don't have a loop for the reader.
While Not reader.EndOfStream
If reader.ReadLine.Contains("string your looking for") Then
'Execute function
End If
End While
This post has been edited by _HAWK_: 16 July 2012 - 06:14 AM
#5
Re: Read file in ftp server and execute a fuction
Posted 16 July 2012 - 06:35 AM
_HAWK_, on 16 July 2012 - 06:13 AM, said:
You don't have a loop for the reader.
While Not reader.EndOfStream
If reader.ReadLine.Contains("string your looking for") Then
'Execute function
End If
End While
I tried this before as well but then i get an error
While Not reader.EndOfStream
Error - Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.
#6
Re: Read file in ftp server and execute a fuction
Posted 16 July 2012 - 08:42 AM
I like to use the Using block, the try catch should be ysed for this type of work.
Try
Using responseStream As Stream = response.GetResponseStream()
Using reader As New StreamReader(responseStream)
While Not reader.EndOfStream
If reader.ReadLine.Contains("string your looking for") Then
'Execute function
End If
End Using 'closes the stream
End Using 'closes the responseStream
End While
Catch ex As Exception
Debug.Writeline(ex.ToString)
End Try
This post has been edited by _HAWK_: 16 July 2012 - 09:45 PM
#7
Re: Read file in ftp server and execute a fuction
Posted 16 July 2012 - 08:57 AM
_HAWK_, on 16 July 2012 - 08:42 AM, said:
I like to use the Using block, the try catch should be ysed for this type of work.
Try
Using responseStream As Stream = response.GetResponseStream()
Using reader As New StreamReader(responseStream)
While Not reader.EndOfStream
If reader.ReadLine.Contains("string your looking for") Then
'Execute function
End If
End Using 'closes the stream
End Using 'closes the responseStream
End While
Catch ex As Exception
Debug.Writeline(ex.ToString)
End Try
Try
Using responseStream As Stream = response.GetResponseStream()
Using reader As New StreamReader(responseStream)
While Not reader.EndOfStream
If reader.ReadLine.Contains("string your looking for") Then
'Execute function
End If
End Using 'closes the stream
End Using 'closes the responseStream
End While
Catch ex As Exception
Debug.Writeline(ex.ToString)
End Try
That worked perfectly but i still don't understand why the earlier code did not work?
And i use the code -
Dim b As New UriBuilder()
b.Host = "bluezap.vacau.com"
b.UserName = ""
b.Password = ""
b.Port = 21
b.Path = "/public_html/" & "Example" & ".txt"
b.Scheme = Uri.UriSchemeFtp
Dim g As Uri = b.Uri
Dim c As System.Net.FtpWebRequest = DirectCast(System.Net.FtpWebRequest.Create(g), System.Net.FtpWebRequest)
c.Method = System.Net.WebRequestMethods.Ftp.AppendFile
Dim h As System.IO.Stream = (c.GetRequestStream)
Dim SW As New System.IO.StreamWriter(h)
Dim contents = "Hello World"
SW.WriteLine(contents)
SW.Close()
h.Close()
to create a file on a server and define what text i want that file to contain for as seen example above "Hello World"
But is there a way of writing many strings into a file which is already there in a server?
This post has been edited by Bluezap: 16 July 2012 - 08:58 AM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|