Hi guys, Here is a small VBscript for compress or Zip/Unzip folders via command line in windows.
Code :
1: ' Script name : zuip.vbs
2:
3: ' A simple vbscript for zipping a folder or unzip a Zip File.
4: '
5: ' By : http://www.Sec-Articles.blogspot.com 03/2015
6: '
7: ' credits : peter mortensen for zipping function.(http://www.superuser.com/users/517/peter-mortensen)
8: '
9: ' Usage :-
10: '
11: ' For Zipping : cscript zuip.vbs C [path_to_folder] [path_to_zip_file]
12: '
13: ' For Unzipping : cscript zuip.vbs E [path_to_Zip_Archive] [path_to _Extract]
14: '
15:
16: Option Explicit
17:
18: Dim ObjArgs
19: Set ObjArgs = Wscript.Arguments
20:
21: IF (ObjArgs.Count <> 3) Then
22: Wscript.echo "??.. Something Error in Arguments..!!"
23: Wscript.echo "Options : "
24: Wscript.echo "For Zipping -"
25: Wscript.echo "cscript zuip.vbs C [path_to_folder] [path_to_zip_file] "
26: Wscript.echo "For Unzipping -"
27: Wscript.echo "cscript zuip.vbs E [path_to_Zip_Archive] [path_to _Extract]"
28: Wscript.quit
29: End IF
30:
31: Select Case ObjArgs(0)
32: Case "C"
33: Zipper ObjArgs(1), ObjArgs(2)
34: Case "c"
35: Zipper ObjArgs(1), ObjArgs(2)
36: Case "E"
37: Unzipper ObjArgs(1), ObjArgs(2)
38: Case "e"
39: Unzipper ObjArgs(1), ObjArgs(2)
40: Case Else
41: Wscript.echo "No Match Found..!!!"
42: Wscript.echo "Options : "
43: Wscript.echo "For Zipping -"
44: Wscript.echo "cscript zuip.vbs C [path_to_folder] [path_to_zip_file] "
45: Wscript.echo "For Unzipping -"
46: Wscript.echo "cscript zuip.vbs E [path_to_Zip_Archive] [path_to _Extract]"
47: End Select
48:
49:
50: '
51: ' Zipping Function
52: '
53: Function Zipper(SrcF, DestF)
54:
55: Dim fsys
56: set fsys = Wscript.CreateObject("Scripting.FileSystemObject")
57:
58: IF Not (fsys.FolderExists(SrcF)) then
59: Wscript.echo " Source Folder Could not found..!! Please Check the path Again."
60: Exit Function
61: End IF
62:
63: IF (fsys.FileExists(DestF)) then
64: Wscript.echo " Zip file Already Exists..!! Deleting it..?"
65: fsys.DeleteFile DestF
66: Wscript.echo " Zip file Deleted SuccessFully."
67: End IF
68:
69: ' create an empty zip file
70: CreateObject("Scripting.FileSystemObject").CreateTextFile(DestF, True).Write "PK" & chr(5) & chr(6) & String(18, 0)
71:
72: dim objshell, src, des
73:
74: Set objshell = CreateObject("Shell.Application")
75: Set src = objshell.NameSpace(SrcF)
76: Set des = objshell.NameSpace(DestF)
77:
78: des.CopyHere(src.Items)
79:
80: Do Until des.Items.Count = src.Items.Count
81: Wscript.Sleep(200)
82: Loop
83:
84: Wscript.echo "SuccessFully Zipped...!!!"
85:
86: Set fsys = Nothing
87: Set objshell = Nothing
88: Set src = Nothing
89: Set des = Nothing
90:
91: End Function
92:
93:
94: '
95: ' Unzipping Function
96: '
97: Function Unzipper(zipF, extrF)
98:
99: Dim fsys
100: set fsys = Wscript.CreateObject("Scripting.FileSystemObject")
101:
102: IF Not (fsys.FileExists(zipF)) Then
103: Wscript.echo "Could not Found Zip Archive..!! please Check the Path Again."
104: Exit Function
105: End IF
106:
107: IF Not (fsys.FolderExists(extrF)) Then
108: Wscript.echo "Could not Found Folder To Extract..!! Creating Folder..."
109: fsys.CreateFolder(extrF)
110: Wscript.echo "Folder Created SuccessFully."
111: End IF
112:
113: Dim objshell, zip, extr
114:
115: Set objshell = CreateObject("Shell.Application")
116: Set zip = objshell.NameSpace(zipF)
117: Set extr = objshell.NameSpace(extrF)
118:
119: extr.CopyHere(zip.Items)
120:
121: Do Until extr.Items.Count = zip.Items.Count
122: Wscript.Sleep(200)
123: Loop
124:
125: Wscript.echo "SuccessFully Extracted.!!"
126:
127: Set fsys = Nothing
128: Set objshell = Nothing
129: Set zip = Nothing
130: Set extr = Nothing
131:
132: End Function
Download it From here :
How To Use :
For Compress a Folder
Syntax : cscript zuip.vbs C [path_to_folder] [path_to_zip_file]
Example : cscript zuip.vbs C "C:\Users\Ajay\desktop\test" "C:\Users\Ajay\desktop\test.zip"
and for extracting files from a Zip archive
Syntax : cscript zuip.vbs E [path_to_Zip_Archive] [path_to _Extract]
Example : cscript zuip.vbs E "C:\Users\Ajay\desktop\test.zip" "C:\Users\Ajay\desktop\test"
How To Use Video :
Ok..!! Thats all for this.
I hope you like this.
& Thanks for visiting.