VBScript to ZIP files into a ZIP file

Hello

I need to use VBScript to create a new zip file and zip into it a load of photos.
I am using Windows 2003 Server and do not wish to use winzip unless I have to..

Please advise..

Derek Jee
DelboyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shanmuga SundaramDirector of Software EngineeringCommented:
I dont know why you are not interested in using winzip. but this code will help you if you make up your mind to use winzip

http://www.naterice.com/blog/template_permalink.asp?id=47

Or else you should you should use some other third party dll's to fulfil you need. Below is the list of sites that provide the vbscript code for zipping using the relavant dlls.

This uses XStandard's .
http://www.tek-tips.com/viewthread.cfm?qid=1231429&page=1
some other links
http://files.filefront.com/VBScriptzip/;8833941;/fileinfo.html
http://www.robvanderwoude.com/vbstech_files_zip.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DelboyAuthor Commented:
Hi Shasunder

Thanks for the quick reply..  I will have a look at your suggestions shortly..  the reason I didn't wanto use WinZip is that I have to run an automated process after the zip has been completed and was not sure if I could control that within my script..  my process will carry on to upload the zip file to a website and want to make sure the zip file is complete before it uploads..

Thanks again..


derek.
0
sr75Commented:
I have used a program called 7zip to zip files via a command line which can be scripted.  I have attached the script I used to perform this.  Although now I am a much more stronger scripter and would change the way I  waited for the zip to finish.

I used a hold down loop instead of just using which would do the same thing and allow me to log any output from the command.

strExFold = "C:\Temp_Archive\*"
strZipName = "Z:\" & strDate & "_Twister2.zip"
strCommand = "7z a -tzip "

set WShell = CreateObject("WScript.Shell")
set objExec = WShell.Exec(strCommand & strZipName & " " & strExFold)
strZipResults = LCase(objExec.StdOut.ReadAll)
'########################################################################################
'#											#
'#	Description:	This script moves the contents from two folders to their	#
'#			corresponding "History" folder.  It will copy the contents	#
'#			twice before finally deleting them.  This is to ensure that 	#
'#			every file has been copied over.				#
'#											#
'#	Modified:	This Script has been modified to work with the 7zip utility.	#
'#			It will copy off the files in Monthly chunks and zip them.	#
'#			When the script completes, it will delete out any temp folder 	#
'#			and will reboot the system					#
'#											#
'#	Req Software:	7zip version 4.4.2						#
'#			Shutdown.exe (should be in C:\WINDOWS\System32)			#
'#											#
'#	Notes:		This script was written to be easily modified for other similar #
'#			tasks.  However, each folder that needs to be cleaned out	#
'#			requires a corresponding folder in the "arrTo" array.		#
'#			This script has been modified to use several Sub functions.	#
'#											#
'#				Sub Function		Description			#
'#			---------------------------------------------------------------	#
'#			SubCheckNewFolder()	This checks to see if "New Folder"	#
'#						exists under the "Export History".	#
'#						Currently this folder hold additional	#
'#						Files and they need to be moved before	#
'#						any more processing can take place.	#
'#											#
'#			SubNetMap()		This will temporarily map the Z: drive	#
'#						to the \\LabSrv\Twister2_Archive$ net	#
'#						share.  It will be disconnected during	#
'#						the script clean up.			#
'#											#
'#			SubCreateFolder()	This Func takes the parameters from 	#
'#						the arrFold array and creates folders	#
'#						if they do not already exists.  This	#
'#						includes the Temporary folders as well.	#
'#											#
'#			SubMoveData()		This Func actually is two subfunctions. #
'#						The first will move the data from the 	#
'#						two working directories (Export and 	#
'#						Samples) and places them in their 	#
'#						corresponding "History" folders.  Then	#
'#						The sub function will move the data to	#
'#						a temporary folder in monthly chunks.	#
'#						After this is done, it will call the 	#
'#						SubZip Function to zip the temporary	#
'#						folders to archival status on the 	#
'#						network.				#
'#											#
'#			SubZip()		This func calls the 7zip executable via #
'#						the cmd shell and zips all the info in	#
'#						the temporary archive folder. 7zip	#
'#						needs to be installed and the exe copied#
'#						to C:\WINDOWS\System32 folder.		#
'#											#
'#			SubHoldDown()		WSH (vbscript) moves on after calling	#
'#						the command shell causing it SubZip() 	#
'#						to fail.  TO prevent this, SubHoldDown	#
'#						was written to watch the 7z.exe process	#
'#						from the WMI Win32_Process object. When	#
'#						the process has finished, then it will	#
'#						release the script to continue on	#
'#											#
'#			SubReboot()		This will reboot the PC.  This happens 	#
'#						at the end of the script to sever the 	#
'#						connection between the PC and the 	#
'#						TWISTER II machine.			#
'#											#
'########################################################################################
'---------------------------------------------------------------------------
'
'				Main Function
'
'---------------------------------------------------------------------------
On Error Resume Next
StrComputer = "."
 
 
'================================
'      Set Script Variables
'================================
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WShell = WScript.CreateObject("Wscript.Shell")
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set Net = WScript.CreateObject("WScript.Network")
 
x = 0
z = 0
 
 
'================================
'   Create an Array of FOlders
'================================
arrFold = array("C:\Temp_Archive",_
		"C:\Export",_
		"C:\Samples",_
		"C:\Export History",_
		"C:\Samples History",_
		"C:\Temp_Archive\Export History",_
		"C:\Temp_Archive\Samples History")
 
 
'================================
' Loop the Array & create Folders
'================================
Do until x > ubound(arrFold)
	strFolder = arrFold(x)
	SubCreateFolder(strFolder)
	x = x +1
Loop
 
SubCheckNewFolder()
SubNetMap()
 
 
'================================
'  Loop the Array to Move Data
'================================
Do Until y > 4
	strFrom = arrFold(y)
	strTo = arrFold(y +2)
	SubMoveData strFrom, strTo
	y = y +1
Loop
 
 
'================================
'     Finish up the Script
'================================
FSO.DeleteFolder(arrFold(0))
Net.RemoveNetworkDrive "Z:"
 
subReboot()
 
 
'---------------------------------------------------------------------------
'
'				Sub Functions
'
'---------------------------------------------------------------------------
'================================
'     Test and Create Folders
'================================
Sub  SubCreateFolder(strFolder)
	If Not FSO.FolderExists(strFolder) then
		strFolder = FSO.CreateFolder(strFolder)
	End If
End Sub
 
 
'================================
'    Temp. Map Archive Folder
'================================
Sub SubNetMap()
	Net.MapNetworkDrive "Z:", "\\Server\Archive"
End Sub
 
 
'================================
'          Move the Data
'================================
Sub SubMoveData(strFrom, strTo)
	Dim objFolder
	Dim colFiles
	Dim objFile
 
 
	'================================
	'       Set Sub Variables
	'================================
	Set objFolder = FSO.GetFolder(strFrom)
	Set colFiles = objFolder.Files
	boolHist = False
	Cnt = 0
 
	'================================
	'          Move the Data
	'================================
	If Right(ucase(strFrom),7) <> "HISTORY" then
		x = 0
		Do until x = 2
			For Each objFile in colFiles
				objFile.Copy(strTo & "\")
				If x = 1 then
					objFile.Delete()
				End If
			Next
			x = x +1
		Loop
 
	'================================
	'     Create and Test Dates
	'================================
	Else
		Do Until boolHist = True
			For Each objFile in colFiles
				DateMod = FormatDateTime(objFile.DateLastModified, VBShortDate)
				if DateFile = "" then
					DateFile = DateMod
				End If
				FileDate = DateDiff("m",DateMod,DateFile)
				DiffDate = DateDiff("m",Now(),DateMod)
 
				If DiffDate = -1 or DiffDate = 0 then
					boolHist = True
				Else 
					If FileDate = "0" then
						objFile.Move(strTo & "\")
					Else
			   			SubZip(DateFile)
						objFile.Move(strTo & "\")
					End If
				End If
				DateFile = DateMod
			Next
			If Cnt = 4 then
				boolHist = True
			End If
			Cnt = Cnt +1		
		Loop
	End If
 
	SubZip(DateFile)
	
	Set objFile = nothing
	Set colFiles = nothing
	set objFolder = nothing
End Sub
 
 
'================================
'          Zip the Data
'================================
Sub SubZip(DateFile)
	StrYear = Year(DateFile)
	StrMonth = Month(DateFile)
	If len(strMonth) < 2 then
		strMonth = "0" & strMonth
	End If
	strDate = strYear & "-" & strMonth
 
	strExFold = "C:\Temp_Archive\*"
	strZipName = "Z:\" & strDate & "_Twister2.zip"
	strCommand = "7z a -tzip "
		
	WShell.Run strCommand & strZipName & " " & strExFold
 
	SubHoldDown()
 
	FSO.DeleteFolder("C:\Temp_Archive")
	FSO.CreateFolder("C:\Temp_Archive")
	FSO.CreateFolder("C:\Temp_Archive\Export History")
	FSO.CreateFolder("C:\Temp_Archive\Samples History")
End Sub
 
 
'================================
'          Wait for Zip
'================================
Sub SubHoldDown()
	Bool = True
        Do until bool = False
    		wscript.sleep 500
	     	Bool = False
	     	Set ColProc = objWMI.ExecQuery("Select * from Win32_Process")
 	     	For Each objProc in ColProc
			If left(objProc.name, 2) = "7z" then
				bool = true
 			End If
	     	Next
             	Set ColProc = Nothing
  	Loop
End Sub
 
 
'================================
'         Reboot the PC
'================================
Sub SubReboot()
	strShutDown = "C:\WINDOWS\System32\Shutdown.exe"
	strReboot = strShutDown & " /r /m \\127.0.0.1 /f"
        intRetVal = WShell.Run(strReboot, 1, True)
 
end Sub
 
 
'================================
'       Check For New Folder
'================================
Sub SubCheckNewFolder()
	Cnt = 0
	Do until Cnt = 2
		FromFold = "C:\Export History\New Folder"
		ToFold = "C:\Export History"
		If FSO.FolderExists(FromFold) then
			FSO.CopyFile FromFold & "\*.*", ToFold
			If CNT = 1 then
				FSO.DeleteFolder(FromFold)
			End If
		End If
		Cnt = Cnt +1
	Loop
End Sub

Open in new window

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Shanmuga SundaramDirector of Software EngineeringCommented:
This link has one script file. when downloaded and viewed you can find the code that uses winzip to zip and it also has code for backup. If not required ignore the backup part.

http://webscripts.softpedia.com/scriptDownload/Free-VBScript-Backup-Software-Script-Download-28700.html
0
Shanmuga SundaramDirector of Software EngineeringCommented:
I am using winzip 10. I downloaded winzip commandline addon from
http://www.winzip.com/prodpagecl.htm
for free. In my computer winzip is installed in "D" drive program files directory. and the below given vbscript is used to zip the 123.xml file present in c drive and store in the dd.zip file.
dim ShellObj
set ShellObj = WScript.CreateObject("WScript.Shell")
str= """d:\Program Files\WinZip\wzzip.EXE"" ""C:\dd.zip"" C:\123.xml"
ShellObj.run str,,true

Open in new window

0
DelboyAuthor Commented:
Hi Shasunder   I followed the X-Zip way and found it perfect..  like WinZip I am sure except I do not have to pay for it first..  many thanks for your help..


Derek.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.