No announcement yet.

2003 FTP scripting

  • Filter
  • Time
  • Show
Clear All
new posts

  • 2003 FTP scripting

    i am just finishing a script that i need to run to download a file from the previous day.
    anyone know how to do this?

    this script works for same day but i can't figure out how to do previous day.
    i've added this to 'scheduled tasks' etc. so it will automate every day. BUT can't get it to work for previous.
    any thoughts?

    echo off
    echo user username> ftpcmd.dat
    echo password>> ftpcmd.dat
    echo cd reports>> ftpcmd.dat
    echo prompt n>> ftpcmd.dat
    echo mget %DATE:~10%%DATE:~4,2%%DATE:~7,>> ftpcmd.dat
    echo close>> ftpcmd.dat
    echo quit>> ftpcmd.dat
    ftp -n -s:ftpcmd.dat server
    del ftpcmd.dat
    copy %DATE:~10%%DATE:~4,2%%DATE:~7, r:\reports
    del %DATE:~10%%DATE:~4,2%%DATE:~7,
    Last edited by Rems; 8th August 2008, 13:25. Reason: added "[code]-tags" around the batchcode

  • #2
    Re: 2003 FTP scripting

    So, every day a new file is created on the FTP site, and the name of the target file is always a datestamp in the format:

    What you can do is to replace the batch with a vbScript.
    Then you have the possibility to add a positive or negative number to any part of a valid date/time expression. (

    *.vbs sample file:
    FTPscript  = "ftpcmd.dat"
    dToday     = now()
    dYesterday = DateAdd("d", -1, dToday)
    dStamp     = Year(dYesterday)*1e4 + Month(dYesterday)*1e2 + Day(dYesterday)
    strFilename    = dStamp & ".zip"
    strDestination = "r:\reports"
    ' Create the FTPscript
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.CreateTextFile(FTPscript)
     objFile.WriteLine("user username")
     objFile.WriteLine("cd reports")
     objFile.WriteLine("prompt n")
     objFile.WriteLine("mget " & strFilename)
    Set objFile = Nothing
    ' Execute FTP command line
    strCommand = "FTP.exe -n -s:""" & FTPscript & """ server"
    Set objShell = Wscript.CreateObject("Wscript.Shell")
    eReturn ="%comspec% /T:2E /K title FTP in progress|" & strCommand & "&&@exit/b", 4, True)
    ' Move file to destination
    If eReturn = 0 then
      If objFSO.FileExists(strDestination & "\" & strFilename) Then
         objFSO.DeleteFile(strDestination & "\" & strFilename)
      End If
      Set objFileCopy = objFSO.GetFile(strFilename)
       objFileCopy.Move(strDestination & "\")
      Set objFileCopy = Nothing
      wscript.echo "ERROR"
    End If

    If you choose to use vbs, there is actually no need to write a separate inputfile (ftpcmd.dat) first!
    In the script above the method is used to run the FTP command line with the same FTP options you use in the batch.
    But, you could also use the objShell.Exec method instead. Then you don't need the -s: switch to call a ftpcmd.dat file. By using the Exec() method the send commands can done via StdIn from within the script and receive output via StdOut and StdErr.
    Google for: FTP via stdIn vbs for examples.

    Last edited by Rems; 3rd January 2009, 17:03.

    This posting is provided "AS IS" with no warranties, and confers no rights.


    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts