您的位置:首页 > 编程语言 > Qt开发

QTP通过Winrar或者纯VBS代码压缩指定的文件或文件夹

2010-08-24 18:37 531 查看
1,利用QTP中SystemUtil.Run的功能,将指定文件压缩成RAR格式。

代码如下:

Function CompressFile(soursefile,targetfile)
On error resume next
CompressFile = false
m1 = chr(34) &targetfile& chr(34)
m2 = chr(34) & soursefile& chr(34)
command = "a -EP" & " " & m1 & " " & m2
SystemUtil.Run "winrar.exe",command
Set fso = CreateObject("Scripting.FileSystemObject")
For i = 0 to 15
If  not fso.FileExists(targetfile) Then
wait(2)
else
CompressFile = True
Exit for
End If
Next
Set fso = Nothing
End Function

CompressFile "c:/attachment.txt","c:/attachment.rar"


2,利用VBS:'Zip File or folder 'Example:Zip "C:\temp", "C:\Temp.zip"

Function ZipFile(ByVal mySourceDir, ByVal myZipFile)
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.GetExtensionName(myZipFile) <> "zip" Then
Exit Sub
ElseIf fso.FolderExists(mySourceDir) Then
FType = "Folder"
ElseIf fso.FileExists(mySourceDir) Then
FType = "File"
FileName = fso.GetFileName(mySourceDir)
FolderPath = Left(mySourceDir, Len(mySourceDir) - Len(FileName))
Else
Exit Sub
End If
Set f = fso.CreateTextFile(myZipFile, True)
f.Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
f.Close
Set objShell = CreateObject("Shell.Application")
Select Case Ftype
Case "Folder"
Set objSource = objShell.NameSpace(mySourceDir)
Set objFolderItem = objSource.Items()
Case "File"
Set objSource = objShell.NameSpace(FolderPath)
Set objFolderItem = objSource.ParseName(FileName)
End Select
Set objTarget = objShell.NameSpace(myZipFile)
intOptions = 256
objTarget.CopyHere objFolderItem, intOptions
Do
wait 1
Loop Until objTarget.Items.Count > 0
End Function
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: