|
本帖最后由 todaynew 于 2015-5-21 16:55 编辑
你光放一个空p当然不行。
你放个空p,意思就是打算放p,于是RAR就跳出对话框问你放个什么p,所以这是正常反应。如果你在p后面写密码,RAR就知道你的p是个什么调调,也就不会弹出对话框再问你什么了。
更通用的搞法可以这样:
Function RarA(ByVal RAR As String, ByVal RARname As String, ByVal filname As String, Optional password As String)
'功能:压缩文件
'参数:RAR --RAR地址
' RARname---压缩文件名(含路径)
' filname---被压缩的文件名或文件夹名(含路径)
'示例:压缩文件 "C:\Program Files\WinRAR\WinRAR.exe",CurrentProject.Path & "\备份\后台备份.rar",CurrentProject.Path & "\备份\*.mdb","123"
Dim cmd As String
If Nz(password, "") = "" Then
cmd = " a -r -ep -df "
Else
cmd = " a -r -ep -df -p{0} "
cmd = ReplaceValues(cmd, password)
End If
Call Shell(RAR & cmd & """" & RARname & """ """ & filname & """", vbNormalFocus)
End Function
Function RarX(ByVal RAR As String, ByVal RARname As String, ByVal fldname As String, Optional password As String)
'功能:解压文件
'参数:RAR --RAR地址
' RARname---压缩文件名(含路径)
' fldname---解压的文件夹名(含路径)
'示例:压缩文件 "C:\Program Files\WinRAR\WinRAR.exe",CurrentProject.Path & "\备份\后台备份.rar",CurrentProject.Path & "\备份\*.mdb","123"
Dim cmd As String
If Nz(password, "") = "" Then
cmd = " x "
Else
cmd = " x -p{0} "
cmd = ReplaceValues(cmd, password)
End If
Call Shell(RAR & cmd & """" & RARname & """ *.* """ & fldname & """", vbNormalFocus)
End Function
Public Function ReplaceValues(ByVal str As String, ParamArray pArr() As Variant) As String
'功能:模仿.Net中的String.Format方法
Dim str1 As String
Dim num As String
Dim i As Integer
str1 = str
For i = 0 To UBound(pArr)
num = "{" & i & "}"
str1 = Replace(str1, num, pArr(i))
Next
ReplaceValues = str1
End Function
|
|