How to create Subdiretcory on the FTP server?

  • 12 years ago

    i want to create subdirectory on the server.But unable to create there.

    ---------------------------------------------------------------------------------------------------------------------------------------------------- 

      Shared Sub main2(ByVal zipdir, ByVal systemname)

            'FTP creadentials'
            Dim servertemp As String = "ftp://hydhtc25116"
            Dim username As String = "hydhtc25116\root"
            Dim password As String = "infy@123"
            Dim host As String = servertemp

                'Renaming the zip file as the current system date'
            Dim filename As String
            filename = zipdir
            Dim New1 As String
            Dim month1 As String
            Dim year1 As String
            Dim day1 As String
            day1 = DatePart("d", DateTime.Now)
            month1 = DatePart("m", DateTime.Now)
            year1 = DatePart("yyyy", DateTime.Now)
            New1 = systemname + "_" + day1 + "-" + month1 + "-" + year1

                'Declaring the FTP zip filename as the current system Date'
            Dim remoteFile As String = "/" & New1 & ".zip"

                '1. Create a request: must be in ftp://hostname format'
               Dim URI As String = host & remoteFile
                Dim ftp As Net.FtpWebRequest = CType(Net.FtpWebRequest.Create(URI), Net.FtpWebRequest)
          

                '2. Set credentials

                ftp.Credentials = New System.Net.NetworkCredential(username, password)

                '3. Settings and action

                ftp.KeepAlive = False

                '4. we want a binary transfer, not textual data

                ftp.UseBinary = True

                '5. Define the action required (in this case, download a file)

                ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile

                '6. Reading the content of the zip file'

                Dim bFile() As Byte = System.IO.File.ReadAllBytes(filename & "/" & New1 & ".zip")
                Dim clsStream As System.IO.Stream = ftp.GetRequestStream()

                'Writing the content of the file'
                clsStream.Write(bFile, 0, bFile.Length)
                clsStream.Close()
                clsStream.Dispose()
                clsStream.Flush()

        End Sub

     

    ---------------------------------------------------------------------------------------------------------------------------------------------

     

    here is the logic of directory which i want to create on the server..

     

    ------------------------------------------------------------------------------------------------------------------------------------------------

    Dim str As String = "C:\ProgramFiles\Ext\ABC\Stv\text.txt"
            Dim newstr As String = str.Substring(0, 1)
            Console.WriteLine(newstr)
            Dim new1 As String = str.Substring(3, str.LastIndexOf("\") - 2)
            Console.WriteLine(new1)
         


            If Not Directory.Exists("C:\C\" & new1) Then
                Directory.CreateDirectory("C:\C\" & new1)
            End If

     ----------------------------------------------------------------------------------------------------

     

    plz help me out.. 

  • 12 years ago

    Great thanks for inspiration. I am going to make some really new conception of ftp (designed for programmers) now. I was angry with some vices in SmartFTP, and also it is stopped as freeware now, so I just don't want to mess up with old version of it around and around.

    Here I created something that could help. Anyway I am continuing to create this "Ftp" Class to some easy to use and performance maximale class, which will be then places in cConnection in support. Here is solvation of ur problem and some stuff around in work state.

     

    Class FTP
    #Region "Enums"
      Public Enum FtpTransferType As Integer
        Binary = -1
        ASCII = 0
      End Enum
      Public Enum FtpPORTsender As Integer
        Server = -1
        Client = 0
      End Enum
      Public Enum FtpConnectionType
        Active = -1
        Passive = 0
      End Enum
    #End Region
    
      Private Credentials As System.Net.NetworkCredential
      Public BUF_SIZE%
      Public Host$
      Public TransferType As FTP.FtpTransferType
      Public SSL As Boolean
      Private UsePassive As Boolean
      Public Property Mode() As FTP.FtpConnectionType
        Get
          Return CInt(Not Me.UsePassive)
        End Get
        Set(ByVal value As FTP.FtpConnectionType)
          Me.UsePassive = Not CBool(value)
        End Set
      End Property
      ''' Synonym to mode
      Public Property PortSender() As FTP.FtpPORTsender
        Get
          Return CInt(Me.UsePassive)
        End Get
        Set(ByVal value As FTP.FtpPORTsender)
          Me.UsePassive = CBool(value)
        End Set
      End Property
    
      Shared Function RemoveEndSlash$(ByVal text$)
        If text.EndsWith("\") Or text.EndsWith("/") Then
          Return Strings.Left(text, text.Length - 1)
        Else
          Return text
        End If
      End Function
      Public Function GetUri(ByVal directory$, ByVal file$) As System.Uri
        If Strings.Len(directory) = 0 Then
          Return New System.Uri(Me.Host & "/" & file)
        ElseIf Strings.Len(file) = Nothing Then
          Return New System.Uri(Me.Host & "/" & directory)
        Else
          Return New System.Uri(Me.Host & "/" & FTP.RemoveEndSlash(directory) & "/" & file)
        End If
      End Function
      Public Function GetLastName$(ByVal url As System.Uri)
        Dim path$ = url.AbsolutePath
        Dim ix% = Strings.InStrRev(path, "/")
        If ix = 0 Then
          Return path
        Else
          Return Strings.Right(path, path.Length - ix)
        End If
      End Function
    
      Public Function SingleUpload(ByVal path$, ByVal opt_file$, Optional ByVal targetFileName$ = Nothing) As System.Net.FtpWebResponse
        If targetFileName = Nothing Then targetFileName = IO.Path.GetFileName(opt_file)
        Dim src As System.IO.Stream = IO.File.OpenRead(opt_file)
    
        Dim request As System.Net.FtpWebRequest
        request = System.Net.FtpWebRequest.Create(Me.GetUri(path, targetFileName))
        request.EnableSsl = Me.SSL
        request.Credentials = Me.Credentials
        request.UseBinary = CBool(Me.TransferType)
        request.KeepAlive = False
        request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
        Dim trg As System.IO.Stream = request.GetRequestStream
        Dim BU% = Me.BUF_SIZE - 1
        Dim buffer(BU) As Byte, bl% = BU + 1
        While src.Length - src.Position > 0
          Array.Clear(buffer, 0, bl)
          bl = src.Length - src.Position
          If bl >= BU Then
            bl = BU
            src.Read(buffer, 0, bl)
          Else
            ReDim buffer(bl)
            src.Read(buffer, 0, bl)
          End If
          trg.Write(buffer, 0, bl)
        End While
        trg.Close()
        trg.Flush()
        trg.Dispose()
        src.Close()
        src.Dispose()
        Return request.GetResponse
      End Function
      Public Function SingleRename(ByVal path$, ByVal opt_file$, ByVal newFileName$) As System.Net.FtpWebResponse
        Dim request As System.Net.FtpWebRequest
        request = System.Net.FtpWebRequest.Create(Me.GetUri(path, opt_file))
        request.EnableSsl = Me.SSL
        request.Credentials = Me.Credentials
        request.KeepAlive = False
        request.RenameTo = newFileName
        request.Method = System.Net.WebRequestMethods.Ftp.Rename
        Return request.GetResponse
      End Function
      Public Function SingleMKDIR(ByVal path$, Optional ByVal dirName$ = Nothing) As System.Net.FtpWebResponse
        Dim request As System.Net.FtpWebRequest
        request = System.Net.FtpWebRequest.Create(Me.GetUri(path, dirName))
        request.EnableSsl = Me.SSL
        request.Credentials = Me.Credentials
        request.KeepAlive = False
        request.RenameTo = Me.GetLastName(request.RequestUri)
        request.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory
        Return request.GetResponse
      End Function
    
    
      Sub New(ByVal server$, ByVal user$, ByVal password$)
        Me.Credentials = New System.Net.NetworkCredential(user, password)
        Me.Host = FTP.RemoveEndSlash(server)
        Me.TransferType = FTP.FtpTransferType.Binary
        Me.SSL = False
        Me.BUF_SIZE = 64
      End Sub
    End Class
  • 12 years ago

    anyway. u'd rather removed your credentials from ur pasted code :)

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.” - Antoine de Saint Exupéry