Avatar of santhoshd48
santhoshd48
Flag for India asked on

VB script tat should read the contents of the ZIP file

I want VB script that wil find out the zip files in a computer and should read the files in the ZIP file and gives the output of all the file names with extension., especially *.MP3 files.
Windows BatchVB Script

Avatar of undefined
Last Comment
sungenwang

8/22/2022 - Mon
sungenwang

Here is the script that should accomplish what you are asking.  This will take a while since it goes through the entire disk (In this script it goes through C:.) After it runs, two files will be created in C:\ :
- output.txt  contains all files within each zip file.
- outputMP3.txt contains only mp3 files within each zip file.

However, you MUST have an unzip utility to have this working. In this example, I used unzip.exe which is a freeware downloadable from here:
ftp://tug.ctan.org/tex-archive/tools/zip/info-zip/WIN32
- Select this one: unz552xN.exe

sew

Option Explicit
 
Const strStartPath = "c:\"
Const strDestFile = "c:\output.txt"
Const strDestMP3File = "c:\outputMP3.txt"
 
 
Dim objFSO, objDestFile, objDestMP3File, strTempFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
strTempFolder = CreateTempFolder()
 
Dim strDest, strDestMP3
strDest = ""
strDestMP3 = ""
 
TraverseFolder strStartPath
 
Set objDestFile = objFSO.CreateTextFile(strDestFile, True)
objDestFile.Write(strDest)
objDestFile.Close
 
Set objDestMP3File = objFSO.CreateTextFile(strDestMP3File, True)
objDestMP3File.Write(strDestMP3)
objDestMP3File.Close
 
msgbox "done"
objFSO.DeleteFolder(strTempFolder)
 
 
Sub TraverseFolder(strFolderPath)
	Dim objCurrentFolder, objFile, objFolder
	Set objCurrentFolder = objFSO.GetFolder(strFolderPath)
 
	For Each objFile In objCurrentFolder.Files
		If LCase(objFSO.GetExtensionName(objFile)) = "zip" then
			Call UnZipAndCheckExtension(objFile, strTempFolder)
		End If
	Next
 
	For Each objFolder In objCurrentFolder.subFolders
		TraverseFolder objFolder.ParentFolder & "\" & objFolder.name
	Next
End Sub
 
 
Function UnZipAndCheckExtension(strZipFile, strTempFolder)
	Const strUNZIPSource = "C:\temp\UNZIP.EXE"
 
	Dim objFSO, objShell
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If Not objFSO.FolderExists(strTempFolder) Then objFSO.CreateFolder (strTempFolder)
 
	Set objShell = CreateObject("WScript.shell")
	objShell.Run """" & strUNZIPSource & """ """ & strZipFile & """ -d """ & strTempFolder & """", _
				0, True
	WScript.Sleep 300	' need this to ensure new files shows up
 
	Dim objFolder, objFile
	Set objFolder = objFSO.GetFolder(strTempFolder)
 
	strDest = strDest & "Zip file '" & strZipFile & "' found:" & vbLf
	strDestMP3 = strDestMP3 & "Zip file '" & strZipFile & "' found:" & vbLf
	For Each objFile In objFolder.Files
		strDest = strDest & "->" & objFSO.GetFileName(objFile) & vbLf
		
		If LCase(objFSO.GetExtensionName(objFile)) = "mp3" Then
			strDestMP3 = strDestMP3 & "->" & objFSO.GetFileName(objFile) & vbLf
		End If
		
		objFSO.DeleteFile(objFile)
	Next
End Function
 
 
Function CreateTempFolder()
	Dim objFSO, strTempFile
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	strTempFile = objFSO.GetTempName
	strTempFile = Replace(strTempFile, "." & objFSO.GetExtensionName(strTempFile), "")
	
	Dim objShell
	Set objShell = CreateObject("WScript.Shell")
	CreateTempFolder = Replace(objShell.SpecialFolders("Desktop"), "Desktop", "Local Settings\Temp") & _
					"\" & strTempFile
End Function

Open in new window

santhoshd48

ASKER
IN OUR ENVIRONMENT we are using winzip on all the desktops., C:\Program Files\WinZip this is the path we r installed winzip, tel me the exact file and path i have to give.Could u guide me what and which line i want to edit, and while running script it should not pop up any window..plz help me on this
santhoshd48

ASKER
urgent see the comment above and modify the script accordingly plz
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
sungenwang

Winzip is not as user-friendly to unzip via command line (hiding all windows) as other zip tools. You'll need to download the command line utility from WinZip here:

http://www.winzip.com/prodpagecl.htm

VERY busy right now, will get back to you later today...

sew
sungenwang

After dealing with a few strange system zip files, this script should do what you requested. Again an output file c:\output.txt will have the files inside all zipped files. An output file c:\outputMP3.txt should only contain MP3 files inside Zip files.

This script uses WinZip's command line utility, wzunzip.exe. As long as you have it installed on a PC, the following script should work. If you get any error message returning from wzunzip, please let me know (the script is setup to display winzip error message, if any). The script is default to check the entire C: drive.

sew

Option Explicit
 
Const strStartPath = "C:\"
Const strDestFile = "c:\output.txt"
Const strDestMP3File = "c:\outputMP3.txt"
 
 
Dim objFSO, strTempFolder, objDestFile, objDestMP3File
Set objFSO = CreateObject("Scripting.FileSystemObject")
strTempFolder = CreateTempFolder()
 
Dim strDest, strDestMP3
strDest = ""
strDestMP3 = ""
 
If objFSO.FileExists(strDestFile) Then objFSO.DeleteFile(strDestFile)
If objFSO.FileExists(strDestMP3File) Then objFSO.DeleteFile(strDestMP3File)
 
TraverseFolder strStartPath
 
msgbox "done"
objFSO.DeleteFolder(strTempFolder)
 
 
 
Sub TraverseFolder(strFolderPath)
	Dim objCurrentFolder, objFile, objFolder
	Set objCurrentFolder = objFSO.GetFolder(strFolderPath)
 
	On Error Resume Next
 
	For Each objFile In objCurrentFolder.Files
		If Not Err Then
			If LCase(objFSO.GetExtensionName(objFile)) = "zip" then
				Call UnZipAndCheckExtension(objFile, strTempFolder)
			End If
		Else
			Err.Clear
		End If
	Next
 
	For Each objFolder In objCurrentFolder.subFolders
		If Not Err Then
			TraverseFolder objFolder.ParentFolder & "\" & objFolder.name
		Else
			Err.Clear
		End If
	Next
	
	On Error Goto 0
End Sub
 
 
Function UnZipAndCheckExtension(strZipFile, strTempFolder)
	Const strUNZIPSource = "WZUNZIP.EXE"
 
	Dim objFSO, objShell, intRet
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If Not objFSO.FolderExists(strTempFolder) Then objFSO.CreateFolder (strTempFolder)
 
	Set objShell = CreateObject("WScript.shell")
	
	On Error Resume Next	
	intRet = objShell.Run("""" & strUNZIPSource & """ -o """ & strZipFile & """ """ & strTempFolder & """", _
				0, True)
	If Err Then
		On Error Goto 0
		msgbox strZipFile & vbLf & Err.Description
		Exit Function
	End If
	On Error Goto 0
 
	Dim objFolder, objFile
	Set objFolder = objFSO.GetFolder(strTempFolder)
 
	strDest = "The following files are found in '" & strZipFile & "':" & vbLf
	strDestMP3 = "The following MP3 files are found in '" & strZipFile & "':" & vbLf
 
	For Each objFile In objFolder.Files
		strDest = strDest & "->" & objFSO.GetFileName(objFile) & vbLf
		
		If LCase(objFSO.GetExtensionName(objFile)) = "mp3" Then
			strDestMP3 = strDestMP3 & "->" & objFSO.GetFileName(objFile) & vbLf
		End If
		
		objFSO.DeleteFile(objFile)
	Next
 
	Set objDestFile = objFSO.OpenTextFile(strDestFile, 8, True)
	objDestFile.Write(strDest)
	objDestFile.Close
 
	Set objDestMP3File = objFSO.OpenTextFile(strDestMP3File, 8, True)
	objDestMP3File.Write(strDestMP3)
	objDestMP3File.Close
End Function
 
 
Function CreateTempFolder()
	Dim objFSO, strTempFile
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	strTempFile = objFSO.GetTempName
	strTempFile = Replace(strTempFile, "." & objFSO.GetExtensionName(strTempFile), "")
	
	Dim objShell
	Set objShell = CreateObject("WScript.Shell")
	CreateTempFolder = Replace(objShell.SpecialFolders("Desktop"), "Desktop", "Local Settings\Temp") & _
					"\" & strTempFile
End Function

Open in new window

santhoshd48

ASKER
wzunzip.exe install automatically while running the winzip setup or we have to install separately
where can i find the wzunzip.exe for download., in our office we are using winzip 9.0 SR-1 version.,
help me on this...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sungenwang

santhoshd48,

You can download the command-line utitlity from WinZip's page:
http://www.winzip.com/prodpagecl.htm

There are a lot of free unzipping software out there, but if you must use winzip 9.0 I don't know if it'll run wzunzip.exe. You can give it a try at least. Of course you may need to upgrade to 11.0 to run it...

The script above will work with most, if not all, unzipping software but your option is limited if you must do it with winzip.

sew
sungenwang

If you try to download wzunzip.exe for version 10.0, it may just work on your system... not sure but worth a try!

http://www.winzip.com/downcl.htm
santhoshd48

ASKER
the above script is working.,
but still i want to get the *.avi, *.wav etc what are the changes i have to made.,
and after finishing the script., the popup window appears to click ok., how to stop this
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
sungenwang

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.