error-handling - VBscript : Verifying that a file has been completely copied/error handling

标签 error-handling vbscript copy verify

我正在尝试向脚本中添加一些内容,以便让我知道我复制的文件是否已被完全复制。

基本上,我要压缩一堆文件,然后将它们发送到网络上的映射驱动器。然后,一旦文件被成功复制,我将脚本删除原始位置的文件。该脚本可以正常工作,但是我只需要添加一些错误处理,这将使我知道复制是否成功完成。

我从来没有在vbscript中使用过任何错误处理,因为我只用了大约一周的时间,所以我们将不胜感激任何帮助。让我知道是否需要进一步解释。我的脚本可以在下面找到:

Option Explicit


Dim sDirectoryPath, sDestinationPath, sOutputFilename, Shell, sFileExt, sFilePrefix

shell = WScript.CreateObject("WScript.Shell")

'指定要压缩文件的目录路径
'指定压缩文件的目的地
'指定要查找的文件扩展名
'指定要查找的文件名前缀
sDirectoryPath = "C:\Testscripts\"
sDestinationPath = "C:\Script\files\outzips\"
sOutputFilename = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
sFileExt = ".evtx"
sFilePrefix = "Archive*"


Dim Command, RetVal
Dim d : d = Date() 
Dim dateStr : dateStr = Year(d) & "-" & Right("00" & Month(d), 2) & "-" & Right("00" &   Day(d), 2)
Dim t : t = Time()
Dim timeStr: timeStr = Hour(t) & "-" & Right("00" & Minute(t), 2) & "-" & Right("00" & Second(t), 2)



Command = """C:\Program Files\7-zip\7z.exe"" a  " & sDestinationPath & sOutputFilename & "-" & dateStr & "-" & timeStr & ".zip " & sDirectoryPath & sFilePrefix & sFileExt


        RetVal = Shell.Run(Command,0,true)

Wscript.Sleep 2000



Dim objFso  

Set objFSO = CreateObject("Scripting.FileSystemObject")

'将文件从一个路径复制到另一路径
objFSO.CopyFile "C:\script\files\outzips\*.zip" , "G:\CopyTestFolder\"

If err.Number <> 0 Then
WScript.Echo "An error occured copying this file, re-attempt copy"
Else
WScript.Echo "No errors occured, copy successful"
End If
On Error GoTo 0

'成功压缩并复制文件后,指定删除位置
'旧的zip文件,以及要删除的本地存档文件夹路径
objFSO.DeleteFolder("C:\Script") 

'可以删除整个存档文件夹,也可以删除文件夹中的.zip文件
objFSO.DeleteFile("C:\Testscripts\Archive*.evtx") 

'复制成功后需要删除原始文件的位置

最佳答案

使用7-zip上的“t”命令来验证完整性。如果“0”正常,则错误。

例如:

Set myshell = WScript.CreateObject("WScript.Shell")
Dim cmd, result
cmd = """C:\Program Files\7-zip\7z.exe"" t C:\NOT_a__valid_zip_file.zip"
result = myshell.Run(cmd,0,true)
Wscript.Echo "Not a valid zip file:  " & result

cmd = """C:\Program Files\7-zip\7z.exe"" t C:\a_valid_zip_file.zip"
result = myshell.Run(cmd,0,true)
Wscript.Echo "A valid zip file:  " & result

输出:
PS> cscript.exe .\7z.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Not a valid zip file:  2
A valid zip file:  0

关于error-handling - VBscript : Verifying that a file has been completely copied/error handling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11300070/

相关文章:

php - 如何将 Bootstrap 中生成的错误转发到 ErrorController?

php - Twilio忽略PHP库中的错误

vbscript - 这个 ASP 代码可以容纳多大的数字?

oracle - 从 MSDAORA 提供程序更改为 OraOLEDB 提供程序后,颠倒的问号而不是真实字符

C++ 字符串到字节错误

C# File.Copy 到多个网络目标 block 其他线程

php - 在 php 中组织技术和面向用户的错误消息

php - 为什么php不赞成使用的功能(相对于反对使用的功能)不调用error_handler函数?

sql-server - VBS第三方程序(如SQL)中的错误处理

perl - 如何获取草莓perl中最后执行的命令的值