Deleting Contents of a Folder, and Downloading and Extracting a Zip in VBS

Discussion in 'Software' started by Heaney, Sep 17, 2011.

  1. Heaney

    Heaney Private E-2

    So I'm wondering how I would have a vbs file that when run would:

    1. Delete the contents of a specific folder
    2. Download a specific zip file (to a temp location)
    3. Extract this zip file to a specific permenant location
    4. Give an "Installation Complete!" popup.

    So far I've found this to do step 1:

    Code:
    Dim FSO, D, F
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set D = FSO.GetFolder("my/path/here")
    For Each F In D.Files
      FSO.DeleteFile F.path, True
    Next
    Does anyone know where I could find the script to do the other things, and put it all into one vbs that runs them in order? :)
     
  2. GermanOne

    GermanOne Guest

    It's probably feasible. I need some more information to suggest a possible solution.
    - What protocol is used for downloading? http, ftp, ...
    - Would you like to extract the zip file by vbs only or is there a program (7-Zip, WinRAR, ...) which should do that?
    - What Windows version(s) are you working with?
    - Is there a subfolder structure inside the zip file?

    Regards
    GermanOne
     
  3. Heaney

    Heaney Private E-2

    HTTP

    Either the built in windows XP extracter or Winzip (note, it's a VERY old version).

    Whichever would be easier.

    XP 32 bit

    Yes, and it's very important that it is kept. Also, some files/folders are hidden and that must be kept too (if possible).


    Thanks for your interest so far.
     
  4. GermanOne

    GermanOne Guest

    OK, step by step.
    Download:
    Code:
    Const strURL = "http://whateverdomain/filename.zip"
    
    Set objWSH = CreateObject("WScript.Shell")
    strSaveAs = objWSH.ExpandEnvironmentStrings("%temp%\temp.zip") 
    Set objWSH = Nothing
    
    Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP")
    On Error Resume Next
    objXMLHTTP.Open "GET", strURL, False
    objXMLHTTP.Send
    If (objXMLHTTP.readyState = 4 And objXMLHTTP.status = 200) Then
      Set objADOS = CreateObject("ADODB.Stream")
      With objADOS
        .Type = 1
        .Open
        .Write objXMLHTTP.responseBody
        .SaveToFile strSaveAs, 2
        .Close
      End With
      Set objADOS = Nothing
    Else
      WScript.Echo "ERROR"
      Set objXMLHTTP = Nothing
      Wscript.Quit
    End If
    On Error Goto 0
    
    Set objXMLHTTP = Nothing
    
    WScript.Echo "File saved as" & vbLf & strSaveAs
    
    Change the value of strURL and search the %temp% folder for temp.zip after the message came up.


    The zip extaraction using pure vbs is not a good idea on XP (works fine only on Win7). Would it be a big deal to install a better program like 7-Zip?
     
  5. Heaney

    Heaney Private E-2

    That worked perfectly as step 2...

    I have no access to installing programs on this xp system, or running exes.

    There is a very old version of winzip on it though...?

    Are you saying it's impossible to use the built in xp extractor?
     
  6. GermanOne

    GermanOne Guest

    That's good news :)

    There is a duality between ZIP as file (in the file system) and ZIP as folder (in the shell). You cannot access the content of the ZIP file, but you can retrieve informations from the ZIP folder. I know it's quite difficult to understand. There is no good way on XP to handle a subfolder structure inside a ZIP especially if it already exists on the hard drive.
    Is it possible to delete these folders before creating them new from the ZIP or could that cause problems?
     
  7. Heaney

    Heaney Private E-2

    Hmmm.

    As a solution, I could just upload the contens of the zip to a hosting space, and have each download all at the same time?

    If so, would the script you provided for downloading just be copied lots of times or is there a shorter script for batch downloads? Or could you even have a script that downloads thr content of a hosted foldrr and keeps the file structure?

    To review, the script would now do:

    1. Delete the contents of a.specific folder (script in my first post)
    2. Download everything from a web folder (http://www.example.eg/files), keeping file structure
    3. Popup an "Installation complete!" box
     
  8. GermanOne

    GermanOne Guest

    Too complicated, too much server traffic, you have to download each single file and you have to specify for each file where to save. The ZIP solution is the better way.
    OK once again. Imagine the ZIP has to be extracted into C:\test. What we need is to clear the contents (delete files and subfolders) of C:\test before extracting the ZIP. This could be done programmatically. The question was whether that would cause problems or not?

    Regards
    GermanOne
     
  9. Heaney

    Heaney Private E-2

    It's actually on a sort of LAN... but with limited access.

    That would cause problems for ONE subfolder of a subfolder of the extraction location...

    Could you copy it out before extraction and after extraction copy it in again? :confused
     
  10. GermanOne

    GermanOne Guest

    You could exclude it from deleting. But now it becomes very special. Not sure how I should explain it without knowing the structure inside the ZIP and inside the folder :confused
     
  11. Heaney

    Heaney Private E-2

    Why couldn't we just copy it out and back in again....?
     
  12. Heaney

    Heaney Private E-2

    What exactly do you need to know about the zip structure?
     
  13. GermanOne

    GermanOne Guest

    OK, I wrote the code according your description. You have to change the constants in the beginning. I tested only on Win7, not sure whether it works on XP too.
    Code:
    Const strURL = "http://whateverdomain/filename.zip"
    Const strExpandTo = "C:\weherever"
    Const strExclude = "C:\weherever\sub1\sub2"
    
    '''' Path Of Temporary ZIP File
    Set objWSH = CreateObject("WScript.Shell")
    strSaveAs = objWSH.ExpandEnvironmentStrings("%temp%\temp.zip")
    strTemp =  objWSH.ExpandEnvironmentStrings("%temp%")
    Set objWSH = Nothing
    
    '''' Download ZIP File
    Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP")
    On Error Resume Next
    objXMLHTTP.Open "GET", strURL, False
    objXMLHTTP.Send
    If (objXMLHTTP.readyState = 4 And objXMLHTTP.status = 200) Then
      Set objADOS = CreateObject("ADODB.Stream")
      With objADOS
        .Type = 1
        .Open
        .Write objXMLHTTP.responseBody
        .SaveToFile strSaveAs, 2
        .Close
      End With
      Set objADOS = Nothing
    Else
      WScript.Echo "ERROR"
      Set objXMLHTTP = Nothing
      Wscript.Quit
    End If
    On Error Goto 0
    
    Set objXMLHTTP = Nothing
    
    '''' Copy Folder To Temp
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(strExclude) Then
      objFSO.CopyFolder strExclude, strTemp & "\", True
      WScript.Sleep 500
    End If
    
    '''' Delete Contents
    Set objExpandTo = objFSO.GetFolder(strExpandTo)
    For Each objFile In objExpandTo.Files
      objFSO.DeleteFile objFile.Path, True
    Next
    For Each objSubFolder In objExpandTo.SubFolders
      objFSO.DeleteFolder objSubFolder.Path, True
    Next
    Set objExpandTo = Nothing
    
    '''' Expand Temporary ZIP File
    Set objShellApp = CreateObject("Shell.Application")
    Set objExpandTo = objShellApp.NameSpace(strExpandTo)
    Set objZip = objShellApp.NameSpace(strSaveAs)
    For Each objItem In objZip.Items
      objExpandTo.CopyHere objItem.Path
      WScript.Sleep 300
    Next
    WScript.Sleep 1000
    Set objZip = Nothing
    Set objExpandTo = Nothing
    Set objShellApp = Nothing
    
    '''' Copy Folder Back
    If Not objFSO.FolderExists(objFSO.GetParentFolderName(strExclude)) Then
      objFSO.CreateFolder objFSO.GetParentFolderName(strExclude)
    End If
    objFSO.CopyFolder strTemp & "\" & objFSO.GetFileName(strExclude), objFSO.GetParentFolderName(strExclude) & "\", True
    
    '''' Delete Temporary ZIP File And Folder
    objFSO.DeleteFile strSaveAs, True
    objFSO.DeleteFolder strTemp & "\" & objFSO.GetFileName(strExclude), True
    WScript.Sleep 500
    Set objFSO = Nothing
    WScript.Echo "Installation Complete!"
    
    Regards
    GermanOne
     
  14. Heaney

    Heaney Private E-2

    Thank you VERY much GermanOne, I'll test this tomorrow. :)
     
  15. Heaney

    Heaney Private E-2

    Okay I tried this.

    It works in downloading the file, but fails at the '''' Delete Contents section.

    It says I don't have access.

    It seems I don't have access to deleting files in the manor that you are suggesting on this system.

    Any solutions that you can think of...?
     
  16. GermanOne

    GermanOne Guest

    I had the same effect when I tested the code. For that reason I inserted the line
    WScript.Sleep 500
    in the section before.
    Try to extend this time from 500 ms to, say, 1000 or 2000 ms to give Windows enough time to finish the copying.

    Regards
    GermanOne
     
  17. PC-XT

    PC-XT Master Sergeant

    Wscript.sleep may not be included in XP's WSH, in case that's the problem. I use shell calls to ping, instead, but there are other ways. If increasing the delay works, ignore me. :p
     
  18. Heaney

    Heaney Private E-2

    Got it working, thanks very much again.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds