Using VBScript to examine properties of files within a zip file - vbscript

I'm trying to use VBScript to examine the contents of several hundred .zip files. Essentially what I want to do is run through each .zip and find all of the files wihtin that zip file. For each one of these files within the zip, I want to record some information about it to an Oracle database. That information being: file name and file modified date.
So far, my solution has been extracting each zips folder structure to a temp folder then running through the temp folder with an fso object. However, this has been proven to be very slow.
Is there a way to accoplish this without unziping the zip files?

Ouch man. I have never heard of vbscript zip object. But it has been a long time since I have done vbscript. Is there anyway you can avoid it?
I did some googling for you. I did find this: http://www.example-code.com/vbscript/zip_List.asp Chilkat has done a lot of stuff I thought not possible. This gives me the impression - that what you are trying to do is not going to be painless.
If given the problem you have I would find a different solution than vbscript. But if you pull-it-off I would vote for you to be mayor of vb land

You can do it in place with Shell Objects. But it will be just as slow, maybe. If just name and date Explorer may get it direct from the zip directory (at the end of the file so the whole file still needs to be read).
This copies items in a folder to another folder. A zip file is a folder so it will copy in and copy out.
To Zip
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(Ag(1))
Set DestFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
To Unzip (note SrcFolder and DestFolder are reversed)
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set DestFldr=objShell.NameSpace(Ag(1))
Set SrcFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
To Create a blank zip. (I should have used an ADODB binary stream rather than an FSO text stream, but it shouldn't matter)
Set Ag=Wscript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Ag(0), 8, vbtrue)
BlankZip = "PK" & Chr(5) & Chr(6)
For x = 0 to 17
BlankZip = BlankZip & Chr(0)
Next
ts.Write BlankZip

Related

File wont move to folder vbs

So im writing a script that drops a file folder then moves that file to the folder it dropped it self. Well the folder drops fine but the file wont move. Can some see whats wrong with my code? Or give me a better way to move the file. I also get no error message about trying to move the file.
Dim folder,fso,filsys,C
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Set wshshell = CreateObject("wscript.shell")
Set filesys = CreateObject("scripting.filesystemobject")
Set objfso = CreateObject("Scripting.filesystemObject")
Set c = fso.GetFile(Wscript.scriptFullname)
On Error Resume NEXT
Set objFolder = objFSO.CreateFolder("C:\55egr932ntn7mk23n124kv1053bmoss5")
If Err.Number<>0 Then
End If
WScript.Sleep 3000
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
And I have a program I use that turns the VBS into and EXE so you see the "file.exe" which really is the .VBS itself
I'm not familiar with this syntax, but the line below looks like it's expecting the folder variable to be a string.
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
Earlier in code it looks as though you're setting folder as an object.
Set folder = fso.GetSpecialFolder(1)
You might not get the error you mentioned in your comment if you convert folder to a string.
~~
Another thing to try is the following code:
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Alert (folder&"\File.exe")
(I'm not sure if it's "Alert" or "Msgbox" or something else.) That test will show you whether the file path makes sense. If you get an error on line 3 of that test, try converting folder to a string before your Alert (or Msgbox).

How to delete files from zip with VBScript

Im very new to VBScript (this is my first time ever using it). So far I've copied and altered my way into getting as far as I am.
I have an APK file that is too big for my needs. So what I've been doing is manually changing it to a zip and then deleting a couple images from it then renaming it back to an APK. Im trying to automate that with a VBScript. So far I have
Set oShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = WORKSPACE & "\temp\" & app & "\build\" & PLATFORMSUBFOLDER & "\dist\" & app & "\bin"
oShell.CurrentDirectory = sFolder
'Make the Apk a Zip.
fso.MoveFile apkfile, zipApk
This is all working and I can see in Windows Explorer that the APK changes to a zip like I want it to. So Im wondering if there is any quick ways to just go in and delete a couple files without extracting the whole thing?
If not is there an easy way to extract the files and parse them at the same time?
Ive looked here Extract files from ZIP file with VBScript
but cant seem to get it working. I keep getting error "Object required: 'objShell.Names(...)'" Any hints to why this is happening?
Use the MoveHere method to move an item out of the zip file:
zipfile = "C:\path\to\your.zip"
fname = "some.file"
dst = "C:\some\folder"
Set app = CreateObject("Shell.Application")
For Each f In app.NameSpace(zipfile).Items
If f.Name = fname Then
app.Namespace(dst).MoveHere(f)
End If
Next
Then delete the files from the dst folder:
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile fso.BuildPath(dst, fname), True

VB script - search for a file in directory

I am trying to write a VB script (having never attempted before) - I need it to search the folder'\file001\source$' - whilst in the folder search for all 'Update.exe'files - If this is done manually, in Windows it takes a long long time!
I would like all the files that it finds with this name - to be copied into a new folder.
Looking at various help forums I am getting more and more confused.
Below is what I have attempted:
Set fso = CreateObject("Scripting.FileSystemObject")
ShowSubfolders fso.GetFolder("\\file001\source$")
'foldername = "\file001\source$"
'filename = "Updater.exe"
Function ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Wscript.Echo Subfolder.Path
ShowSubFolders Subfolder
Next
End Function
This is to search through a folder, recursively through the folders sub folders to find all files with this name.
I have also done research into -directory.getfiles. But have no clue if this is the right direction.
As a newbie to VB script, I have researched and attempted to play around with vb script, to get the function I desire. I would be grateful to any help I can get.
Again - my target is to - find all files within the given folder and subfolders with the name update.exe - and then to copy these files found into a new folder.
Thank you in advance.
If you only want to check the content of a single folder for the existence of a particular file you can do that like this:
Set fso = CreateObject("Scripting.FileSystemObject")
foldername = "\\file001\source$"
filename = "Update.exe"
If fso.FileExists(fso.BuildPath(foldername, filename)) Then
WScript.Echo filename & " exists."
End If
If you want to check the subfolders of foldername as well, you need to recurse into subfolders with something like this. You can either integrate the check from the above code sample in the loop over the subfolders, or add another loop over the files in the folder:
Set fso = CreateObject("Scripting.FileSystemObject")
CopyUpdater fso.GetFolder("\\file001\source$")
Sub CopyUpdater(fldr)
For Each f In fldr.Files
If LCase(f.Name) = "update.exe" Then
'copy file to somewhere else
End If
Next
For Each sf In fldr.SubFolders
CopyUpdater sf
Next
End Sub
See my question here, i benchmark three languages (vbscript also) which do a subdirectory traversal with full working samples and optimised for the language. benchmarks: does python have a faster way of walking a network folder?
Thats a good attempt . Read more on below link and understand things better .
Vbscript list all PDF files in folder and subfolders
VBScript to traverse through subdirectories
dim sFilename
Dim objDict
Set objDict=CreateObject("Scripting.Dictionary")
sFilename = ""
'root folder path where subfolder exists
fileLocation="C:\Users\u258251\Desktop\TestSubfolder"
Dim objFSO 'File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Add all files with specific extention to dictonary
Call Recurse(fileLocation)
ItemArray = objDict.Items
'Loop through dictonary
For i = 0 To objDict.count -1
sFilename = sFilename & ItemArray(i) & VBCRLF
Next
msgbox(sFilename)
'find a specific file by name and return path
if objDict.Exists("DP103.txt") then
msgbox(objDict.Item("DP103.txt"))
end if
Sub Recurse(strFolderPath)
Dim objFolder
Set objFolder = objFSO.GetFolder(strFolderPath)
Dim objFile
Dim objSubFolder
For Each objFile In objFolder.Files
If (InStr(objFile.Name, ".") > 0) Then
'proceed if extention is .txt
If (LCase(Mid(objFile.Name, InStrRev(objFile.Name, "."))) = ".txt") Then
if objDict.Exists(objFile.Name)=false then
'add files and path to dictonary
objDict.Add objFile.Name,objfile.Path
End if
End if
End If
Next
For Each objSubFolder In objFolder.SubFolders
Call Recurse(objSubFolder.Path)
Next
End Sub

Using VB6 + WSH with Windows Compression

Having trouble with WSH and Windows Compression.
My goal is to be able to zip up files (not folders, but individual files from various locations, which I have stored in an array) using the built-in Windows Compression. I am using VB6.
Here is my routine (vb6 code):
Dim objShell
Dim objFolder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.namespace(savePath & "\export.zip")
' --
' loop through array holding files to zip
For i = 0 To filePointer
objFolder.CopyHere (filesToZip(i))
Next
' --
Set objShell = Nothing
Set objFolder = Nothing
It works, but issues arise when there are more than a few files. I start getting errors from Windows (presumably, its calling the compression too fast, and the zip file is locked). I cant seem to figure out how to WAIT until the COPYHERE function completes before calling the next one to avoid issues.
Does anyone have any experience with this?
Thanks -
You should be able to achieve that sort of synchronization by checking the file count in your target ZIP folder before proceeding to the next loop iteration (as suggested here and here):
For i = 0 To filePointer
objFolder.CopyHere filesToZip(i)
Do Until objFolder.Items.Count = i+1
WScript.Sleep 100
Loop
Next

How to unzip a file in VBScript using internal Windows XP options in

I want to unzip a .zip file using VBScript, only it's always a new computer with no external applications on it. Now I know Windows XP and 2003 have an internal .zip folder option, so I guess I can use it via VBScript in order to extract the file.
How do I do it?
I tried:
Set objShell = CreateObject("Shell.Application")
Set SrcFldr = objShell.NameSpace(fileName)
Set DestFldr = objShell.NameSpace(appDir)
DestFldr.CopyHere(SrcFldr)
Which didn't work.
What could be the problem?
Just set ZipFile = The location of the zip file, and ExtractTo = to the location the zip file should be extracted to.
'The location of the zip file.
ZipFile="C:\Test.Zip"
'The folder the contents should be extracted to.
ExtractTo="C:\Test\"
'If the extraction location does not exist create it.
Set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(ExtractTo) Then
fso.CreateFolder(ExtractTo)
End If
'Extract the contants of the zip file.
set objShell = CreateObject("Shell.Application")
set FilesInZip=objShell.NameSpace(ZipFile).items
objShell.NameSpace(ExtractTo).CopyHere(FilesInZip)
Set fso = Nothing
Set objShell = Nothing

Resources