我正在尝试通过ftp发送文件,然后检查该过程是否成功完成,如果是的话,我将删除原始文件并仅将发送的文件保留在FTP的目标文件夹中。
我设法修补了一个脚本,该脚本连接到FTP并发送文件,但是我不确定如何将原始文件夹与FTP上的那个文件夹交叉检查,以便确定复制是否成功。
这是通过FTP发送文件的代码,在测试中它成功发送了所有文件,但是删除之前我必须检查
Set oShell = CreateObject("Shell.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
path = "D:\test\"
FTPUpload(path)
Sub FTPUpload(path)
On Error Resume Next
Const copyType = 16
'FTP Wait Time in ms
waitTime = 80000
FTPUser = "test"
FTPPass = "testtest"
FTPHost = "ftp.test.com"
FTPDir = "/htdocs/test/"
strFTP = "ftp://" & FTPUser & ":" & FTPPass & "@" & FTPHost & FTPDir
Set objFTP = oShell.NameSpace(strFTP)
'Upload single file
If objFSO.FileExists(path) Then
Set objFile = objFSO.getFile(path)
strParent = objFile.ParentFolder
Set objFolder = oShell.NameSpace(strParent)
Set objItem = objFolder.ParseName(objFile.Name)
Wscript.Echo "Uploading file " & objItem.Name & " to " & strFTP
objFTP.CopyHere objItem, copyType
End If
'Upload all files in folder
If objFSO.FolderExists(path) Then
'Entire folder
Set objFolder = oShell.NameSpace(path)
Wscript.Echo "Uploading folder " & path & " to " & strFTP
objFTP.CopyHere objFolder.Items, copyType
End If
If Err.Number <> 0 Then
Wscript.Echo "Error: " & Err.Description
End If
'Wait for upload
Wscript.Sleep waitTime
End Sub
我将不胜感激,谢谢。
不确定这是否是最好的方法,但是您可以通过FTP
命令获取文件列表,并将结果与期望的结果进行比较。这是一个例子:
' Create the FTP command file...
With CreateObject("Scripting.FileSystemObject").CreateTextFile("c:\ftp.txt", True)
.WriteLine "USER test"
.WriteLine "testtest"
.WriteLine "ls /htdocs/test/"
.WriteLine "quit"
.Close
End With
' Run the command and capture its output...
With CreateObject("WScript.Shell")
.Run "%comspec% /c ftp -n -v -s:c:\ftp.txt ftp.test.com >c:\filelist.txt", 0, True
End With
这将创建一个文件c:\filelist.txt
,然后可以打开该文件并检查是否存在您上载的文件。当然,您可以在ls
命令中添加其他参数以获取更多详细信息。例如,ls -l
将为您提供更新日期以及文件大小。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句