wscript FTP下载错误

时间:2014-03-22 23:13:34

标签: windows vbscript ftp

我正在尝试使用naterice.com的这个vbs脚本。它似乎工作但下载的文件,或文件是空白的。任何的想法? (Windows 2000,IIS6)。谢谢。

DIM sSite
DIM sUsername
DIM sPassword
DIM sLocalPath
DIM sRemotePath
DIM sRemoteFile

sSite="xxx.xxx.xx"
sUsername="yyyy"
sPassword="password"
sLocalPath="C:\rss"
sRemotePath="/directory"
sRemoteFile="*.htm"

FTPDownload sSite, sUsername, sPassword, sLocalPath, sRemotePath, sRemoteFile

Function FTPDownload(sSite, sUsername, sPassword, sLocalPath, sRemotePath, sRemoteFile)
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com

  Const OpenAsDefault = -2
  Const FailIfNotExist = 0
  Const ForReading = 1
  Const ForWriting = 2

  Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
  Set oFTPScriptShell = CreateObject("WScript.Shell")

  sRemotePath = Trim(sRemotePath)
  sLocalPath = Trim(sLocalPath)

  sOriginalWorkingDirectory = oFTPScriptShell.CurrentDirectory
  oFTPScriptShell.CurrentDirectory = sLocalPath
  '--------END Path Checks---------

  'build input file for ftp command
  sFTPScript = sFTPScript & "USER " & sUsername & vbCRLF
  sFTPScript = sFTPScript & sPassword & vbCRLF
  sFTPScript = sFTPScript & "cd " & sRemotePath & vbCRLF
  sFTPScript = sFTPScript & "binary" & vbCRLF
' sFTPScript = sFTPScript & "ascii" & vbCRLF
  sFTPScript = sFTPScript & "prompt n" & vbCRLF
  sFTPScript = sFTPScript & "mget " & sRemoteFile & vbCRLF
  sFTPScript = sFTPScript & "quit" & vbCRLF & "quit" & vbCRLF & "quit" & vbCRLF


  sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
  sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName
  sFTPResults = sFTPTemp & "\" & oFTPScriptFSO.GetTempName

  'Write the input file for the ftp command
  'to a temporary file.
  Set fFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)
  fFTPScript.WriteLine(sFTPScript)
  fFTPScript.Close
  Set fFTPScript = Nothing 

  oFTPScriptShell.Run "%comspec% /c FTP -n -s:" & sFTPTempFile & " " & sSite &  " > " & sFTPResults, 0, TRUE

  Wscript.Sleep 1000

  'Check results of transfer.
  Set fFTPResults = oFTPScriptFSO.OpenTextFile(sFTPResults, ForReading, FailIfNotExist, OpenAsDefault)
  sResults = fFTPResults.ReadAll
  fFTPResults.Close

  'oFTPScriptFSO.DeleteFile(sFTPTempFile)
  'oFTPScriptFSO.DeleteFile (sFTPResults)

  If InStr(sResults, "226 Transfer complete.") > 0 Then
    FTPDownload = True
  ElseIf InStr(sResults, "File not found") > 0 Then
    FTPDownload = "Error: File Not Found"
  ElseIf InStr(sResults, "cannot log in.") > 0 Then
    FTPDownload = "Error: Login Failed."
  Else
    FTPDownload = "Error: Unknown."
  End If

  Set oFTPScriptFSO = Nothing
  Set oFTPScriptShell = Nothing
End Function

您好,我正在尝试使用naterice.com的这个vbs脚本。它似乎工作但下载的文件,或文件是空白的。任何的想法? (Windows 2000,IIS6)。谢谢。

2 个答案:

答案 0 :(得分:0)

以下是从MS的ftp服务器下载文本文件并将其保存在c:\ user文件夹中的示例。

使用cscript在命令提示符下运行它以查看错误消息(服务器和本地),或将Outp.write更改为消息框。

cscript "c:\path to script\ftp.vbs"

您必须绝对正确地指定网址。找不到你的意思,没有帮助。服务器路径上的匹配大小写,例如服务器上的README.TXT也应该是URL中的大写。

ADODB是二进制格式。您拥有的代码只能在英语计算机上可靠地工作,因为Windows会翻译所用语言的字符。此代码最初下载了MS的安全扫描程序。

 Set fso = CreateObject("Scripting.FileSystemObject")
 Set Outp = Wscript.Stdout
 On Error Resume Next
 Set File = WScript.CreateObject("Microsoft.XMLHTTP")
 File.Open "GET", "ftp://ftp.microsoft.com/Softlib/README.TXT", False
 File.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C; .NET4.0E; BCD2000; BCD2000)"
 File.Send
 If err.number <> 0 then 
  Outp.writeline "" 
  Outp.writeline "Error getting file" 
  Outp.writeline "==================" 
  Outp.writeline "" 
  Outp.writeline "Error " & err.number & "(0x" & hex(err.number) & ") " & err.description 
  Outp.writeline "Source " & err.source 
  Outp.writeline "" 
  Outp.writeline "HTTP Error " & File.Status & " " & File.StatusText
  Outp.writeline  File.getAllResponseHeaders
 End If

On Error Goto 0

 Set BS = CreateObject("ADODB.Stream")
 BS.type = 1
 BS.open
 BS.Write File.ResponseBody
 BS.SaveToFile "c:\users\ReadMe.txt", 2

答案 1 :(得分:0)

感谢Fred的帮助,最后我发现我的服务器遇到了一些网络问题。它接受一些流量,但不是全部...这可以解释为什么naterice.com的第一个脚本一直在我的计算机上工作,而不是在服务器上工作。您的脚本也是如此。