Saturday, February 18, 2017

VBScript Create Rar File With Password Protection Using WinRar.exe

VBScript is a scripting language to manage computer developed by Microsoft. Sometimes we need to create .rar file using WirRar.exe and use VBScript. Below is a code snippet to create rar file using both VBScript & WinRar.exe. There is a extension you can use password to protect your rar file. -p"PASSWORD" will create a rar file with password or if you use only -p as arguments then a prompt will appear to take password from user as input. -r use to create recursive all folders and files withing the root folder. -hs argument for include all hidden files.

Dim ObjShell, FSO
Set ObjShell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")

Wscript.StdOut.Write "Enter Source: "
Source = Wscript.StdIn.ReadLine

Wscript.StdOut.Write "Enter Destination: "
Destination = Wscript.StdIn.ReadLine

Wscript.StdOut.Write "Delete Source: "
DeleteSource = Wscript.StdIn.ReadLine

ObjShell.Run """C:\Program Files\WinRAR\WinRar.exe"" a -x -IBCK -ep1 -r -hs -p """ & Destination & """ """ & Source & """ ", 0, True
Set ObjShell = Nothing

Sub CheckRarCreated
    If (FSO.FileExists(Destination)) Then
       WScript.Echo Destination & " Created"
       If StrComp(DeleteSource, "True", 1) = 0 And FSO.FolderExists(Source) Then
          FSO.DeleteFolder Source
       End If
    Else
       WScript.Echo Destination & " Not Created"
    End If
End Sub
CheckRarCreated

No comments:

Post a Comment