• FTP

    Author
    Topic
    #464688

    Hi
    I’ve had a few attempts at this today without success.
    The following code transfers files successfully but I need to add a command to check if a folder exists before the files are transferred.
    If it’s not, then create one.
    The folder created should represent strRef, so if strRef is 12345 then the path should be “ConsoleFiles12345”
    I think I need to use FileSystemObject but would this need to open a session to the server to check if the folder exists?
    Here’s what I have ( no reference to FileSystemObject) in raw format.
    Can anyone help?

    Select Case List1

    Case “123 Leek”
    strRef = InputBox(“Please Enter A Leek Job Number”, “”)
    If strRef = “” Then
    MsgBox “You Must Enter A Leek Job Number Before Proceeding”, vbInformation, “Reference Error”
    Exit Sub
    End If

    If Len(strRef) > 5 Then
    MsgBox “Too Many Characters” & vbCrLf & _
    “You Can Only Use 5 Characters” & vbCrLf & _
    “Please Enter A Valid 5 Character Number”, vbInformation, “Information”: Exit Sub
    End If

    If Len(strRef) < 5 Then
    MsgBox "Not Enough Characters" & vbCrLf & _
    "You Need To Use 5 Characters" & vbCrLf & _
    "Please Enter A Valid 5 Character Number", vbInformation, "Information": Exit Sub
    End If

    For N = 0 To List2.ListCount – 1

    FTPSource = "L:MmpdfConsoleFiles" & Left(List2.List(N), 5) & ""
    FTPPath = "ConsoleFiles"
    FF = Right(List2.List(N), 7)

    frmFTP1024.FTPFile "SERVER", "PUT", "USER", "PASSWORD", FTPSource & List2.List(N), FTPPath & strRef & FF
    Next N

    Viewing 5 reply threads
    Author
    Replies
    • #1190580

      Hi Dave,

      The Scripting.FileSystemObject object has a FolderExists method, but you’d have to connect to the target server to do that. I can’t help you with that.

      For what it’s worth, here is the syntax (in VB6 / VBA style):

      Code:
      Dim fso As Object
      Set fso = CreateObject("Scripting.FileSystemObject")
      If fso.FolderExists("\serverpathConsoleFiles" & strRef) Then
        ...
      Else
        ...
      End If
    • #1190593

      Hans

      Does this help, I tried the fso statement in the “PUT” section as I think this opens the connection.
      It crashed because strRef isn’t created until later in the code?

      Friend Sub FTPFile(ByVal sFTPServer As String, _
      ByVal sFTPCommand As String, _
      ByVal sFTPUser As String, _
      ByVal sFTPPwd As String, _
      ByVal sFTPSrcFileName As String, _
      ByVal sFTPTgtFileName As String)

      Dim oFS As Scripting.FileSystemObject
      Dim sURL As String

      On Error GoTo FTPFileExit

      Me.HRG True
      msCurrentFile = “”

      Set oFS = New Scripting.FileSystemObject

      sURL = “ftp://&#8221; & sFTPUser & “:” & sFTPPwd & “@” & sFTPServer

      Inet1.Protocol = icFTP
      Inet1.RequestTimeout = 60
      Inet1.RemotePort = 21
      Inet1.AccessType = icDirect
      Inet1.URL = sURL

      Select Case sFTPCommand

      Case “PUT”

      msCurrentFile = sFTPSrcFileName
      If oFS.FileExists(sFTPSrcFileName) = False Then GoTo FTPFileExit
      Inet1.Execute sURL, sFTPCommand & Space(1) & sFTPSrcFileName & ” ” & sFTPTgtFileName

      Case “GET”

      msCurrentFile = sFTPTgtFileName
      If oFS.FileExists(sFTPTgtFileName) = True Then oFS.DeleteFile sFTPTgtFileName, True
      Inet1.Execute sURL, sFTPCommand & Space(1) & sFTPSrcFileName & ” ” & sFTPTgtFileName

      End Select

      Me.WaitForResponse
      Inet1.Execute sURL, “quit”
      Me.WaitForResponse

      FTPFileExit:
      Set oFS = Nothing
      HRG False
      End Sub

      • #1190598

        I don’t think you can use FileSystemObject to check a file or folder on the target location.

    • #1190600

      Seems you’re right !

      I tried:

      Case “PUT”

      msCurrentFile = sFTPSrcFileName
      If oFS.FileExists(sFTPSrcFileName) = False Then GoTo FTPFileExit
      If oFS.FileExists(sFTPTgtFileName, vbDirectory) = False Then MkDir sFTPTgtFileName
      Inet1.Execute sURL, sFTPCommand & Space(1) & sFTPSrcFileName & ” ” & sFTPTgtFileName

      Which doesn’t work 🙁

      • #1190601

        I’m not sure it’s possible at all, but perhaps someone else will have a suggestion…

    • #1190626

      I fired up an FTP program, navigated to a folder, then made a new directory. The FTP client sent DIR to obtain the list of existing files/folders, then sent MKD to make the new directory. If you can find documentation on these commands for your server, I think that would help. I’m most curious what format the DIR information is in. (I turned on WireShark to see for myself, but because I’m using FTP over SSH, it was encrypted…)

    • #1190785

      I have taken a different approach to this.
      Instead of creating a folder via FTP I have changed the load up script to create it instead.
      Each time the user loads the software it checks to see if “L:MMPDFTransfer” exists, if not then create it.
      The transfer sequence of the individual files is good and works perfectly.

      I now need to count the number of files in that folder that begin with a specific number ( dependant what file the user is looking at).
      With the following code, a file count is successful, but it counts all files and not just files that begin with strLeft5.
      Where do I put strLeft5 to count only files it refers to?

      Many thanks for the help.

      Dim fso As New Scripting.FileSystemObject
      Dim fld As Scripting.Folder
      Dim strPath As String
      Dim strTrans5 As String

      strPath = “L:mmpdfTransfer”
      strTrans5 = Left(Me.txtEst, 5)

      If fso.FolderExists(strPath) Then
      Set fld = fso.GetFolder(strPath)
      If fld.Files.Count > 0 Then
      Me.txtTrans.Text = fld.Files.Count

      End If
      End If
      Set fld = Nothing
      Set fso = Nothing

      • #1190787

        Don’t you mean strTrans5 instead of strLeft5? Anyway, you’ll have to loop through the files to count those whose name starts with the specified text:

        Code:
          Dim fso As New Scripting.FileSystemObject
          Dim fld As Scripting.Folder
          Dim fil As Scripting.File
          Dim strPath As String
          Dim strTrans5 As String
          Dim lngCount As Long
         
          strPath = "L:mmpdfTransfer"
          strTrans5 = Left(Me.txtEst, 5)
           
          If fso.FolderExists(strPath) Then
            Set fld = fso.GetFolder(strPath)
            lngCount = 0
            For Each fil In fld.Files
              If fil.Name Like strLeft5 & "*" Then ' or strTrans5 ?
                lngCount = lngCount + 1
              End If
            Next fil
            If lngCount > 0 Then
              Me.txtTrans.Text = lngCount
            End If
          End If
          Set fil = Nothing
          Set fld = Nothing
          Set fso = Nothing
    • #1190788

      I’ve been trying to do this for ages.

      Cheers Hans

      Works great :))

    Viewing 5 reply threads
    Reply To: FTP

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: