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

Vbscript 为什么远程上传到FTP时需要生成临时文件?,vbscript,upload,ftp,download,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

我对此感到非常困惑,我正试图通过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)
虽然我在循环脚本,但它必须上传当前目录中的每个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)

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)