Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 geting file creation date

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-04-08 : 09:17:14
Amir writes "Hi.
My app is generating text files (through SQL server agent jobs)
these file accumulate.
I need to delete old ones (30 days back)
any ideas ?

thanks"

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-04-08 : 11:53:48
You can use xp_cmdshell using DOS commands. Or you can write VBScript to do it.

Tara
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-04-08 : 12:55:58
Create a log table that records the filename and creation date.
Log the files to the table.
Have a batch job that deletes them once a day out of the directory, then out of the table.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-04-08 : 13:07:13
Here is some code to delete files that are older than 2 days:



DECLARE @Path VARCHAR(100) -- location of the files
DECLARE @cmd SYSNAME -- stores the dynamically created DOS command
DECLARE @RowCnt INT -- stores @@ROWCOUNT

SET @Path = 'G:\MSSQL\BACKUP\'

-- Stores the name of the file to be deleted
DECLARE @WhichFile VARCHAR(1000)

CREATE TABLE #DeleteOldFiles
(
DirInfo VARCHAR(7000)
)

-- Build the command that will list out all of the files in a directory
SELECT @cmd = 'dir ' + @Path + ' /OD'

-- Run the dir command and put the results into a temp table
INSERT INTO #DeleteOldFiles
EXEC master.dbo.xp_cmdshell @cmd

-- Delete all rows from the temp table except the ones that correspond to the files to be deleted
DELETE
FROM #DeleteOldFiles
WHERE ISDATE(SUBSTRING(DirInfo, 1, 10)) = 0 OR DirInfo LIKE '%<DIR>%' OR SUBSTRING(DirInfo, 1, 10) >= GETDATE() - 2

-- Get the file name portion of the row that corresponds to the file to be deleted
SELECT TOP 1 @WhichFile = SUBSTRING(DirInfo, LEN(DirInfo) - PATINDEX('% %', REVERSE(DirInfo)) + 2, LEN(DirInfo))
FROM #DeleteOldFiles

SET @RowCnt = @@ROWCOUNT

-- Interate through the temp table until there are no more files to delete
WHILE @RowCnt <> 0
BEGIN

-- Build the del command
SELECT @cmd = 'del ' + @Path + @WhichFile + ' /Q /F'

-- Delete the file
EXEC master.dbo.xp_cmdshell @cmd, NO_OUTPUT

-- To move to the next file, the current file name needs to be deleted from the temp table
DELETE
FROM #DeleteOldFiles
WHERE SUBSTRING(DirInfo, LEN(DirInfo) - PATINDEX('% %', REVERSE(DirInfo)) + 2, LEN(DirInfo)) = @WhichFile

-- Get the file name portion of the row that corresponds to the file to be deleted
SELECT TOP 1 @WhichFile = SUBSTRING(DirInfo, LEN(DirInfo) - PATINDEX('% %', REVERSE(DirInfo)) + 2, LEN(DirInfo))
FROM #DeleteOldFiles

SET @RowCnt = @@ROWCOUNT

END

DROP TABLE #DeleteOldFiles






I pulled this code out of a stored procedure and modified it a bit. I did not test it though, but it should be pretty close to working.

Tara
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-04-08 : 14:41:51
I use this for my backups. It always deletes the previous days backup file before making todays. We verify the tape backup each day before all this happens.

Don't know if it will help you or not, but it should give you an idea how the whole process works.

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER PROCEDURE SABackupFull

--Name: SABackupFull
--Purpose: This stored Procedure(SP) will be used to run full backups for each database.
--
--Format: EXEC SABackupFull @txt_backup_setup_schedule, @int_backup_setup_schedule_step, @txt_core_database_name
--
--Example_1: EXEC SABackupFull '1900','1',NULL --Makes a backup based on the schedule and schedule step.
--Example_2: EXEC SABackupFull NULL,NULL,'web_nhm' --Makes a backup based on the database name.
--Example_3: EXEC SABackupFull NULL,NULL,'PE' --Makes a backup script only based on the database name.
--
--Action: Author: Date: Comments:
--------- ------------- --/--/---- ----------------------------------------
--Created Derrick Leggett 10/03/2003 Initial Development
--Modified Derrick Leggett 11/25/2003 Modified to delete the transaction log backups.
--Modified Derrick Leggett 01/10/2004 Modified to have temp tables filled in declaration.
--Modified Derrick Leggett 02/05/2004 Modified the error handling to handle bad copies correctly.
--

@txt_backup_setup_schedule VARCHAR(10),
@int_backup_setup_schedule_step INT,
@txt_core_database_name VARCHAR(55)

AS

--select * from backup_setup WHERE backup_type_key = 1 order by backup_setup_schedule, backup_setup_schedule_step
--select * from core_database

DECLARE @txt_error VARCHAR(397)

SELECT @txt_error = 'SABackupFull::'

VARIABLE_HANDLING:
--Verify the variables were passed in correctly.
--Must have a valid variable.
IF @txt_backup_setup_schedule IS NULL AND @int_backup_setup_schedule_step IS NULL AND @txt_core_database_name IS NULL
BEGIN
SELECT @txt_error = @txt_error + 'Stored proc requires at leads one variable.'
RAISERROR(@txt_error,16,1)
GOTO ERRORPROCEDURE
END

--Must have a valid schedule and schedule step.
IF (@txt_backup_setup_schedule IS NULL AND @int_backup_setup_schedule_step IS NOT NULL) OR (@txt_backup_setup_schedule IS NOT NULL AND @int_backup_setup_schedule_step IS NULL)
BEGIN
SELECT @txt_error = @txt_error + 'Stored proc requires both schedule and schedule step.'
RAISERROR(@txt_error,16,1)
GOTO ERRORPROCEDURE
END

--Must have a valid schedule and schedule step.
IF @txt_core_database_name IS NOT NULL AND (@txt_backup_setup_schedule IS NOT NULL OR @int_backup_setup_schedule_step IS NOT NULL)
BEGIN
SELECT @txt_error = @txt_error + 'Stored proc requires either database name or schedule and schedule step.'
RAISERROR(@txt_error,16,1)
GOTO ERRORPROCEDURE
END

SELECT @txt_error = @txt_error + @txt_core_database_name + '::'

VARIABLE_POPULATION:
--Declare/Populate needed variables.
DECLARE
@dte_run_date DATETIME,
@int_backup_setup_key INT,
@int_backup_status_key INT,
@int_backup_status_type_key_completed INT,
@int_backup_status_type_key_failed INT,
@int_backup_status_type_key_started INT,
@int_backup_status_type_key_post_backup_copy_completed INT,
@int_backup_status_type_key_post_backup_copy_failed INT,
@int_backup_status_type_key_post_backup_copy_started INT,
@int_backup_type_key_full INT,
@int_backup_type_key_tran INT,
@int_counter INT,
@int_max_identifier INT,
@int_core_database_key INT,
@txt_backup_file VARCHAR(255),
@txt_backup_file_old VARCHAR(255),
@txt_backup_file_old_copy VARCHAR(255),
@txt_backup_file_tran VARCHAR(255),
@txt_backup_name VARCHAR(55),
@txt_backup_setup_directory VARCHAR(255),
@txt_backup_setup_file VARCHAR(255),
@txt_backup_setup_file_copy VARCHAR(255),
@txt_backup_setup_post_backup_copy_directory VARCHAR(255),
@txt_datetime VARCHAR(55),
@txt_datetime_minus_one VARCHAR(55),
@txt_restore_file VARCHAR(255),
@txt_sql_string NVARCHAR(4000)

SELECT
@dte_run_date = GETDATE(),
@int_backup_type_key_full = (
SELECT backup_type_key FROM backup_type WHERE backup_type_name = 'FULL')

SELECT
@int_backup_setup_key = bs1.backup_setup_key,
@int_backup_status_type_key_failed = (
SELECT backup_status_type_key FROM backup_status_type WHERE backup_status_type_name = 'BACKUP FAILED'),
@int_backup_status_type_key_completed = (
SELECT backup_status_type_key FROM backup_status_type WHERE backup_status_type_name = 'BACKUP COMPLETED'),
@int_backup_status_type_key_started = (
SELECT backup_status_type_key FROM backup_status_type WHERE backup_status_type_name = 'BACKUP STARTED'),
@int_backup_status_type_key_post_backup_copy_failed = (
SELECT backup_status_type_key FROM backup_status_type WHERE backup_status_type_name = 'POST BACKUP COPY FAILED'),
@int_backup_status_type_key_post_backup_copy_completed = (
SELECT backup_status_type_key FROM backup_status_type WHERE backup_status_type_name = 'POST BACKUP COPY COMPLETED'),
@int_backup_status_type_key_post_backup_copy_started = (
SELECT backup_status_type_key FROM backup_status_type WHERE backup_status_type_name = 'POST BACKUP COPY STARTED'),
@int_backup_type_key_full = (
SELECT backup_type_key FROM backup_type WHERE backup_type_name = 'FULL'),
@int_backup_type_key_tran = (
SELECT backup_type_key FROM backup_type WHERE backup_type_name = 'TRANSACTIONAL'),
@int_core_database_key = cd.core_database_key,
@txt_core_database_name = cd.core_database_name,
@txt_backup_setup_directory = bs1.backup_setup_directory,
@txt_backup_setup_file = bs1.backup_setup_directory + '\' + bs1.backup_setup_file,
@txt_backup_setup_file_copy = bs1.backup_setup_file,
@txt_backup_setup_post_backup_copy_directory = bs1.backup_setup_post_backup_copy_directory,
@txt_backup_setup_schedule = MAX(bs1.backup_setup_schedule),
@int_backup_setup_schedule_step = MAX(bs1.backup_setup_schedule_step),
@txt_datetime = (SELECT dbo.GENDateOnlyFormat (@dte_run_date)),
@txt_datetime_minus_one = (SELECT dbo.GENDateOnlyFormat (DATEADD(dd,-1,@dte_run_date))),
@txt_restore_file = bs1.backup_setup_post_backup_copy_directory + '\' + bs1.backup_setup_file
FROM
backup_setup bs1
INNER JOIN core_database cd ON bs1.core_database_key = cd.core_database_key
LEFT OUTER JOIN backup_status bs2 ON bs1.backup_setup_key = bs2.backup_setup_key
WHERE
((bs1.backup_setup_schedule = @txt_backup_setup_schedule
AND bs1.backup_setup_schedule_step = @int_backup_setup_schedule_step)
OR cd.core_database_name = @txt_core_database_name)
AND bs1.backup_type_key = @int_backup_type_key_full
GROUP BY
cd.core_database_key,
cd.core_database_name,
bs1.backup_setup_key,
bs1.backup_setup_file,
bs1.backup_setup_directory,
bs1.backup_setup_post_backup_copy_directory

SELECT
@txt_backup_file = @txt_backup_setup_file + '_' + @txt_datetime + '.bak',
@txt_backup_file_old = @txt_backup_setup_file + '_' + @txt_datetime_minus_one + '.bak',
@txt_backup_file_old_copy = @txt_backup_setup_post_backup_copy_directory + '\' + @txt_backup_setup_file_copy + '_' + @txt_datetime_minus_one + '.bak',
@txt_backup_name = @txt_core_database_name + ' BACKUP ' + @txt_datetime,
@txt_backup_setup_file_copy = @txt_backup_setup_file_copy + '_' + @txt_datetime + '.bak',
@txt_restore_file = @txt_restore_file + '_' + @txt_datetime + '.bak'

IF @txt_core_database_name IS NULL OR @txt_backup_file IS NULL OR @int_backup_setup_key IS NULL OR @txt_backup_setup_file IS NULL
BEGIN
SELECT @txt_error = @txt_error + 'core_database_name = ' + ISNULL(@txt_core_database_name,'') + '::backup_setup_schedule = ' + ISNULL(@txt_backup_setup_schedule,'') + '::backup_setup_schedule_step = ' + ISNULL(CAST(@int_backup_setup_schedule_step AS VARCHAR(55)),'') + '::Error populating variables.'
PRINT @txt_error
RAISERROR(@txt_error,16,1)
RETURN -1
END

IF (SELECT OBJECT_ID('tempdb..#tmp_files')) IS NOT NULL
BEGIN
DROP TABLE #tmp_files
END

--Create/Populate temp table to hold backup file information. Will be used to
-- delete transaction files and guide restore and copy process.
CREATE TABLE #tmp_files(
int_identifier INT PRIMARY KEY IDENTITY(1,1),
txt_files VARCHAR(255))

INSERT #tmp_files(txt_files)
SELECT bs1.backup_status_file
FROM
backup_status bs1
INNER JOIN backup_setup bs2 ON bs1.backup_setup_key = bs2.backup_setup_key
WHERE
bs2.backup_type_key = @int_backup_type_key_tran
AND bs2.core_database_key = @int_core_database_key
AND bs1.backup_status_file LIKE ('%' + @txt_datetime_minus_one + '%')

BACKUP_DATABASE:

--Record the beginning of the backup.
INSERT backup_status(
backup_setup_key,
backup_status_type_key,
backup_status_start_datetime,
backup_status_file)

SELECT
@int_backup_setup_key,
@int_backup_status_type_key_started,
GETDATE(),
@txt_backup_file

IF @@ERROR <> 0
BEGIN
SELECT @txt_error = @txt_error + 'Error writing backup beginning status.'
RAISERROR(@txt_error,16,1)
GOTO ERRORPROCEDURE
END

--Deletes the old backup file on production server.
IF @txt_backup_file_old IS NOT NULL
BEGIN
SELECT @txt_sql_string = (SELECT 'del ' + @txt_backup_file_old)
EXEC master..xp_cmdshell @txt_sql_string
END

IF @@ERROR <> 0
BEGIN
SELECT @txt_error = @txt_error + 'Error deleting old backup file.'
RAISERROR(@txt_error,16,1)
GOTO ERRORPROCEDURE
END

--Deletes the old backup file on backup server.
IF @txt_backup_file_old_copy IS NOT NULL
BEGIN
SELECT @txt_sql_string = (SELECT 'del ' + @txt_backup_file_old_copy)
EXEC master..xp_cmdshell @txt_sql_string
END

IF @@ERROR <> 0
BEGIN
SELECT @txt_error = @txt_error + 'Error deleting old backup file on destination server.'
RAISERROR(@txt_error,16,1)
GOTO ERRORPROCEDURE
END

IF @txt_backup_file_old_copy IS NOT NULL
BEGIN
--Delete any transaction logs that are more than one day old.
SELECT
@int_counter = (SELECT MIN(int_identifier) FROM #tmp_files),
@int_max_identifier = (SELECT MAX(int_identifier) FROM #tmp_files)

WHILE @int_counter <= @int_max_identifier
BEGIN

--Creates a string to delete all the transactional log backups older than one day for the database.
SELECT @txt_backup_file_tran = (
SELECT txt_files
FROM #tmp_files
WHERE int_identifier = @int_counter)

SELECT @txt_sql_string = (SELECT 'del ' + @txt_backup_file_tran)
EXEC master..xp_cmdshell @txt_sql_string

IF @@ERROR <> 0
BEGIN
SELECT @txt_error = @txt_error + 'Error deleting old transaction backup files.'
RAISERROR(@txt_error,16,1)
GOTO ERRORPROCEDURE
END

SELECT @int_counter = @int_counter + 1
END
END

IF @@ERROR <> 0
BEGIN
SELECT @txt_error = @txt_error + 'Error deleting old transaction log files on server.'
RAISERROR(@txt_error,16,1)
GOTO ERRORPROCEDURE
END

--Dumps the transaction log file for the selected database.
BACKUP LOG @txt_core_database_name
WITH NO_LOG

IF @@ERROR <> 0
BEGIN
SELECT @txt_error = @txt_error + 'Error dumping log file.'
RAISERROR(@txt_error,16,1)
GOTO ERRORPROCEDURE
END

--Backup the database.
BACKUP DATABASE @txt_core_database_name
TO DISK = @txt_backup_file
WITH
INIT,
NAME = @txt_backup_name,
STATS = 5

IF @@ERROR <> 0
BEGIN
SELECT @txt_error = @txt_error + 'Error backing up database.'
RAISERROR(@txt_error,16,1)
GOTO ERRORPROCEDURE
END

--Record the success of the backup
INSERT backup_status(
backup_setup_key,
backup_status_type_key,
backup_status_start_datetime,
backup_status_file)

SELECT
@int_backup_setup_key,
@int_backup_status_type_key_completed,
GETDATE(),
@txt_backup_file

IF @@ERROR <> 0
BEGIN
SELECT @txt_error = @txt_error + 'Error writing backup completed status.'
RAISERROR(@txt_error,16,1)
GOTO ERRORPROCEDURE
END

POST_BACKUP_COPY:

IF @txt_backup_setup_post_backup_copy_directory IS NOT NULL
BEGIN

--Record the beginning of the post backup file copy.
INSERT backup_status(
backup_setup_key,
backup_status_type_key,
backup_status_start_datetime,
backup_status_file)

SELECT
@int_backup_setup_key,
@int_backup_status_type_key_post_backup_copy_started,
GETDATE(),
@txt_backup_file

IF @@ERROR <> 0
BEGIN
SELECT @txt_error = @txt_error + 'Error writing post backup copy beginning status.'
RAISERROR(@txt_error,16,1)
GOTO ERRORCOPY
END

--If file exists, delete the existing file.
SELECT @txt_sql_string = (SELECT 'del ' + @txt_backup_setup_post_backup_copy_directory + '\' + @txt_backup_setup_file_copy)
EXEC master..xp_cmdshell @txt_sql_string

--drop table #tmp_files create table #tmp_files(txt_files varchar(2500)) insert #tmp_files(txt_files) exec master..xp_cmdshell 'c:\robocopy \\mkcssql01\g$\mssql\backup \\mkcssql02\z$\mssql\backup PE_full_20031023.bak' SELECT txt_files FROM #tmp_files WHERE txt_files LIKE '%100[%]'

--Robocopy the file to the backup server.
SELECT @txt_sql_string =
'c:\robocopy ' + @txt_backup_setup_directory + ' ' +
@txt_backup_setup_post_backup_copy_directory + ' ' + @txt_backup_setup_file_copy

INSERT #tmp_files(txt_files)

EXEC master..xp_cmdshell @txt_sql_string

SELECT * FROM #tmp_files

IF @@ERROR <> 0 OR (SELECT TOP 1 txt_files FROM #tmp_files WHERE txt_files = ' Files : 1 1 0 0 0 0') IS NULL
BEGIN
SELECT @txt_error = @txt_error + 'Error copying backup file.'
RAISERROR(@txt_error,16,1)
GOTO ERRORCOPY
END

--Record the success of the post backup file copy.
INSERT backup_status(
backup_setup_key,
backup_status_type_key,
backup_status_start_datetime,
backup_status_file)

SELECT
@int_backup_setup_key,
@int_backup_status_type_key_post_backup_copy_completed,
GETDATE(),
@txt_restore_file

IF @@ERROR <> 0
BEGIN
SELECT @txt_error = @txt_error + 'Error writing post backup copy beginning status.'
RAISERROR(@txt_error,16,1)
GOTO ERRORCOPY
END
END

RETURN 0

ERRORPROCEDURE:

--Record the failure of the backup.
INSERT backup_status(
backup_setup_key,
backup_status_type_key,
backup_status_start_datetime,
backup_status_file)

SELECT
@int_backup_setup_key,
@int_backup_status_type_key_failed,
@dte_run_date,
@txt_backup_file

SELECT @int_backup_status_key = SCOPE_IDENTITY()

GOTO ERRORWRITE

ERRORCOPY:

--Record the failure of the backup.
INSERT backup_status(
backup_setup_key,
backup_status_type_key,
backup_status_start_datetime,
backup_status_file)

SELECT
@int_backup_setup_key,
@int_backup_status_type_key_post_backup_copy_failed,
@dte_run_date,
@txt_backup_file

SELECT @int_backup_status_key = SCOPE_IDENTITY()

ERRORWRITE:

--Insert error message into backup_error table.
INSERT backup_error(
backup_status_key,
backup_error_status)

SELECT
@int_backup_status_key,
@txt_error
RETURN -1


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page
   

- Advertisement -