FTP’ing Files with ASP - The Great FTP Function
(Page 3 of 4 )
If you were blessed (ok, cursed) to have the insight and perception to understand what I’m thinking this very moment, you’d understand how perplexed I am. See, I’ve been trying - quite unsuccessfully - to think of some rolling way to pronounce FTP. SQL is pronounced ‘sequel’, C# pronounced ‘See Sharp,’ the humor portion of my brain pronounced it comatose, but FTP has nothing. Not ‘efftup’, not ‘fu-tap’, it’s really disconcerting. Or perhaps no one cares as much as I do. That being most probable, let’s forget I mentioned it, and do some coding!
It is our goal to put together some kind of FTP command(s), which we can send to a function by typing something not too dissimilar to FTP( strCommands ). We will deal with the commands later; let’s worry about the function for now.
'=====================
Function FTP( strCMD )
'=====================
'=== Build a command script, FTPs with it, deletes it
Dim objFSO, strFile, objTempFldr, objFile, objRegExp
Dim objShell, WSX, ReturnCode, Output, strLog, strErrorLog
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objTempFldr = objFSO.GetSpecialFolder( 2 )
strFile = objFSO.GetTempName
This sets up our File System Object, Finds the system TEMP folder, and generates a random filename. You’re welcome to change whatever portion of this you don’t agree with of course, it’s merely a good suggestion.
strFile
= objTempFldr & "" & strFile & ".ftp"
if not objFSO.FileExists( strFile ) then objFSO.CreateTextFile( strFile )
Set objFile = objFSO.OpenTextFile( strFile, 2, True )
So we’ve created and opened the [randomly named] text file. Now we entirely free to begin throwing our FTP (foo-top?) commands into it, provided that they’re in proper formation of course!
objFile
.WriteLine( strUser )
objFile.WriteLine( strPass )
If LocalDir <> "" Then objFile.WriteLine( "lcd " & LocalDir )
If RemoteDir <> "" Then objFile.WriteLine( "cd " & RemoteDir )
objFile.WriteLine( Mode )
At this point we’ve established our operating environment, by use of our very variable variants. Now all that’s left is to tell the function exactly that we want to transfer.
objFile
.WriteLine( strCMD )
objFile.WriteLine( "bye" )
objFile.Close()
Set objShell = Server.CreateObject("WScript.Shell")
We’ve finished with the .ftp command file, and now we begin our FTP process by creating a Shell object. If you encounter any mysterious errors at this line (Library not registered), it’s a safe bet you don’t have C:winntsystem32wshom.ocx. It is by default registered, but sometimes removed for... security! (Thanks again, Microsoft.) Let’s execute this sucker!
set WSX
= objShell.Exec( COMMAND_FTP & strFile & " " & strHost )
set ReturnCode = WSX.StdErr
set Output = WSX.stdOut
strErrorLog = objTempFldr.Path & "ftpErrors.txt"
strLog = objTempFldr.Path & "ftpLog.txt"
Set objFile = objFSO.OpenTextFile( strErrorLog, 2, True )
objFile.Write( ReturnCode.ReadAll() )
objFile.Close()
Set objFile = objFSO.OpenTextFile( strLog, 2, True )
objFile.Write( Output.ReadAll() )
objFile.Close()
set objFSO = nothing
set objFile = nothing
objFSO.DeleteFile strFile, True
set objFSO = nothing
So there we have it. We’ve done it this time, we’ve really done it! And we’ve managed to clean up after ourselves too, logging all the actions within ftpErrors.txt and ftpLog.txt. Just in case you’re wondering, your password is not included in the log file, and the script file has been deleted, so you should be safe there.
Now the next little part is somewhat optional. It’s in case you want to be notified immediately upon encountering an error, which most of us do. Of course if you wish to continue in your state of blissful ignorance, that’s entirely your prerogative. But we have the option of searching through our logs for error messages, and here’s how it goes:
Set objRegExp
= New RegExp
objRegExp.IgnoreCase = True
objRegExp.Pattern = "not connected|invalid command|error"
If (objRegExp.Test( Output.ReadAll ) = True ) or
(objRegExp.Test( ReturnCode.ReadAll ) ) Then ‘on one line
FTP = False
Else
FTP = True
End If
Set objRegExp = nothing
End Function
And that’s that in its entirety. Oh, and just in case you’re wondering what kind of commands the function is looking for, they would resemble:
strCommands
= “MKD JUSTINS_BABY_PICTURES” & vbCrLf & _
“MKD LOOKED_LIKE_MONKEY” & vbCrLf & _
“PUT simian1.jpg” & vbCrLf & _
“PUT banana.gif”
There you go, you are fully equipped to transfer some bytes. Go nuts I tell you, go nuts.
Next: Conclusion >>
More ASP Articles
More By Justin Cook