繁体   English   中英

为什么远程上传到FTP时需要生成一个临时文件?

[英]Why do we need generate a temporary file when remote uploading to FTP?

我对此感到很困惑,我试图通过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文件的名称,但仅上传第一个文件。

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

[编辑]对不起,我想念您的第二个问题。 您的脚本仅上传第一个文件,因为它在CreateTextFile方法周围循环。 第一次尝试后此方法失败,因为该文件已存在。 您真正应该做的是将文件添加到临时文件中,然后仅执行一次FTP。 您的Web服务器也将不胜感激。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM