Option Explicit 'Saves a text FTP file locally. Pass URL, local file, 'user name, and password on the command line. 'Returns errorlevel 0 if succeeded, errorlevel 1 if failed. Main Sub Main Dim strUrl, strLocalFile, strUserName, strPassword 'Get arguments If Wscript.Arguments.Count > 4 Then CreateObject("Wscript.Shell").Popup "Too many arguments! You may have to quote any arguments that have spaces or commas in them.", 10 Wscript.Quit 1 End If If Wscript.Arguments.Count = 4 Then strUrl = Wscript.Arguments(0) strLocalFile = Wscript.Arguments(1) strUserName = Wscript.Arguments(2) strPassword = Wscript.Arguments(3) End If If Wscript.Arguments.Count = 3 Then CreateObject("Wscript.Shell").Popup "Incorrect number of arguments! This script needs either two or four arguments. You may have to quote any arguments that have spaces or commas in them.", 10 Wscript.Quit 1 End If If Wscript.Arguments.Count = 2 Then strUrl = Wscript.Arguments(0) strLocalFile = Wscript.Arguments(1) strUserName = "anonymous" strPassword = "ieuser@" End If If Wscript.Arguments.Count < 2 Then CreateObject("Wscript.Shell").Popup "Not enough arguments! This script needs an FTP URL, a local file name, and optionally a user name and password.", 10 Wscript.Quit 1 End If 'Check local file If Instr(strLocalFile, ":\") = 0 Then strLocalFile = FileNameInThisDir(strLocalFile) End If 'Download the file If DownloadFtp(strUrl, strLocalFile, strUserName, strPassword) Then Wscript.Quit 0 Else Wscript.Quit 1 End If End Sub Function DownloadFtp(strUrl, strLocalFile, strUserName, strPassword) 'As Boolean Dim wsh 'As Wscript.Shell Dim fs 'As Scripting.FileSystemObject Dim tsScript 'As Scripting.TextStream Dim strRemoteSite 'As String Dim strRemoteFile 'As String Dim strScript 'As String Const TemporaryFolder = 2 Const WshHide = 0 Const WshNormalFocus = 1 Const WshMinimizedFocus = 2 Const WshMaximizedNoFocus = 3 Const WshNormalNoFocus = 4 Const WshMinimizedNoFocus = 6 ' Create objects Set wsh = CreateObject("Wscript.Shell") Set fs = CreateObject("Scripting.FileSystemObject") strScript = fs.BuildPath(fs.GetSpecialFolder(TemporaryFolder), fs.GetBaseName(Wscript.ScriptFullName) & ".script") If strUrl = "" Then DownloadFtp = False Exit Function End If If Instr(strUrl, ".") = 0 Then DownloadFtp = False Exit Function End If If Instr(strUrl, "/") = 0 Then DownloadFtp = False Exit Function End If ' Create the local file just so we can get a short name later On Error Resume Next Err.Clear fs.CreateTextFile(strLocalFile, True).Close If Err.Number <> 0 Then DownloadFtp = False Exit Function End If On Error Goto 0 If strUserName = "" Then strUserName = "anonymous" If strPassword = "" Then strPassword = "robot@" ' Parse site name strRemoteSite = strUrl If Lcase(Left(strRemoteSite, 6)) = "ftp://" Then strRemoteSite = Mid(strRemoteSite, 7) strRemoteSite = Left(strRemoteSite, Instr(strRemoteSite, "/") - 1) ' Parse file name strRemoteFile = strUrl If Lcase(Left(strRemoteFile, 6)) = "ftp://" Then strRemoteFile = Mid(strRemoteFile, 7) strRemoteFile = Mid(strRemoteFile, Instr(strRemoteFile, "/")) ' Create and run the FTP script Set tsScript = fs.CreateTextFile(strScript, True) tsScript.WriteLine "open " & strRemoteSite tsScript.WriteLine strUserName tsScript.WriteLine strPassword tsScript.WriteLine "hash" tsScript.WriteLine "ascii" tsScript.WriteLine "get " & strRemoteFile & " " & fs.GetFile(strLocalFile).ShortPath tsScript.WriteLine "bye" tsScript.Close Set tsScript = Nothing If fs.FileExists(fs.GetSpecialFolder(1) & "\ftp.exe") Then wsh.Run fs.GetSpecialFolder(1) & "\ftp.exe -s:""" & strScript & """", WshHide, True Else wsh.Run fs.GetSpecialFolder(0) & "\ftp.exe -s:""" & strScript & """", WshHide, True End If ' Delete the script as soon as possible because it has the user name and password in clear text fs.DeleteFile strScript ' Delete the local file if we failed to download anything ' FTP may have already deleted it. If fs.FileExists(strLocalFile) Then If fs.GetFile(strLocalFile).Size = 0 Then fs.DeleteFile strLocalFile, True DownloadFtp = False Else DownloadFtp = True End If Else DownloadFtp = False End If End Function Function FileNameInThisDir(strFileName) 'As String 'Returns the complete path and file name to a file in 'the script directory. For example, "trans.log" might 'return "C:\Program Files\Scripts\Database\trans.log" 'if the script was in the "C:\Program Files\Scripts\Database" 'directory. Dim fs 'As Object Set fs = Wscript.CreateObject("Scripting.FileSystemObject") FileNameInThisDir = fs.GetAbsolutePathName(fs.BuildPath(Wscript.ScriptFullName, "..\" & strFileName)) ''''''''''Clean up Set fs = Nothing End Function