ASP
  Home arrow ASP arrow Page 3 - FTP’ing Files with ASP
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

FTP’ing Files with ASP
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 176
    2004-01-21

    Table of Contents:
  • FTP’ing Files with ASP
  • Variable: Having No Fixed Quantitative Value
  • The Great FTP Function
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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
    .FileExistsstrFile then objFSO.CreateTextFilestrFile )
    Set objFile 
    objFSO.OpenTextFilestrFile2True )

    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.WriteLinestrUser )
    objFile
    .WriteLinestrPass )
    If LocalDir 
    <> "" Then objFile.WriteLine"lcd " LocalDir )
    If RemoteDir 
    <> "" Then objFile.WriteLine"cd " RemoteDir )
    objFile
    .WriteLineMode )

    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.WriteLinestrCMD )
    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.ExecCOMMAND_FTP strFile " " strHost )
    set ReturnCode 
    WSX.StdErr
    set Output 
    WSX.stdOut
    strErrorLog 
    objTempFldr.Path "ftpErrors.txt"
    strLog 
    objTempFldr.Path "ftpLog.txt"
     
    Set objFile 
    objFSO.OpenTextFilestrErrorLog2True )
    objFile
    .WriteReturnCode.ReadAll() )
    objFile
    .Close()
     
    Set objFile 
    objFSO.OpenTextFilestrLog2True )
    objFile
    .WriteOutput.ReadAll() )
    objFile
    .Close()
    set objFSO 
    nothing
    set objFile 
    nothing
     
    objFSO
    .DeleteFile strFileTrue
    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.TestOutput.ReadAll ) = True ) or 
    (objRegExp.TestReturnCode.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.

    More ASP Articles
    More By Justin Cook


       · Thank you very much for your spectacular and entertaining code!However, I don't...
     

    ASP ARTICLES

    - ADO for the Beginner
    - ADO.NET 101: Data Rendering with a DataGrid ...
    - Introducing SoftArtisans OfficeWriter 3.0 En...
    - Getting Remote Files With ASP
    - The Real Basics of Functions in ASP
    - Enhancing Readability with ASP
    - Mimicking PHP's String Formatting Functions
    - Windows Server Hacks 12, 77, and 98
    - How to Sort a Multi-Dimensional Array
    - Developing an Information Management Tool wi...
    - What are Active Server Pages?
    - Getting Remote Pages with ASP
    - FTP’ing Files with ASP
    - Apply Single-Sign-On to Your Application
    - Easy Error Management





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT