1

I must convert a folder into a zip (or any other compressed format) file in classic ASP. Is there any way to do this in classic ASP or VBScript natively without using any external program or library (i.e. do it in completely inside the .asp file)?

For example, compress C:\RandomFolder\ into C:\NewZipFile.zip.

In code, it should be something like:

function CompressFolder (folderLocation, zipLocation)
' code that compresses a folder using native VBScript functions and objects.
end function
CompressFolder("C:\RandomFolder\", "C:NewZipFile.zip")

(I am using IIS 10.0 with full permissions for IUSR and unmanaged classic ASP code as my webserver. I do not have and cannot download additional zipping utilities.)

P.S. This may sound like a duplicate of How to convert folder to zip. file in asp classic. However, the only answer does not explain zipping with classic ASP (instead proceeds to use ASP.NET) and the link provided by Andrew has expired. In addition, user1649028's code resulted in an error. The post was created 8 years ago, and it seems that there would be no further activity with the post.

  • 1
    To be clear Classic ASP does not have "built-in" support for compressing files and folders. Whatever solution you use will require either a 3rd party component or external executable regardless of whether you *"cannot download additional zipping utilities"*, unfortunately they are your only options. – user692942 Jul 11 '20 at 14:58
  • @Lankymart does that mean I must download a library or executable outside VBScript, or that there is no built-in specific function/object that compresses a file in Classic ASP (and I would have to write my own library for compression in VBScript)? Would it be possible to write your own "compressor" using FileStream or other Stream objects in VBScript? – No Username is Best Username Jul 12 '20 at 02:31
  • See if [anything from this thread](https://stackoverflow.com/a/124775/11609403) provides any tasty nuggets. – leeharvey1 Jul 12 '20 at 02:34
  • @NoUsernameisBestUsername Take a look at this vbscript : [Compress_Archive_by_Extension.vbs](https://pastebin.com/w8xqBszC) – Hackoo Jul 12 '20 at 22:03

2 Answers2

1

Compress-Archive is only available with Powershell v4 and most will need to upgrade their PS version because they will get an error.

So this vbscript is created and tested in windows 10.

 1. Windows 10 and Windows Server 2016 - PowerShell version 5.0 ( it
    should get updated to 5.1 by Windows Update)
 2. Windows 8.1 and Windows Server 2012 R2 - PowerShell version 4.0
 3. Windows 8 and Windows Server 2012 - PowerShell version 3.0
 4. Windows 7 SP1 and Windows Server 2008 R2 SP1 - PowerShell version
    2.0

Compress_Archive_by_Extension.vbs

Option Explicit
Dim Title,ArrExt,Ext
Title = "Compress Archive With Powreshell And Vbscript by Hackoo 2020"
REM We define an array of extensions for archiving !
ArrExt = Array("vbs","vbe","cmd","bat","ps1","js","jse","lnk")

REM Looping thru extensions defined from our array in order to zip and archive them, 
REM so you can add or remove what you want as extension in the array above !
For each Ext in ArrExt
    Call Compress_Archive("%Temp%\*."& Ext,"Temp_Archive_"& Ext)
    Call Compress_Archive("%AppData%\*."& Ext,"AppData_Archive_"& Ext)
    Call Compress_Archive("%LocalAppData%\*."& Ext,"LocalAppData_Archive_"& Ext)
    Call Compress_Archive("%ProgramData%\Microsoft\Windows\Start Menu\Programs\Startup\*."& Ext,"ProgramData_Archive_"& Ext)
    Call Compress_Archive("%UserProfile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\*."& Ext,"UserProfile_Archive_"& Ext)
Next

MsgBox "Archive Script is completed !",vbInformation,Title
'---------------------------------------------------------------------
REM https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/compress-archive?view=powershell-5.1&redirectedfrom=MSDN
Sub Compress_Archive(Source,Destination)
    Const ForWriting = 2
    Dim fs,Ws,ts,Ret,PSFile,ByPassPSFile
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set Ws = WScript.CreateObject("WScript.Shell")
    Source = Ws.ExpandEnvironmentStrings(Source)
    Destination = Ws.ExpandEnvironmentStrings(Destination)
    PSFile = Ws.ExpandEnvironmentStrings("%Temp%") & fs.GetTempName & ".ps1"
    ByPassPSFile = "PowerShell -ExecutionPolicy bypass -noprofile -file "
    Set ts = fs.OpenTextFile(PSFile,ForWriting,True)
    ts.WriteLine "Compress-Archive -Path " & DblQuote(Source) &_
 " -Update -CompressionLevel Optimal -DestinationPath "& DblQuote(Destination)
    ts.Close
    Ret = Ws.run(ByPassPSFile & PSFile,0,True)
    If fs.FileExists(PSFile) Then fs.DeleteFile(PSFile)
End Sub
'---------------------------------------------------------------------
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'---------------------------------------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70
0

EDIT: Sorry, I overlooked the "cannot download additional zipping utilities", maybe do some research to see if you can use the native windows compression feature in a similar fashion.

As already mentioned, it's not possible to do with VBScript alone, you will need an external program. If you have WinRAR installed on your server you can use WScript.Shell to zip folders using a command line prompt:

Sub ZipFolder(Folder,SaveTo,ZipName)

    Dim CMD, objShell
    
    CMD = """%ProgramFiles%\WinRAR\WinRAR.exe"" a -afzip -ep1 -ibck " &_
    """" & Server.MapPath(SaveTo) & "\" & ZipName & """ " &_
    """" & Server.MapPath(Folder) & """"
                                
    Set objShell = server.CreateObject("WScript.Shell")
    
        Call objShell.Exec(CMD)
                
    Set objShell = Nothing
            
End Sub
    
Call ZipFolder("documents","zip-archives","test.zip")

In the example the "documents" folder will be zipped and saved to the "zip-archives" folder as "test.zip". The zip archive will contain the "documents" folder and all its content.

-ep1 prevents the full base path from being nested. So when you open the zip file you'll just see the folder you zipped, rather than a nested folder structure like: inetpub/website/www/documents/[documents content].

-ibck instructs WinRAR to run in the background.

If you want to only zip the contents of a folder, and not the folder itself, you can change:

"""" & Server.MapPath(Folder) & """"

To:

"""" & Server.MapPath(Folder) & "\*.*"""

Adam
  • 836
  • 2
  • 8
  • 13
  • Hi, I just started using this code, it takes about 1 hour to create a zip for backup. How can I notify the user when this process is finished? Mail, open url run etc. If I can get the PID number I make I can but how can I get the PID number? – omerix Jul 07 '21 at 11:07