问题描述
我完全不是VB Scripting的专家,但是因为这是我目前的一个项目的需求,所以我正在尝试编写一个VBScript来从指定的FTP文件夹中获取所有文件。
我设法得到一个指定的文件,但我似乎无法获取文件夹中的所有文件。这里是我试图使用的脚本:
Dim objOutStream
Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject(Scripting.FileSystemObject)
Set objOutStream = objFSO.OpenTextFile(C:\ temp \temp\empty.txt,ForWriting,True,TristateFalse)
使用objOutStream
.WriteLineUSER myuser'USERNAME
.WriteLinemypass'Password
.WriteLinebinary
.WriteLineprompt n
.WriteLinelcd / foldertocopyfrom'文件夹我正在更改为
.WriteLinemget *'获取当前日期的所有文件in
.WriteLinebye
.Close
End With
Set oFTPScriptShell = CreateObject(WScript.Shell)
oFTPScriptShell.Run %comspec%/ c FTP -n -s:& C:\temp\temp\empty.txt& & ftp.location.com,0,True
它不会给我一个错误或任何东西,它基本上什么都不做(是的,我确定/ foldertocopy中的文件来自: - ))
任何想法?
谢谢!
我试过了你的解决方案,但必须做一些小的修复,使其工作:
$ b
- 添加选项显式(更好地检测未定义的变量) li>
- 删除TristateFalse参数
- 删除
提示符
因为交互模式已关闭,因此切换 > - 更改
bye
至退出
- 已添加-i参数添加到FTP命令中
- 另外,我在命令行上测试了您的FTP命令,然后在您的脚本中使用它
以下是修改过的脚本
pre $ $ $ c $ Option Option Explicit
Const ForWriting = 2
Dim objOutStream,objjFSO,objShell
Set objFSO = CreateObject(Scripting.FileSystemObject)
Set objOutStream = objFSO.OpenTextFile(C:\temp\temp\ empty.txt,ForWriting,True)
使用objOutStrea m
.WriteLineUSER myuser'USERNAME
.WriteLinemypass'Password
.WriteLinebinary
.WriteLinelcd / foldertocopyfrom'文件夹我正在更改转换为
.WriteLinemget *'获取所有当前日期的文件
.WriteLinequit
.Close
End With
Set objShell = CreateObject(WScript.Shell)
objShell.Run%comspec%/ c FTP -n -i -s:& C:\temp\temp\empty.txt& & ftp.location.com,0,True
I'm not at all an expert at VB Scripting, but since it's a requirement at one of my projects for the moment, I am trying to write a VBScript that will GET all files from a specified FTP Folder.
I manage to get a single specified file, but I can't seem to get all files in a folder. Here's the script I'm trying to use:
Dim objOutStream
Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutStream = objFSO.OpenTextFile("C:\temp\temp\empty.txt", ForWriting, True, TristateFalse)
With objOutStream
.WriteLine "USER myuser" ' USERNAME
.WriteLine "mypass" ' Password
.WriteLine "binary"
.WriteLine "prompt n"
.WriteLine "lcd /foldertocopyfrom" ' FOLDER I'm changing into
.WriteLine "mget *" ' Get all files with today's date in it
.WriteLine "bye"
.Close
End With
Set oFTPScriptShell = CreateObject("WScript.Shell")
oFTPScriptShell.Run "%comspec% /c FTP -n -s:" & "C:\temp\temp\empty.txt" & " " & "ftp.location.com", 0, True
It doesn't give me an error or anything, it basically does nothing (and yes, I'm sure there are files in the /foldertocopy from :-))
Any ideas? Something obvious I am missing?
Thanks!
I tried your solution but had to make a couple small fixes to make it work:
- Added Option Explicit (for better detection of undefined variables)
- Removed the TristateFalse parameter
- Removed
prompt
since toggle since interactive mode is already off - Changed
bye
toquit
- Added -i parameter to the FTP command
- Plus I tested your FTP command on the command line before using it in your script
Here's the modified script
Option Explicit
Const ForWriting = 2
Dim objOutStream, objjFSO, objShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutStream = objFSO.OpenTextFile("C:\temp\temp\empty.txt", ForWriting, True)
With objOutStream
.WriteLine "USER myuser" ' USERNAME
.WriteLine "mypass" ' Password
.WriteLine "binary"
.WriteLine "lcd /foldertocopyfrom" ' FOLDER I'm changing into
.WriteLine "mget *" ' Get all files with today's date in it
.WriteLine "quit"
.Close
End With
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /c FTP -n -i -s:" & "C:\temp\temp\empty.txt" & " " & "ftp.location.com", 0, True
这篇关于使用VBScript从FTP下载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!