none
How to upload file or files using wildcards to FTP server directory using VBScript? RRS feed

  • Question

  • I am trying to make update this VBScript to upload files or files using wildcards like ("*.png" & "*.txt") to remote ftp directory, after running the script and I check the directory, I discovered that the expected file didn't upload.

    Below is the copy of the code:

    Set oShell = CreateObject("Shell.Application")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Dim path As String
    LocalPath = Environ("HOMEPATH") & "\AppData\Roaming\source1\"
    
    'Path to file or folder to upload
    path = LocalPath & "filescopy.txt"
    
    intMaxAttempt = 5
    intCurAttempt = 0
    boolSuccess = False
    
    Do While Not boolSuccess
        intCurAttempt = intCurAttempt + 1
    
        If intCurAttempt > intMaxAttempt Then
            Set objFile = objFSO.OpenTextFile(LocalPath & "log.txt", 8, True) ' 8=append, 2=overwrite
            objFile.WriteLine(path & " did NOT upload successfully on " & Now)
            objFile.Close
    
            Exit Do
        End If
    
        boolSuccess = FTPUpload(path)
    
        If boolSuccess Then
    
            If objFSO.FileExists(path) Then
                objFSO.DeleteFile path
            End If
    
            If objFSO.FolderExists(path) Then
                Set objFolder = objFSO.GetFolder(path)
                For Each strFile In objFolder.Files
                    objFSO.DeleteFile strFile
                Next
            End If
    
            Set objFile = objFSO.OpenTextFile(LocalPath & "log.txt", 8, True) ' 8=append, 2=overwrite
            objFile.WriteLine(path & " uploaded successfully on " & Now)
            objFile.Close
    
            Exit Do
        Else
    
        End If  
    Loop
    
    
    
    
    Function FTPUpload(path)
        FTPUpload = True
    
        On Error Resume Next
    
        'Copy Options: 16 = Yes to All
        Const copyType = 16
    
        'FTP Wait Time in ms
        waitTime = 80000
    
        FTPUser = "User"
        FTPPass = "Pass"
        FTPHost = "host.com"
        FTPDir = "/"
    
        strFTP = "ftp://" & FTPUser & ":" & FTPPass & "@" & FTPHost & FTPDir
        Set objFTP = oShell.NameSpace(strFTP)
    
        'Make new folder on FTP site
        'objFTP.NewFolder "FTP Backup"
    
    
        'Upload single file      
        If objFSO.FileExists(path) Then
    
            Set objFile = objFSO.getFile(path)
            strParent = objFile.ParentFolder
            Set objFolder = oShell.NameSpace(strParent)
    
            Set objItem = objFolder.ParseName(objFile.Name)
    
            Wscript.Echo "Uploading file " & objItem.Name & " to " & strFTP
             objFTP.CopyHere objItem, copyType
    
        End If
    
        'Upload all files in folder
        If objFSO.FolderExists(path) Then
    
            'Code below can be used to upload entire folder
            Set objFolder = oShell.NameSpace(path)
    
            Wscript.Echo "Uploading folder " & path & " to " & strFTP
            objFTP.CopyHere objFolder.Items, copyType
    
        End If
    
    
        If Err.Number <> 0 Then
            Wscript.Echo "Error: " & Err.Description
            FTPUpload = False
        End If
    
        'Wait for upload
        WScript.Sleep waitTime
    End Function

    It didn't give a prescribe error from any line. It just stated that Error: Object Required. Looking deeply into the code when I added option Explicit to the begining of the code. I Prompt
    Variable is Undefined: "oshell"

    And after some showed up:
    Uploading file: filescopy to ftp directory

    When I hit Okay on the uploading image it prompted: Object required again

    Below is the log.txt:
    \Users\WebAdmin_Win7\AppData\Roaming\source1\filescopy.txt did NOT upload successfully on 2/15/2020 9:59:56 PM

    I have been working arounf this for some hours now and I am unbale to fix this myself. I'd Appreciate A solution to this.



    Sunday, February 16, 2020 5:49 AM

All replies

  • The following is wrong. You are mixing VBA and VBScript.

    Set objFile = objFSO.OpenTextFile(homePath + "\AppData\Roaming\source1\log.txt", 8, True) ' 8=append, 2=overwrite

    I highly recommend that, since you are not a programmer of someome who knows scriptin, that you use PowerShell for you task.  I is easier to use and easier to learn.

    You cannot use the shell to make an FTP connection.  Take some time to learn Windows.  YOu have copied code from somewhere and are trying to modify it incorrectly.  The code you copied is also a problem.


    \_(ツ)_/



    • Edited by jrv Sunday, February 16, 2020 6:20 AM
    Sunday, February 16, 2020 6:17 AM
  • The following is wrong. You are mixing VBA and VBScript.

    Set objFile = objFSO.OpenTextFile(homePath + "\AppData\Roaming\source1\log.txt", 8, True) ' 8=append, 2=overwrite

    I highly recommend that, since you are not a programmer of someome who knows scriptin, that you use PowerShell for you task.  I is easier to use and easier to learn.

    You cannot use the shell to make an FTP connection.  Take some time to learn Windows.  YOu have copied code from somewhere and are trying to modify it incorrectly.  The code you copied is also a problem.


    \_(ツ)_/



    Thank you but please I would like to use VBScript to solve this task. Is there any assitance or refrence you can reffer me too?
    Sunday, February 16, 2020 12:07 PM
  • Now I am trying to use VBA Variable path.

    Dim path As String
    LocalPath = Environ("HOMEPATH") & "\AppData\Roaming\source1\"
     
    'Path to file or folder to upload
    path = LocalPath & "filescopy.txt"

    I am getting path not found. Can you help me please?

    I ahve updated the script


    Sunday, February 16, 2020 12:50 PM
  • This is not a VBA forum.  For VBA post in the VBA developer5s forum.  VBA is not script it is compiled VB code,


    \_(ツ)_/


    • Edited by jrv Sunday, February 16, 2020 1:49 PM
    Sunday, February 16, 2020 1:49 PM