vbs脚本通过ftp发送文件并检查/删除原始文件 [英] vbs script to send files via ftp and check/delete original files

查看:214
本文介绍了vbs脚本通过ftp发送文件并检查/删除原始文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过ftp发送文件,然后检查过程是否成功完成,如果是这样的话,我会删除原始文件,并只保留FTP目标文件夹中发送的文件。



我设法修补连接到FTP并发送文件的脚本,但我不确定如何与FTP上的原始文件夹进行交叉检查,以便确定副本已成功。

这是通过FTP发送文件的代码,并且在测试中它成功发送了所有文件,但我必须在删除之前进行检查

  Set oShell = CreateObject(Shell.Application)
Set objFSO = CreateObject(Scripting.FileSystemObject)

path =D:\test\

FTPUpload(路径)
子FTPUpload(路径)

错误恢复下一个

Const copyType = 16

'FTP等待时间,单位为ms
waitTime = 80000

FTPUser =test
FTPPass = testtest
FTPHost =ftp.test.c om
FTPDir =/ htdocs / test /

strFTP =ftp://& FTPUser& :& FTPPass& @& FTPHost& FTPDir
Set objFTP = oShell.NameSpace(strFTP)


'上传单个文件
如果objFSO.FileExists(path)则

Set objFile = objFSO.getFile(path)
strParent = objFile.ParentFolder
Set objFolder = oShell.NameSpace(strParent)

Set objItem = objFolder.ParseName(objFile.Name)

Wscript.Echo上传档案& objItem.Name& to和 strFTP
objFTP.CopyHere objItem,copyType


结束如果


'上传文件夹
中的所有文件如果objFSO。 FolderExists(path)然后

'整个文件夹
设置objFolder = oShell.NameSpace(路径)

Wscript.Echo上传文件夹&路径& to和 strFTP
objFTP.CopyHere objFolder.Items,copyType

End If


如果Err.Number<> 0然后
Wscript.Echo错误:& Err.Description
End If

'等待上传
Wscript.Sleep waitTime

End Sub


我会很感激任何形式的帮助,谢谢。

解决方案

不知道这是否是最好的方法,但可以通过 FTP 命令获取文件列表,并将结果与​​预期结果进行比较。这里有一个例子:

pre code创建FTP命令文件
使用CreateObject(Scripting.FileSystemObject) .CreateTextFile(c:\ ftp.txt,True)
.WriteLineUSER test
.WriteLinetesttest
.WriteLinels / htdocs / test /
.WriteLinequit
.Close
End With

'运行该命令并捕获其输出...
使用CreateObject(WScript.Shell )
.Run%comspec%/ c ftp -n -v -s:c:\ ftp.txt ftp.test.com> c:\filelist.txt,0,True

结尾

这将创建文件 c:\filelist.txt ,然后您可以打开并检查您上传的文件是否存在。当然,您可以将其他参数添加到 ls 命令以获取更详细的信息。例如, ls -l <​​/ code>会给你更新日期以及文件大小。

I'm trying to send files over ftp and afterwards check if the process was completed successfully, if it was I would delete the original files and keep only the ones sent in the destination folder on FTP.

I managed to patch up a script that connects to FTP and sends the files, but I'm not sure how to cross check original folder with the one on the FTP so I could determine if the copy was successful.

This is the code to send files over FTP, and in testing it sends successfully all the files, but I have to check before deletion

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

I would appreciate any kind of help, thank you.

解决方案

Not sure if this is the best way but you can get a file listing via the FTP command and compare the results to what you're expecting. Here's an example:

' 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

This would create the file c:\filelist.txt, which you could then open and check for the existence of the file(s) you've uploaded. Of course, you can add additional args to the ls command to get more detailed info. For example, ls -l would give you the date updated as well as the file size.

这篇关于vbs脚本通过ftp发送文件并检查/删除原始文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆