为什么我们需要在远程上传到FTP时生成临时文件?

时间:2012-02-17 20:50:20

标签: vbscript upload ftp download

我对此非常困惑,我正在尝试通过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文件的名称,但仅上传第一个文件。

1 个答案:

答案 0 :(得分:5)

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)
相关问题