How to Easily Use FTP in WSH - Extending the Chilkat FTP ActiveX control
(Page 3 of 4 )
As you begin using the Chilkat FTP control, you may find that you wish to take your scripts much farther than simple uploads and downloads. I want to take a few minutes and examine some other coding practices that you may find useful.
One of these is checking for the existence of a file on the remote server. Perhaps you want to add some error-handling, or maybe you want to download an updated file only if it exists. In either case, checking for the existence of a file could prove useful.
Function FileExists(rDir, rFile)
objFtp.ChangeRemoteDir(rDir)
strTemp = objFtp.Listpattern
objFtp.ListPattern = rFile
If objFtp.GetSize(0) = -1 Then
FileExists = False
Else
FileExists = True
End If
objFtp.ListPattern = strTemp
End Function
This simple function makes use of the GetSize method to check whether a file exists. It returns the file’s size in bytes if found and returns –1 if not. So a simple If statement allows us to create a function that will return a Boolean value based on whether or not a file exists.
Notice that I’ve changed the ListPattern value so that it only lists the file for which we are looking. Since this is a global setting, I’ve made sure to capture its entering value and reset it afterward.
Now we’ll take a look at creating files and directories. Again, you will see a similar construct for these functions. Notice that the only real changes are the methods I’ve invoked to perform the action.
Function CreateDirectory(rDir, rName)
If objFtp.IsConnected Then
success = objFtp.ChangeRemoteDir(rDir)
If (success <> 1) Then
CreateDirectory = objFtp.LastErrorText
Exit Function
End If
success = objFtp.CreateRemoteDir(rName)
If (success <> 1) Then
CreateDirectory = objFtp.LastErrorText
Exit Function
End If
CreateDirectory = "ok"
End If
End Function
Next: Creating a Text File >>
More Windows Scripting Articles
More By Nilpo