Be careful with the names of the folders of origin and destination, knowing that this procedure does not accept "spaces" in the names of the folders.
The same S.P can be used with Winzip, but, why don't use a free software?
Be careful with the names of the folders of origin and destination, knowing that this procedure does not accept "spaces" in the names of the folders.
The same S.P can be used with Winzip, but, why don't use a free software?
USE [master]
GO
CREATE PROCEDURE [dbo].[UP_Compacta_Copia]
@pDirCompacta VARCHAR(40)
,@pDirDestino VARCHAR(100)
,@pExclui INT =0
/*******************************************************************************************************
NAME: UP_Compacta_Copia
DESCRIPTION: Compacta os arquivos de uma pasta e os move para um determinado diretório
USAGE:
EXEC MASTER.dbo.UP_Compacta_Copia 'E:\BACKUP_SQLSERVER','\\destination_folder\',1 --The files are deleted
EXEC MASTER.dbo.UP_Compacta_Copia 'E:\BACKUP_SQLSERVER','\\destination_folder\' --the original files are kept in the folder
*** just be careful with "spaces" in folder names ***
********************************************************************************************************
*/
AS
BEGIN
DECLARE @vCmdDOS VARCHAR(400)
,@vNOArquivo VARCHAR(1000)
, @vSubject VARCHAR(150)
, @vBODY VARCHAR(MAX)
, @vResult INT
IF RIGHT(@pDirCompacta,1)<>'\' SET @pDirCompacta = @pDirCompacta + '\'
SET @vSubject = 'Compactação/Centralização de arquivos'
SET @vBODY = 'Lista de arquivos compactados. Servidor' + @@SERVERNAME + ' em ' + CONVERT(CHAR(10), getdate(), 103) + ' ' + CONVERT(CHAR(10), getdate(), 108)
SET @vBODY = @vBODY + CHAR(13) + '-------------------------------------------------------------------------------------------------'
CREATE TABLE #Arquivos (Nome varchar(1000), Depth varchar(1), isFile varchar(1))
INSERT INTO #Arquivos Exec master.dbo.xp_dirtree @pDirCompacta, 1, 1
--SELECT * FROM #Arquivos
DECLARE CRS_Arquivos CURSOR
FOR SELECT DISTINCT Nome FROM #Arquivos WHERE isFile='1'
OPEN CRS_Arquivos
FETCH NEXT FROM CRS_Arquivos INTO @vNOArquivo
WHILE (@@fetch_status = 0)
BEGIN
SET @vCmdDOS= '"C:\Program Files (x86)\7-Zip\7z.EXE" -mx7 a -tzip' + @pDirCompacta + @vNOArquivo + '.zip' + @pDirCompacta + @vNOArquivo
PRINT @vCmdDOS
EXEC @vResult = master.dbo.xp_cmdshell @vCmdDOS, no_output
IF (@vResult=0 AND @pExclui=1)
BEGIN
SET @vCmdDOS='DEL' + @pDirCompacta + @vNOArquivo
EXEC master.dbo.xp_cmdshell @vCmdDOS
END
SET @vBODY = @vBODY + CHAR(13) + @pDirCompacta + @vNOArquivo
FETCH NEXT FROM CRS_Arquivos INTO @vNOArquivo
END
CLOSE CRS_Arquivos
DEALLOCATE CRS_Arquivos
DROP TABLE #Arquivos
SET @vBODY = @vBODY + CHAR(13) + '-------------------------------------------------------------------------------------------------'
SET @vBODY = @vBODY + CHAR(13) + 'Destino dos arquivos: ' + @pDirDestino
IF @pExclui='1'
SET @vBODY = @vBODY + CHAR(13) + 'Exclusão de arquivos: OS ARQUIVOS FORAM EXCLUÍDOS'
ELSE
SET @vBODY = @vBODY + CHAR(13) + 'Exclusão de arquivos: sem exclusão de arquivos'
--MOVER OS ARQUIVOS
IF (REPLACE(@pDirCompacta,'/','')<>REPLACE(@pDirDestino,'/',''))
BEGIN
--Mover somente se for para diretórios diferentes
SET @vCmdDOS = 'EXEC master.dbo.xp_cmdshell ''MOVE /Y' + @pDirCompacta + '*.zip' + @pDirDestino +''''
PRINT 'Movido de' + @pDirCompacta + 'para' + @pDirDestino
END
PRINT @vCmdDOS
EXECUTE (@vCmdDOS)
EXEC [Master].dbo.UP_EnviarEmailDBAs @vSubject, @vBODY
END