Click here to Skip to main content
14,575,451 members
Rate this:
Please Sign up or sign in to vote.
I am trying to make a vbscript to upload files to remote ftp directory, after running the script and I check the directory, I discovered that the expected file didn't upload.

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.

What I have tried:

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


When I used this code, I get path not found.
Posted
Updated 16-Feb-20 11:45am
v6
Comments
Richard MacCutchan 15-Feb-20 11:00am
   
You need to add some debug code to your script to find out what happens when you run it. People here cannot guess what is going on.
cHl Security 15-Feb-20 17:56pm
   
I get the error:
Winscript Host"

Error: Object Required
Richard MacCutchan 16-Feb-20 3:22am
   
How many times do you need to see that error before you do something about it? It is clearly telling you that CreateObject("Shell.Application") is failing, so you need to investigate why.
cHl Security 16-Feb-20 0:29am
   
I have updated the question. Kindy check please
Rate this:
Please Sign up or sign in to vote.

Solution 1

Not an answer to your problem, but something to help you find out what the actual problem is. Your code has the wonderful line On Error Resume Next which suppresses the script from stopping at the point of failure; and allows you to do error handling for the error, by utilizing the properties of Err.

Your code however is not utilizing Err and therefore you are not learning what the problem is. I would be tempted to comment out On Error Resume Next and then try the program out to see if and what any errors are.

References:
vbscript - What does the "On Error Resume Next" statement do? - Stack Overflow[^]
On Error Statement - Visual Basic | Microsoft Docs[^]
   
Comments
cHl Security 15-Feb-20 17:56pm
   
I get the error:
Winscript Host"

Error: Object Required
MadMyche 15-Feb-20 18:09pm
   
On what line? Did you do any troubleshooting of why this happens?
cHl Security 16-Feb-20 0:02am
   
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 got this
https://prnt.sc/r2ylgi

And after some time this showed up again:
https://prnt.sc/r2ym10

When I hit Okay on the uploading image I got this prompt again:
https://prnt.sc/r2ymdp
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
It didn't give a prescribe error from any line.

First picture says Line 7 and running the code with debugger would have go strait to the error.
From your code, it is:
Set oShell = CreateObject("Shell.Application")


Quote:
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Comments
cHl Security 16-Feb-20 8:03am
   
I have updated the code after debugging, Now I get path not found. Please help me
Richard MacCutchan 16-Feb-20 9:23am
   
Where do you get "path not found", what path is it referring to? Please make an effort to provide proper information; no one here can see your screen or read your mind.
Patrice T 16-Feb-20 12:53pm
   
As already said, the debugger show you where error happen (the line number)
Rate this:
Please Sign up or sign in to vote.

Solution 3

It showed nothing. Id appreciate any form of diret assitance from you.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100