简体   繁体   English

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

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

I am so confused with this, I am trying to upload data to FTP through VBS script and it works fine for a single file file but doesn't upload multiple files when I add the script inside a loop. 我对此感到很困惑,我试图通过VBS脚本将数据上传到FTP,并且它对于单个文件文件可以正常工作,但是当我在循环中添加脚本时不能上传多个文件。

Also , why do we need to generate a temporary file when remote uploading to FTP, 另外,为什么远程上传到FTP时我们需要生成一个临时文件,

I mean to say this, 我是说这个

this is the script I am using, 这是我正在使用的脚本,

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)

Why do we creating a Temporary file with this code, :S 为什么我们使用以下代码创建临时文件: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

Although I am looping through the script , it must upload every txt file inside current directory but it uploads only the first one, why is it so ? 尽管我遍历该脚本,但它必须上载当前目录中的每个txt文件,但只上载第一个,为什么呢?

When I print the current file ie 当我打印当前文件即

MsgBox defaultfile

it display the name of each txt file inside the current folder but uploads first file only. 它显示当前文件夹中每个txt文件的名称,但仅上传第一个文件。

There is no native method of live automation for FTP. 没有用于FTP的实时自动化本机方法。 This is actually a scripting workaround. 这实际上是脚本解决方法。 Your script is creating a text file with FTP instructions. 您的脚本正在使用FTP指令创建文本文件。 Then, it launches FTP from the command line using the -s switch. 然后,它使用-s开关从命令行启动FTP。 That -s switch allows you to provide a text file containing session commands for FTP.exe to execute before closing. 使用-s开关,您可以提供一个包含会话命令的文本文件,以便FTP.exe在关闭前执行。 So while your script isn't actually automating the FTP program directly, it simulates automation by running FTP in "unattended mode". 因此,尽管您的脚本实际上并未直接直接使FTP程序自动化,但它通过在“无人参与模式”下运行FTP来模拟自动化。 You can get a better understanding by opening a command prompt and typing ftp /? 通过打开命令提示符并键入ftp /,可以更好地理解 .

[EDIT] I apologize, I missed your second question. [编辑]对不起,我想念您的第二个问题。 Your script is only uploading the first file because it loops around the CreateTextFile method. 您的脚本仅上传第一个文件,因为它在CreateTextFile方法周围循环。 This method is failing after the first attempt because the file already exists. 第一次尝试后此方法失败,因为该文件已存在。 What you really ought to do is loop around adding your files to the temporary file and then execute FTP only once. 您真正应该做的是将文件添加到临时文件中,然后仅执行一次FTP。 Your web server will appreciate the break as well. 您的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