Search code examples
vbscriptzipextract

VBScript command to wait for files to be extracted before launching EXE to install program


I'm looking at having a script that decompresses a file (PDMsetup.zip) and then launch the executable that it extracts.

ZipFile="PDMsetup.zip"
ExtractTo=".\"
 
Set fso = CreateObject("Scripting.FileSystemObject")
sourceFile = fso.GetAbsolutePathName(ZipFile)
destFolder = fso.GetAbsolutePathName(ExtractTo)
 
Set objShell = CreateObject("Shell.Application")
Set FilesInZip=objShell.NameSpace(sourceFile).Items()
objShell.NameSpace(destFolder).copyHere FilesInZip, 16
 
Set fso = Nothing
Set objShell = Nothing
Set FilesInZip = Nothing

wscript.sleep 480000

Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile) 
strPath = strFolder &  "\Startwinstall.exe"
objShell.Run strPath

I want to get rid of;

wscript.sleep 480000

and replace it with a command that tells the script wait until the extraction is done before launching startwinstall.exe

I've kept adjusting the wait time to make up for differences in PC performance with the extraction, but a command to just 'wait' until it's done would be preferential.


Solution

  • Delete any previous copy of the installer exe in the target folder and then wait for that file to be created. Create your objects once at the top of the script. And there's no need to set the objects to Nothing. That will happen automatically when the script ends. The edited script is below:

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oWSH = CreateObject("Wscript.Shell")
    Set oApp = CreateObject("Shell.Application")
    
    MyFolder = oFSO.GetParentFolderName(WScript.ScriptFullName) 
    ExtractTo = ".\"
    ZipFile = "PDMsetup.zip"
    StartApp = ExtractTo & "Startwinstall.exe"
    On Error Resume Next
    oFSO.DeleteFile StartApp
    On Error Goto 0
    
    sourceFile = oFSO.GetAbsolutePathName(ZipFile)
    destFolder = oFSO.GetAbsolutePathName(ExtractTo)
    
    Set FilesInZip = oApp.NameSpace(sourceFile).Items()
    oApp.NameSpace(destFolder).copyHere FilesInZip, 16
    
    Do Until oFSO.FileExists(StartApp)
      WScript.Sleep 1000
    Loop
    
    oWSH.Run StartApp
    

    Note: I assigned a MyFolder variable, but it's not currently being used. ExtractTo = ".\" could be changed to ExtractTo = MyFolder. You could also eliminate the GetAbsolutePathName lines if you are using MyFolder with the ZipFile name. There are always many ways to do the same thing.

    Note: I think the above can be done with a much briefer (probably two line) PowerShell script. Let me know if you're interested in that solution.