Today I'm searching a method to compress a file with Window Zip tool in
my cpp project, I searched many links and found many related
information. However, there remained a question...
In this message:
I got to know use VBScript can call zip funtion. I wonder whether I can
call zip funtion in C++ directly? If not, can I call VBScript
funtion(with input parameters and return value) in C++., and how?
I aslo try this to call VBScript but failed to excute my vbs, I don't
know where the problem is.. :(
http://www.codeproject.com/com/scriptdemo.asp
Any links or advices are appreiated. Thanks in advance.
-- This is the vbs which works in my operation system:
Option Explicit
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim MySource, MyTarget, MyZipName, MyHex, MyBinary, i
Dim oShell, oApp, oFolder, oCTF, oFile
Dim oFileSys
MySource = "C:\testfile\test.log"
MyTarget = "d:\temp\SinkFolder.zip"
MyHex = Array(80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0)
For i = 0 To UBound(MyHex)
MyBinary = MyBinary & Chr(MyHex(i))
Next
Set oShell = CreateObject("WScript.Shell")
Set oFileSys = CreateObject("Scripting.FileSystemObject")
'Create the basis of a zip file.
Set oCTF = oFileSys.CreateTextFile(MyTarget, True)
oCTF.Write MyBinary
oCTF.Close
Set oCTF = Nothing
Set oApp = CreateObject("Shell.Application")
'Copy the files to the compressed folder
'If Not MySource Is Nothing Then
oApp.NameSpace(MyTarget).CopyHere MySource
'End If
'Wait for compressing to begin, this was necessary on my machine
wScript.Sleep(5000)
'wait for lock to release
Set oFile = Nothing
On Error Resume Next
Do While (oFile Is Nothing)
'Attempt to open the file, this causes an Err 70,
Permission Denied when the file is already open
Set oFile = oFileSys.OpenTextFile(MyTarget, ForAppending,
False)
If Err.number <> 0 then
Err.Clear
wScript.Sleep 3000
End If
Loop
Set oFile=Nothing
Set oFileSys=Nothing
Thank you for your advice, unfortunately, I can't open the link right
now..(might because of the Earthquake in Taiwan, damaged an undersea
cable, currently many websites couldn't be opened or very slow..)
With someone's help, I can call VBScript sucessfully now, Now there was
another question: similar as following one I searched in google:
"So, I'm working on a VBScript to run on our ...servers which will
backup ... on some log files. Part of the script uses the
"oApp.NameSpace(pTarget).CopyHere pSource" method to copy the files to
another location. The problem is, sometimes there may only be 20MB to
copy, sometimes 1.5GB. I would like the script to wait until the
CopyFolder method completes before continuing with processing, without
using a wscript.sleep() call - I don't know why my application check
wScript.Sleep(5000) as INVALID"
In Summary, so there are three questions:
1. How to get the status of a method(CopyHere), and return the result
value until the method is finished;
2. I also want the application method run in a silent mode(without a
window appearing) I use "FOF_SILENT' but it doesn't work? Also I want
without confirmation if file already exist...
oApp.NameSpace(pTarget).CopyHere pSource,
FOF_NOCONFIRMATION+FOF_SILENT
3. I aslo want to know is it safe to use Windows ZIP with VBSript in
this way? Are there some limitation(for example: file size is very
large, or there are many files...?)
Thanks in advance.
--- Following script is available:
function waitfor(secs)
dim start
start = timer()
do until timer() >= start + secs
loop
end function
Function CreateZip(pSource, pTarget)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const FOF_NOCONFIRMATION = &H10
Const FOF_SILENT = &H4
Dim MyZipName, MyHex, MyBinary, i
Dim oShell, oApp, oFolder, oCTF, oFile
Dim oFileSys
MyHex = Array(80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0)
For i = 0 To UBound(MyHex)
MyBinary = MyBinary & Chr(MyHex(i))
Next
Set oShell = CreateObject("WScript.Shell")
Set oFileSys = CreateObject("Scripting.FileSystemObject")
'Create the basis of a zip file.
Set oCTF = oFileSys.CreateTextFile(pTarget, True)
oCTF.Write MyBinary
oCTF.Close
Set oCTF = Nothing
Set oApp = CreateObject("Shell.Application")
'Copy the files to the compressed folder
'If Not pSource Is Nothing Then
oApp.NameSpace(pTarget).CopyHere pSource,
FOF_NOCONFIRMATION+FOF_SILENT
'End If
'Wait for compressing to begin, this was necessary on my machine
waitfor(200)
'wScript.Sleep(5000) ------ can't used in my
applistion
'wait for lock to release
Set oFile = Nothing
On Error Resume Next
Do While (oFile Is Nothing)
'Attempt to open the file, this causes an Err 70,
Permission Denied when the file is already open
Set oFile = oFileSys.OpenTextFile(pTarget, ForAppending, False)
If Err.number <> 0 then
Err.Clear
'wScript.Sleep 3000
waitfor(200)
End If
Loop
Set oFile=Nothing
Set oFileSys=Nothing
CreateZip = True
End Function
"Paolo 写道: