问题描述
我很困惑,我试图通过VBS脚本将数据上传到FTP,它对单个文件文件可以正常工作,但在循环中添加脚本时不会上传多个文件。另外,为什么我们需要在远程上传到FTP时生成一个临时文件,
我的意思是说, / p>
这是我使用的脚本,
Dim fso,folder ,文件,strPath
设置fso = CreateObject(Scripting.FileSystemObject)
strPath =E:/ Test
设置文件夹= fso.GetFolder(strPath)
设置文件= folder.Files
Const hostname =ftp.domain.com
Const port = 21
Const username =username
Const password =password
Const remoteDir =/
Const useDefaultsExclusively = True
Const skipConfirmation = True
对于每个项目在文件
如果InStr(1,item.Name,txt)<> 0然后
defaultFile = item.Name
localFile = fso.getFileName(defaultFile)
localDir = fso.getParentFolderName(defaultFile)
设置shell = CreateObject(WScript.Shell)
tempDir = shell.ExpandEnvironmentStrings(%TEMP%)
'提供给Windows FTP客户端的临时脚本文件
scriptFile = tempDir& \& fso.GetTempName
'从Windows FTP客户端存储标准输出的临时文件
outputFile = tempDir& \& fso.GetTempName
'输入脚本
script = script& lcd& & localDir& & vbCRLF
script = script& 开放&主机名称&港口& vbCRLF
script = script& 用户&用户名和vbCRLF
script = script&密码& vbCRLF
script = script& cd& & remoteDir& & vbCRLF
script = script& 二进制& vbCRLF
script = script& 提示n& vbCRLF
script = script& 放& &本地文件& & vbCRLF
script = script& 退出& vbCRLF
设置textFile = fso.CreateTextFile(scriptFile,True)
textFile.WriteLine(脚本)
'bWaitOnReturn设置为TRUE - 指示脚本应该等待程序
'完成执行,然后再继续下一条语句
shell.Run%comspec%/ c FTP -n -s:&脚本文件& && outputFile,0,TRUE
Wscript.Sleep 10
'打开标准输出临时文件只读,如果不存在则失败
设置textFile = fso.OpenTextFile(outputFile,1,0,-2)
results = textFile.ReadAll
textFile.Close
如果
下一个
fso.DeleteFile(scriptFile)
fso.DeleteFile(outputFile)
为什么要使用此代码创建临时文件: S
设置shell = CreateObject(WScript.Shell)
tempDir = shell.ExpandEnvironmentStrings %TEMP%)
'提供给Windows FTP客户端的临时脚本文件
scriptFile = tempDir& \& fso.GetTempName
'从Windows FTP客户端存储标准输出的临时文件
outputFile = tempDir& \& fso.GetTempName
设置textFile = fso.CreateTextFile(scriptFile,True)
textFile.WriteLine(脚本)
'bWaitOnReturn设置为TRUE - 指示脚本应该等待程序
'完成执行,然后再继续下一条语句
shell.Run%comspec%/ c FTP -n -s:& ;脚本文件& && outputFile,0,TRUE
Wscript.Sleep 10
'打开标准输出临时文件只读,如果不存在则失败
设置textFile = fso.OpenTextFile(outputFile,1,0,-2)
results = textFile.ReadAll
textFile.Close
尽管我正在循环脚本,它必须上传当前目录中的每个txt文件,但它只上传第一个,为什么是这样?
当我打印当前文件ie
MsgBox defaultfile
它显示当前文件夹中每个txt文件的名称,但仅上传第一个文件。
没有本机方法的FTP实时自动化。这实际上是一个脚本解决方法。您的脚本正在使用FTP指令创建文本文件。然后,它使用-s开关从命令行启动FTP。该-s开关允许您提供一个包含会话命令的文本文件,供FTP.exe在关闭之前执行。因此,尽管您的脚本实际上并不直接自动化FTP程序,但是通过在无人参与模式中运行FTP来模拟自动化。打开命令提示符并输入 ftp /?可以获得更好的理解。
我道歉,我错过了你的第二个问题。您的脚本只是上传第一个文件,因为它围绕CreateTextFile方法循环。该方法在第一次尝试后失败,因为该文件已经存在。你真正应该做的是将文件添加到临时文件中,然后再执行一次FTP。您的网络服务器也将欣赏中断。
Dim fso,文件夹,文件,strPath
设置fso = CreateObject (Scripting.FileSystemObject)
strPath =E:/ Test
设置文件夹= fso.GetFolder(strPath)
设置文件= 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%)
'提供给Windows FTP的临时脚本文件client
scriptFile = tempDir& \& fso.GetTempName
'从Windows FTP客户端存储标准输出的临时文件
outputFile = tempDir& \& fso.GetTempName
设置textFile = fso.CreateTextFile(scriptFile,True)
'输入脚本
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)
对于每个项目在文件
如果InStr(1,item.Name,txt)<> 0然后
textFile.WriteLine(put& Chr(34)& item.Path&\& item.Name& Chr(34))
End If
textFile.WriteLine(quit)
textFile.Close
设置shell = CreateObject(WScript.Shell)
' bWaitOnReturn设置为TRUE - 指示脚本应该等待程序
'在继续下一条语句之前完成执行
shell.Run%comspec%/ c FTP -n -s:&脚本文件& && outputFile,0,True
WScript.Sleep 10
'打开标准输出临时文件只读,如果不存在则失败
设置textFile = fso.OpenTextFile(outputFile,1,0,-2)
results = textFile.ReadAll
textFile.Close
fso.DeleteFile(scriptFile)
fso.DeleteFile(outputFile)
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.
Also , why do we need to generate a temporary file when remote uploading to 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
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 ?
When I print the current file i.e
MsgBox defaultfile
it display the name of each txt file inside the current folder but uploads first file only.
There is no native method of live automation for FTP. This is actually a scripting workaround. Your script is creating a text file with FTP instructions. Then, it launches FTP from the command line using the -s switch. That -s switch allows you to provide a text file containing session commands for FTP.exe to execute before closing. So while your script isn't actually automating the FTP program directly, it simulates automation by running FTP in "unattended mode". You can get a better understanding by opening a command prompt and typing ftp /?.
[EDIT]I apologize, I missed your second question. Your script is only uploading the first file because it loops around the CreateTextFile method. 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. Your web server will appreciate the break as well.
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时需要生成一个临时文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!