Search code examples
vbscriptunzip

unzip file silently vbscript


I found online script that basically unzip every .zip archive in a given path.

sub UnzipAll(path)

set folder = fso.GetFolder(path)

for each file in folder.files

    if (fso.GetExtensionName(file.path)) = "zip" then

        set objShell = CreateObject("Shell.Application")

        objshell.NameSpace(path).CopyHere objshell.NameSpace(file.path).Items

        file.delete

    end if

next

end sub

This is actually working, but the problem is that I want to unzip "silently" (silently means that I don't want any kind of message from the system when unzipping, like "do you want to overwrite?" ect.).

I've searched a lot on google and I found that you just need to add a few flags on the "CopyHere" method, like this:

objshell.NameSpace(path).CopyHere objshell.NameSpace(file.path).Items, *FLAGHERE*

But the problem is right here. The flags would normally work, but they are completely ignored when unzipping a .zip archive.

So I searched for a workaround, but I didn't find anything helpful.


Solution

  • I managed to do it by myself. Basically you want to unzip 1 file per time and not everyone togheter, and before copying it you just check if it already exists, and evenutally delete it:

    set fso = CreateObject("Scripting.FileSystemObject")
    
    
    sub estrai(percorso)
    
    set cartella = fso.GetFolder(percorso)
    
    for each file in cartella.files
    
    
        if fso.GetExtensionName(file.path) = "zip" then
    
    
            set objShell = CreateObject("Shell.Application")
    
            set destinazione = objShell.NameSpace(percorso)
    
            set zip_content = objShell.NameSpace(file.path).Items   
    
            for i = 0 to zip_content.count-1
    
                'msgbox fso.Buildpath(percorso,zip_content.item(i).name)+"."+fso.getExtensionName(zip_content.item(i).path)
    
                if (fso.FileExists(fso.Buildpath(percorso,zip_content.item(i).name)+"."+fso.getExtensionName(zip_content.item(i).path))) then
    
                    'msgbox "il file esiste, ora lo cancello"
                    fso.DeleteFile(fso.Buildpath(percorso,zip_content.item(i).name)+"."+fso.getExtensionName(zip_content.item(i).path))
    
                end if
    
                destinazione.copyHere(zip_content.item(i))
    
            next        
    
            file.Delete
    
        end if
    
    next
    
    'for each sottocartella in cartella.subfolders
    '   call estrai(folder.path)
    'next
    
    end sub
    
    call estrai("C:\Documents and Settings\Mattia\Desktop\prova")