I already posted my old article regarding the creating zip/compressed file using sql server here- http://blog.akumars.esoftera.in/post/2012/05/17/Create-zip-file-in-stored-procedure-in-Sql-server.aspx
And http://blog.akumars.esoftera.in/post/2012/05/17/Create-zip-or-rar-file-in-stored-procedure-in-Sql-server.aspx
But some body asks me for that will be done using vbScript, so I also tried to create vbscript .vbs file for compressed file. You can call that .vbs file either from command line directly or call from stored procedure its users choice, with this article I also try to figure out how can working vbScript and that can be run from sql server.
Step 1- Just open a notepad and copy this below vbscript into and save with .vbs, so you will get the .vbs file created.
Option Explicit
Dim arrResult
Dim folder
Dim ZipFile
folder = WScript.Arguments.Item(0)
ZipFile = WScript.Arguments.Item(1)
'Here WScript.Arguments using for geting parameter value from command line
'or from sql server, we can get any number of parameter using index
'Arguments for folder path should be fully qualified path
'If zip file exist it overwritten not appended and empty folder will be skipped
arrResult = ZipFolder( folder,ZipFile)
If arrResult(0) = 0 Then
If arrResult(1) = 1 Then
WScript.Echo "Done with at least 1 empty folder was skipped."
Else
WScript.Echo "Done with " & arrResult(1) & " empty subfolders were skipped."
End If
Else
WScript.Echo "ERROR " & Join( arrResult, vbCrLf )
End If
Function ZipFolder( myFolder, myZipFile )
' This function recursively zip all file from specified folder into a single ZIP file
Dim intSkipped, intSrcItems
Dim objApp, objFolder, objFSO, objItem, objTxt
Dim strSkipped
Const ForWriting = 2
intSkipped = 0
' Make sure the path ends with a backslash
If Right( myFolder, 1 ) <> "\" Then
myFolder = myFolder & "\"
End If
' Use custom error handling
On Error Resume Next
' Create an empty ZIP file here using Scripting.FileSystemObject
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
Set objTxt = objFSO.OpenTextFile( myZipFile, ForWriting, True )
objTxt.Write "PK" & Chr(5) & Chr(6) & String( 18, Chr(0) )
objTxt.Close
Set objTxt = Nothing
' Abort if errors occurd
If Err Then
ZipFolder = Array( Err.Number, Err.Source, Err.Description )
Err.Clear
On Error Goto 0
Exit Function
End If
' Creating a Shell object
Set objApp = CreateObject( "Shell.Application" )
' Copy the files only to the compressed folder
For Each objItem in objApp.NameSpace( myFolder ).Items
If objItem.IsFolder Then
' Check if the subfolder is empty, and if
' so, skip it to prevent an error message
Set objFolder = objFSO.GetFolder( objItem.Path )
If objFolder.Files.Count + objFolder.SubFolders.Count = 0 Then
intSkipped = intSkipped + 1
Else
objApp.NameSpace( myZipFile ).CopyHere objItem
End If
Else
objApp.NameSpace( myZipFile ).CopyHere objItem
End If
Next
Set objFolder = Nothing
Set objFSO = Nothing
' Abort if errors occured
If Err Then
ZipFolder = Array( Err.Number, Err.Source, Err.Description )
Set objApp = Nothing
Err.Clear
On Error Goto 0
Exit Function
End If
' Keep script waiting until compression is finished
intSrcItems = objApp.NameSpace( myFolder ).Items.Count
Do Until objApp.NameSpace( myZipFile ).Items.Count + intSkipped = intSrcItems
WScript.Sleep 200
Loop
Set objApp = Nothing
' Abort if errors occured
If Err Then
ZipFolder = Array( Err.Number, Err.Source, Err.Description )
Err.Clear
On Error Goto 0
Exit Function
End If
' Restore default error handling
On Error Goto 0
' Return message if empty subfolders were skipped
If intSkipped = 0 Then
strSkipped = ""
Else
strSkipped = "skipped empty subfolders"
End If
' Return code 0 if no error occurred
ZipFolder = Array( 0, intSkipped, strSkipped )
End Function
Step2-Now you can call this .vbs file from Sql server as follows
EXEC XP_CMDSHELL 'CScript C:\vScriptForZip.vbs "C:\testfolder" "C:\testfile.zip"'
See here one more important thing you can see how passing the parameter from command line or from sql to .vbs file. Just put the parameter with double quotes after the .vbs file and that parameter value are getting as arguments list in .vbs file using WScript.Arguments. eg WScript.Arguments.Item(0)—1st parameter
WScript.Arguments.Item(1)—2nd parameter and so on if more will be needed.
There are one most important thing if any single line of code written than need to debug most of the time so there are one good way to debug the .vbs file using command line
Eg. Above .vbs file need to debug than just run as follows
EXEC XP_CMDSHELL 'CScript /x /d C:\vScriptForZip.vbs "C:\testfolder" "C:\testfile.zip"'
This will ask you for debug environment eg visual studio 2005 or 2010 what ever installed on system and once you choose that one you can debug as normal c#.net code.
Note- some time vbScript are not register on system so that didn’t running so first you need to register it using register32
Eg-Start->Run-> Type “regsvr32 vbscript.dll” and press Enter
Reference:for xp_cmdshell-- http://msdn.microsoft.com/en-us/library/ms175046(v=sql.90).aspx
For running command line issue also see this link-- http://technet.microsoft.com/en-us/library/ee156587.aspx