SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Create and populate a Zip file


Create and populate a Zip file

Author
Message
JimS-Indy
JimS-Indy
SSC-Addicted
SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)

Group: General Forum Members
Points: 453 Visits: 444
I have a small program that runs every day from my client machine that creates a new zip folder on a shared drive, then copies the contents of another folder (and its subfolders) into that zip folder for backup. The backed-up files are around 50 MB total before zipping, and contain an ACCDB, ACCDE, and DDL file among a few others.

I would rather my MS Server 2008 do this job, since it's already there and running my SQL Server instance. I'm thinking there must be a way to do this using T-SQL in a daily scheduled job. Any resources?

Jim
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14257 Visits: 15974
Jim

If you have xp_cmdshell enabled, you can use it to run the command line for your zip utility. Otherwise, you could consider using SSIS to do the same. Another alternative is to run it from a Windows scheduled task, bypassing SQL Server altogether.

John
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28359 Visits: 39959
here's an xp_cmdshell example, like John mentioned above.
the list of files, isntead of being a single file, can be a space delimited set uf multiple files.



--http://www.7-zip.org/download.html
--http://downloads.sourceforge.net/sevenzip/7za920.zip

--xp_cmdshell is limited to ONE set of double quotes.
--one of the paths needs to not contain spaces!
--see 2003 MS KB http://support.microsoft.com/kb/266224
DECLARE @results TABLE(results varchar(255))
declare @command varchar(2000)
--zip one file
SET @command =
'"C:\DataFiles\7zip_CommandLine_7za465\' --path to 7za command line utility note the dbl quotes for long file names!
+ '7za.exe"' --the exe: i'm using in the command line utility.
+ ' a ' --the Add command: add to zip file:
+ 'C:\DataFiles\' --path for zip
+ 'myZipFile.zip' --zip file name, note via xp_cmdshell only one pair of dbl quotes allowed names!
+ ' ' --whitespace between zip file and file to add
+ 'C:\DataFiles\' --path for the files to add
+ 'SandBox_2011-07-25.bak' --the file
+ ' -y' --suppress any dialogs by answering yes to any and all prompts
print @command
--"C:\DataFiles\7zip_CommandLine_7za465\7za.exe" a C:\DataFiles\myZipFile.zip C:\DataFiles\SandBox_2011-07-25.bak -y

insert into @results
exec xp_cmdshell @command

select * from @results
/*
NULL
7-Zip (A) 4.65 Copyright (c) 1999-2009 Igor Pavlov 2009-02-03
NULL
Scanning
NULL
Updating archive C:\DataFiles\myZipFile.zip
NULL
Compressing SandBox_2011-07-25.bak
NULL
Everything is Ok
NULL
*/




Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

JimS-Indy
JimS-Indy
SSC-Addicted
SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)

Group: General Forum Members
Points: 453 Visits: 444
Lowell, I continue to struggle with this. I downloaded zip-7, and wrote a vbs script to call it. Problem is, both the source and target paths have spaces in the pathnames. In any shell situation, I can't seem to use two sets of double quotes. One set gets eaten up. Am I doing it wrong?

    Option Explicit

' Need to create a full path following the Target Location:
' Higher Level Folder: "YYYY-MMM_Backup"
' File Name "YY-MM-DD.Zip"
' Backing up the "CPAS folder and all its subfolders"

Dim YYYY
Dim YY
Dim MMM
Dim MM
Dim DD

Dim strZipInstruction
Dim strZipProgLoc
Dim strTargetLoc
Dim strSourceLoc
Dim FileNameZip
Dim strDate

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")


YYYY = Cstr(Year(Date))
YY = Right(YYYY,2)
MM = FormatNumber((Month(Date)),0,-1)
MMM = Left(MonthName(Month(Date)),3)
DD = FormatNumber(Day(Date),0,-1)

strTargetLoc = "H:\mtv\secure\Construction\Access\All Database Backup\" & YYYY & "-" & MMM & "_Backup\"
If Not oFSO.FolderExists(strTargetLoc) Then
oFSO.CreateFolder strTargetLoc
End If
strTargetLoc = """H:\mtv\secure\Construction\Access\All Database Backup\" & YYYY & "-" & MMM & "_Backup\"""
strSourceLoc = "H:\mtv\secure\Construction\Access\CPAS\"
strZipProgLoc = """C:\Documents and Settings\jamesshaffer\My Documents\7-Zip\7za.exe"" a "
strDate = YY & "-" & MM & "-" & DD
FileNameZip = strDate & ".zip"
strZipInstruction=strZipProgLoc & strTargetLoc & FileNameZip & " " & strSourceLoc
MsgBox strZipInstruction



Jim
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28359 Visits: 39959
Jim the single set of double quotes is an xp_cmdshell thing;there's only the classic extra-step work arounds.

the work around is to either use a variable, or create it in a non-space-containing folder,and simply move it after words....c:\Temp or %appdata%, for example.

so create the zip in %appdata%\Zipfilename.zip, and then move %appdata%\Zipfilename.zip to the final folder that has spaces.

if the move is on the same drive it's super quick, since it's just updating the allocaiton tables, right?

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14257 Visits: 15974
Jim

A vbs script isn't a SQL Server solution, so it's a little outside the scope of these forums. I think it would be simpler to create a batch file, or use the xp_cmdshell solution described by Lowell. If you're having trouble escaping your double quotes in your vbs script, this discussion has a couple of suggestions.

John
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28359 Visits: 39959
jim if you print the variable results, you'll see there's a quote in the wrong place.

"C:\Documents and Settings\jamesshaffer\My Documents\7-Zip\7za.exe" a "F:\mtv\secure\Construction\Access\All Database Backup\2012-Oct_Backup\"12-10-23.zip F:\mtv\secure\Construction\Access\CPAS\




i've corrected your vbs code below to build the path correctly:

--"C:\Documents and Settings\jamesshaffer\My Documents\7-Zip\7za.exe" a "F:\mtv\secure\Construction\Access\All Database Backup\2012-Oct_Backup\12-10-23.zip" "F:\mtv\secure\Construction\Access\CPAS\"

' Need to create a full path following the Target Location:
' Higher Level Folder: "YYYY-MMM_Backup"
' File Name "YY-MM-DD.Zip"
' Backing up the "CPAS folder and all its subfolders"

Dim YYYY
Dim YY
Dim MMM
Dim MM
Dim DD

Dim strZipInstruction
Dim strZipProgLoc
Dim strTargetLoc
Dim strSourceLoc
Dim FileNameZip
Dim strDate

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")


YYYY = CStr(Year(Date))
YY = Right(YYYY, 2)
MM = FormatNumber((Month(Date)), 0, -1)
MMM = Left(MonthName(Month(Date)), 3)
DD = FormatNumber(Day(Date), 0, -1)

strTargetLoc = "M:\mtv\secure\Construction\Access\All Database Backup\" & YYYY & "-" & MMM & "_Backup\"
If Not oFSO.FolderExists(strTargetLoc) Then
'oFSO.CreateFolder strTargetLoc
End If
strTargetLoc = """M:\mtv\secure\Construction\Access\All Database Backup\" & YYYY & "-" & MMM & "_Backup\"
strSourceLoc = """M:\mtv\secure\Construction\Access\CPAS\"""
strZipProgLoc = """C:\Documents and Settings\jamesshaffer\My Documents\7-Zip\7za.exe"" a "
strDate = YY & "-" & MM & "-" & DD
FileNameZip = strDate & ".zip"""
strZipInstruction = strZipProgLoc & strTargetLoc & FileNameZip & " " & strSourceLoc
'Debug.Print strZipInstruction
MsgBox strZipInstruction



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

OTF
OTF
Mr or Mrs. 500
Mr or Mrs. 500 (500 reputation)Mr or Mrs. 500 (500 reputation)Mr or Mrs. 500 (500 reputation)Mr or Mrs. 500 (500 reputation)Mr or Mrs. 500 (500 reputation)Mr or Mrs. 500 (500 reputation)Mr or Mrs. 500 (500 reputation)Mr or Mrs. 500 (500 reputation)

Group: General Forum Members
Points: 500 Visits: 4128
This problem could probably be more "cleanly" solved with a CLR object.
Happy to help you knock one up if you'd like to go that route.
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28359 Visits: 39959
OTF (10/23/2012)
This problem could probably be more "cleanly" solved with a CLR object.
Happy to help you knock one up if you'd like to go that route.


That would be a valuable contribution; i'd love to see it!

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

JimS-Indy
JimS-Indy
SSC-Addicted
SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)

Group: General Forum Members
Points: 453 Visits: 444
I'd love to have that learning experience.

VBS is very unsatisfactory. No editor, No debugger. I wrote the original program in VBA and deployed it as a standalone db (with no tables...) and set it up as a daily scheduled task. I've had lots of authentication issues that I think are now behind me, so that approach will likely work now. We'll see tonight....

So, if you could show me how to implement this as a CLR object, I'd love to learn it. Thanks.

Jim
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search