FTP a file from one server to another, automatically

We are using an ASP script to create iCal files from a database, so another department can display the events on their web-based calendar.

Their calendar page is outside the corporate firewall. The ASP script needs to reside on and IIS server within the firewall. I needed a fool-proof way to transfer the ical file nightly out to an external web server so it could be accessed by any calendar applications that need to use it. The best way I’ve found so far is to create a batch file with command-line FTP instructions, and add it to a Windows scheduled task.

  1. To do this with your own files, create a directory that will hold the batch file somewhere on the server. Since the batch file will contain your destination server’s ftp password in plaintext, you will want to secure the directory. Create a Windows or domain account whose password will never change. I happened to have a “utility” domain-based account that already had access to some parts of the server, so I gave that account rights to the new directory.

  2. Create a new text file inside the directory called “fileup.bat”. Enter the following text. The items in red need to be customized: add your ftp username and password, and the file-system path to the destination directory on the receiving server. Save the file. Test it by running it in the command window.
    @echo off
    echo user ftpusername> ftpcmd.dat
    echo ftpuserpassword>> ftpcmd.dat
    echo cd /pathto/destination/directory/>>ftpcmd.dat
    echo bin>> ftpcmd.dat
    echo put %1>> ftpcmd.dat
    echo quit>> ftpcmd.dat
    ftp -n -s:ftpcmd.dat ftp.umhscompliance.net
    del ftpcmd.dat

  3. Test the file by running it in the command window:
    E:fileStorageftp>fileup E:contentcbtlibmodulestestrssicalDemo.ics
    Connected to external.server.com.
    220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
    220-You are user number 2 of 50 allowed.
    220-Local time is now 12:40. Server port: 21.
    220 You will be disconnected after 15 minutes of inactivity.
    ftp> user ftpusername
    331 User ftpusername OK. Password required

    230-User ftpusername has group access to: ftpusername
    230 OK. Current restricted directory is /
    ftp> cd /pathto/destination/directory/
    250 OK. Current directory is /pathto/destination/directory/
    ftp> bin
    200 TYPE is now 8-bit binary
    ftp> put E:contentcbtlibmodulestestrssicalDemo.ics
    200 PORT command successful
    150 Connecting to port 4297
    226-File successfully transferred
    226 0.174 seconds (measured here), 331.30 Kbytes per second
    ftp: 59172 bytes sent in 0.00Seconds 59172000.00Kbytes/sec.
    ftp> quit
    221-Goodbye. You uploaded 58 and downloaded 0 kbytes.
    221 Logout. E:fileStorageftp>


  4. Log in to the destination webserver and check that the file has arrived, in the right directory.


  5. Create a scheduled task for this procedure. Select Add Scheduled Task as shown.


  6. Click Browse to find the batch file you want to schedule.


  7. Select the file and click Open.


  8. Type a name for the task, and how often to perform the task. Click Next.


  9. Select a time and start date, and click Next.


  10. Enter the name and password of the Windows “utility” user mentioned earlier. The password should never change, or the task will stop running.


  11. Check “Open advanced properties for this task…” and click Finish.


  12. In the Run box, type a space after the name of your batch file, and add the filesystem path to the file you want to send to the other server. In the example shown here, the full entry reads:
    E:fileStorageftpfileup.bat E:contentcbtlibmodulestestrssicalDemo.ics