簡體   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