Question

Locked

How to know copy file to FTP Server done successfully or failed??? Help!

By poppet_sundae ·
Well, I'm using VB Script to automatically transfer file to FTP server and make a backup at local PC. The thing is no matter the file done the copy successfully or failed, the message always said it transfer successfully. There must be something wrong in the script, but I'm just not figure it out. Can anyone help me have a look?


Function UploadAllFiles(ByVal sFromLocalFolder, ByVal sArchiveLocalFolder, ByVal sFtpFolder, ByRef nNoOfFiles, ByRef nNoIfFailed)
nNoOfFiles = 0
nNoOfFailed = 0
Set f = fso.GetFolder(sFromLocalFolder)

Set fc = f.Files
For Each f1 in fc
If Right(f1.name,4) <> ".ftp" AND _
Right(f1.name,4) <> ".vbs" AND _
Right(f1.name,4) <> ".bat" And _
Right(f1.name,4) <> ".log" Then

sFilename = f1.name

If fso.FileExists(sFtpFolder &"\"& sFilename) Then
fso.DeleteFile(sFtpFolder &"\"& sFilename)
End If
If CopyOneFiletoFtp(sFromLocalFolder,sFtpFolder, sFilename) Then
nNoOfFiles = nNoOfFiles + 1
MoveOneLocalFile sFromLocalFolder,sArchiveLocalFolder,sFilename
Call WriteLog(">>>>>" & sFilename & " Transfered O.K.")
Else
nNoOfFailed = nNoOfFailed + 1
Call WriteLog(">>>>>" & sFilename & " Transfer Failed")
End If
End If
Next

End Function

Function CopyOneFiletoFtp(sLocalFolder,sFtpFolder,sFileName)
Dim fso, WshShell
Dim Counter

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set ftpFile = fso.OpenTextFile("uploadfile.ftp", ForWriting, true)

ftpFile.WriteLine(sFTP)
ftpFile.WriteLine("User " & sUser)
ftpFile.WriteLine(sPass)

' Disable the Prompt Question
ftpFile.WriteLine("prompt" )

' Goto Plant Folder
ftpFile.WriteLine("cd " & sFTPFolder)

' Copy all the files
ftpFile.WriteLine("put " & sLocalFolder & "\" & sFileName)
ftpFile.WriteLine("bye")
ftpFile.Close

Set CmdFile = fso.OpenTextFile("upload.bat", ForWriting, true)
''cmdFile.WriteLine("cd " & sToFolder)
CmdFile.WriteLine("ftp -n -s:uploadFile.ftp" )

Counter = 0
Do While Counter < 5
Counter = Counter + 1
Call Waitfor(1)
Loop

CmdFile.Close
Set CmdFile = Nothing
Set ftpFile = Nothing
nError = WshShell.Run("upload.bat", 2, true)

If nError = 0 Then
CopyOneFiletoFtp = True
Else
CopyOneFiletoFtp = False
End If

If fso.FileExists("upload.bat") Then
fso.DeleteFile("upload.bat")
End If
If fso.FileExists("uploadfile.ftp") Then
fso.DeleteFile("uploadfile.ftp")
End If
End Function


'Move One local file from Fromfolder to Tofolder
Function MoveOneLocalFile(sFromFolder,sToFolder,sFileName)
Dim fso, WshShell
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

If fso.FileExists(sToFolder & "\" & sFilename) Then
fso.DeleteFile(sToFolder & "\" & sFilename)
End If

fso.MoveFile sFromFolder & "\" & sFilename , sToFolder & "\" & sFilename
End Function

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
Thread display: Collapse - | Expand +

All Answers

Collapse -

Success and failure in your case

by Tony Hopkinson In reply to How to know copy file to ...

is that the command file executed successfully
not that the commands within it did.
If an FTP command fails, it gives you an error code, you need to exit out of the script with it or some other non-zero value.

Collapse -

I thought I've already check the execution result on command file

by poppet_sundae In reply to Success and failure in yo ...

Hello Tony,

Yes, I got what you mean, and I really do check the command file, not the commands within it. Please see the code below:

nError = WshShell.Run("upload.bat", 2, true)
If nError = 0 Then
CopyOneFiletoFtp = True
Else
CopyOneFiletoFtp = False
End If

I check the command file "upload.bat", if the return code is zero, then set CopyOneFiletoFtp = True, and if it's the non-zero value, set the CopyOneFiletoFtp = False.

And I disabled the network during the sript testing, in this case, nError supposed to be a non-zero value, and CopyOneFiletoFtp supposed to be False, then it shall write the error message of file transferred failed. But thing is the CopyOneFiletoFtp still equal to Ture, which cause the script write the file transferred successfully in the log.

It seems no matter the network works or the file do transfer to the Ftp server, the command file always return zero, which means non-error. So, what shall I do to catch the error if file not transferred to Ftp server???

By the way, the content in "upload.bat" is like below:
ftp -n -s:uploadFile.ftp

Collapse -

Sorry I wasn't clear

by Tony Hopkinson In reply to I thought I've already ch ...

Windows scripting isn't my forte.

But it's the exit code of FTP.exe you need to echo back to your code, what you seem to be getting, is that of the command file with the FTP command in it.

Back to Networks Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums