6 Replies - 638 Views - Last Post: 16 July 2012 - 08:57 AM Rate Topic: -----

#1 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Read file in ftp server and execute a fuction

Posted 15 July 2012 - 02:35 AM

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Read file in ftp server and execute a fuction

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,053
  • Joined: 17-July 10

Re: Read file in ftp server and execute a fuction

Posted 15 July 2012 - 05:21 AM

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 


Was This Post Helpful? 0
  • +
  • -

#3 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: Read file in ftp server and execute a fuction

Posted 16 July 2012 - 02:09 AM

View PostIonut, 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()


Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 956
  • View blog
  • Posts: 3,676
  • Joined: 02-July 08

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

Was This Post Helpful? 0
  • +
  • -

#5 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: Read file in ftp server and execute a fuction

Posted 16 July 2012 - 06:35 AM

View Post_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'.
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 956
  • View blog
  • Posts: 3,676
  • Joined: 02-July 08

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

Was This Post Helpful? 1
  • +
  • -

#7 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: Read file in ftp server and execute a fuction

Posted 16 July 2012 - 08:57 AM

View Post_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

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1