【问题标题】:Why do we need generate a temporary file when remote uploading to FTP?为什么远程上传到 FTP 时需要生成一个临时文件?
【发布时间】:2012-03-09 06:18:48
【问题描述】:

我对此感到很困惑,我正在尝试通过 VBS 脚本将数据上传到 FTP,它适用于单个文件文件,但当我在循环中添加脚本时不会上传多个文件。

还有,为什么我们在远程上传到FTP的时候需要生成一个临时文件,

我想说的是,

这是我正在使用的脚本,

Dim fso, folder, files, strPath
Set fso = CreateObject("Scripting.FileSystemObject")

strPath = "E:/Test"
Set folder = fso.GetFolder(strPath)
Set files = folder.Files

Const hostname = "ftp.domain.com"
Const port = 21
Const username = "username"
Const password = "password"
Const remoteDir =  "/"
Const useDefaultsExclusively = True
Const skipConfirmation = True


For each item In files

    If InStr(1, item.Name, "txt") <> 0 Then
    defaultFile = item.Name
    localFile = fso.getFileName(defaultFile)
    localDir = fso.getParentFolderName(defaultFile)
    Set shell = CreateObject("WScript.Shell")

  tempDir = shell.ExpandEnvironmentStrings("%TEMP%")
  ' temporary script file supplied to Windows FTP client
  scriptFile = tempDir & "\" & fso.GetTempName
  ' temporary file to store standard output from Windows FTP client
  outputFile = tempDir & "\" & fso.GetTempName

  'input script
  script = script & "lcd " & """" & localDir & """" & vbCRLF
  script = script & "open " & hostname & " " & port & vbCRLF
  script = script & "user " & username & vbCRLF
  script = script & password & vbCRLF
  script = script & "cd " & """" & remoteDir & """" & vbCRLF
  script = script & "binary" & vbCRLF
  script = script & "prompt n" & vbCRLF
  script = script & "put " & """" & localFile & """" & vbCRLF
  script = script & "quit" & vbCRLF


  Set textFile = fso.CreateTextFile(scriptFile, True)
  textFile.WriteLine(script)



  ' bWaitOnReturn set to TRUE - indicating script should wait for the program
  ' to finish executing before continuing to the next statement
  shell.Run "%comspec% /c FTP -n -s:" & scriptFile & " > " & outputFile, 0, TRUE
  Wscript.Sleep 10
  ' open standard output temp file read only, failing if not present
  Set textFile = fso.OpenTextFile(outputFile, 1, 0, -2)
  results = textFile.ReadAll
  textFile.Close

End If

Next

 fso.DeleteFile(scriptFile)
 fso.DeleteFile(outputFile)

我们为什么要用这段代码创建一个临时文件,:S

Set shell = CreateObject("WScript.Shell")

  tempDir = shell.ExpandEnvironmentStrings("%TEMP%")
  ' temporary script file supplied to Windows FTP client
  scriptFile = tempDir & "\" & fso.GetTempName
  ' temporary file to store standard output from Windows FTP client
  outputFile = tempDir & "\" & fso.GetTempName


 Set textFile = fso.CreateTextFile(scriptFile, True)
      textFile.WriteLine(script)



      ' bWaitOnReturn set to TRUE - indicating script should wait for the program
      ' to finish executing before continuing to the next statement
      shell.Run "%comspec% /c FTP -n -s:" & scriptFile & " > " & outputFile, 0, TRUE
      Wscript.Sleep 10
      ' open standard output temp file read only, failing if not present
      Set textFile = fso.OpenTextFile(outputFile, 1, 0, -2)
      results = textFile.ReadAll
      textFile.Close

虽然我在循环脚本,它必须上传当前目录中的每个txt文件,但它只上传第一个,为什么会这样?

当我打印当前文件时,即

MsgBox defaultfile

它显示当前文件夹中每个txt文件的名称,但只上传第一个文件。

【问题讨论】:

    标签: vbscript upload ftp download


    【解决方案1】:

    FTP 没有实时自动化的本地方法。这实际上是一种脚本解决方法。您的脚本正在创建一个带有 FTP 指令的文本文件。然后,它使用 -s 开关从命令行启动 FTP。该 -s 开关允许您提供一个包含会话命令的文本文件,以便 FTP.exe 在关闭之前执行。因此,虽然您的脚本实际上并没有直接自动化 FTP 程序,但它通过在“无人值守模式”下运行 FTP 来模拟自动化。您可以通过打开命令提示符并键入 ftp /? 来更好地理解。

    [编辑] 对不起,我错过了你的第二个问题。您的脚本仅上传第一个文件,因为它围绕 CreateTextFile 方法循环。此方法在第一次尝试后失败,因为该文件已存在。您真正应该做的是将文件循环添加到临时文件,然后只执行一次 FTP。您的网络服务器也会感谢您的休息。

    Dim fso, folder, files, strPath
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    strPath = "E:/Test"
    Set folder = fso.GetFolder(strPath)
    Set files = folder.Files
    
    Const hostname = "ftp.domain.com"
    Const port = 21
    Const username = "username"
    Const password = "password"
    Const remoteDir =  "/"
    Const useDefaultsExclusively = True
    Const skipConfirmation = True
    
    tempDir = shell.ExpandEnvironmentStrings("%TEMP%")
    ' temporary script file supplied to Windows FTP client
    scriptFile = tempDir & "\" & fso.GetTempName
    ' temporary file to store standard output from Windows FTP client
    outputFile = tempDir & "\" & fso.GetTempName
    
    Set textFile = fso.CreateTextFile(scriptFile, True)
    
    'input script
    textFile.WriteLine("open " & hostname & " " & port)
    textFile.WriteLine("user " & username)
    textFile.WriteLine(password)
    textFile.WriteLine("cd " & Chr(34) & remoteDir & Chr(34))
    textFile.WriteLine("binary")
    textFile.WriteLine("prompt n")
    
    For Each item In files
        If InStr(1, item.Name, "txt") <> 0 Then
            textFile.WriteLine("put " & Chr(34) & item.Path & "\" & item.Name & Chr(34))
        End If
    Next
    
    textFile.WriteLine("quit")
    textFile.Close
    
    Set shell = CreateObject("WScript.Shell")
    ' bWaitOnReturn set to TRUE - indicating script should wait for the program
    ' to finish executing before continuing to the next statement
    shell.Run "%comspec% /c FTP -n -s:" & scriptFile & " > " & outputFile, 0, True
    WScript.Sleep 10
    ' open standard output temp file read only, failing if not present
    Set textFile = fso.OpenTextFile(outputFile, 1, 0, -2)
    results = textFile.ReadAll
    textFile.Close
    
    fso.DeleteFile(scriptFile)
    fso.DeleteFile(outputFile)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      相关资源
      最近更新 更多