forked from himadanreddy/SQL-DBA-SCRIPTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path050.pr_DeleteFiles.sql
More file actions
574 lines (506 loc) · 26.8 KB
/
050.pr_DeleteFiles.sql
File metadata and controls
574 lines (506 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
USE [DBAdmin]
GO
IF dbo.fn_SQLVersion() >= 9
BEGIN
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[lp_DeleteFiles]') AND type in (N'P', N'PC'))
BEGIN
PRINT 'Dropping procedure [lp_DeleteFiles] - SQL 2005'
DROP PROCEDURE [dbo].[lp_DeleteFiles]
END
END
ELSE
BEGIN
IF EXISTS (SELECT * FROM sysobjects WHERE name = 'lp_DeleteFiles' AND type in (N'P', N'PC'))
BEGIN
PRINT 'Dropping procedure [lp_DeleteFiles] - SQL 2000'
DROP PROCEDURE [dbo].[lp_DeleteFiles]
END
END
IF dbo.fn_SQLVersion() >= 9
BEGIN
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pr_DeleteFiles]') AND type in (N'P', N'PC'))
BEGIN
PRINT 'Dropping procedure [pr_DeleteFiles] - SQL 2005'
DROP PROCEDURE [dbo].[pr_DeleteFiles]
END
END
ELSE
BEGIN
IF EXISTS (SELECT * FROM sysobjects WHERE name = 'pr_DeleteFiles' AND type in (N'P', N'PC'))
BEGIN
PRINT 'Dropping procedure [pr_DeleteFiles] - SQL 2000'
DROP PROCEDURE [dbo].[pr_DeleteFiles]
END
END
GO
/****** Object: StoredProcedure [dbo].[pr_DeleteFiles] Script Date: 1/25/2013 1:52:51 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[pr_DeleteFiles]
(
@RootFolder VARCHAR(128) = NULL,
@FileSuffix VARCHAR(8) = NULL,
@ProcessSubFolders BIT = 0,
@CutOffDate DATETIME = NULL,
@CutOffDays INT = NULL,
@ForceDeleteForReadonly BIT = 0,
@Debug BIT = 0,
@DOSDateFormat INT = 101,
@OnlyDeleteArchived BIT = 1
)
-- --------------------------------------------------------------------------------------------------
-- Procedure : [pr_DeleteFiles]
-- Description : To scan all files in a folder and delete the files that are older than the
-- specified date.
-- Requires xp_cmdshell be enabled.
-- Parameters RootFolder The folder to search for files to delete.
-- REQUIRED.
-- FileSuffix Define the files to delete by file suffix,
-- ALL files are processed if NULL / '*' / BLANK.
-- OPTIONAL - defaults to NULL
-- ProcessSubFolder Switch to determine if the process should delete from
-- subfolders.
-- OPTIONAL - defaults to 0 (No).
-- CutOffDate Date which files must be older than for the process to
-- delete.
-- OPTIONAL - defaults to NULL.
-- CutOffDays Age (in days) that files must be older than for the
-- process to delete.
-- Value is always converted to 'Days Ago', whether +ve
-- or -ve value entered.
-- Value overrides CutOffDate, if both entered.
-- OPTIONAL - defaults to NULL.
-- NOTE - Either CutOffDate or CutOffDays is REQUIRED.
-- ForceDeleteForReadOnly Switch to determine if the process should delete even
-- if the file is READ-ONLY.
-- OPTIONAL - defaults to 0 (No).
-- Debug Switch to determine if debugging information should be output
-- OPTIONAL - defaults to 0 (No).
-- DOSDateFormat Specify the Date Format for File Dates from the DIR command,
-- specify as per BOL / Format article.
-- OPTIONAL - Defaults to NULL, only sepcify if Non-US format.
-- OnlyDeleteArchived Switch to specify whether to only delete files that have
-- the Archive flag reset.
-- OPTIONAL - Defaults to 1 (Yes - Only delete files with Archive bit Reset)
--
--
-- Modification Log
-- When Who Description
-- 02/27/2007 Simon Facer Original Version
-- 10/09/2007 Simon Facer Add logic to delete files without a suffix.
-- 10/16/2007 Simon Facer Add logic to handle File Dates without a trailing 'm', as in
-- '10/14/2007 06:00p 34,172,416 ATC_Nevada_Data.DIFF'
-- 03/31/2008 David Creighton Added references to pr_SetCMDShell
-- 06/03/2008 David Creighton Added parameter and processing to handle British DOS file dates.
-- 11/25/2008 Simon Facer Increased column sizes in #Files table.
-- 06/15/2010 Derek Adams Added the @OnlyDeleteArchived parameter to allow the deletes to be
-- filtered by whether the file was backed up to tape or not.
-- 08/17/2010 Simon Facer Remove entries from #Files for File Not Found condition -
-- 'VOLUME' and 'FILE NOT FOUND' literals
-- 01/25/2013 SGB Added OR DirResult = 'The system cannot find the path specified.' to line 183
-- --------------------------------------------------------------------------------------------------
AS
BEGIN
DECLARE @OS_Cmd VARCHAR(1024)
DECLARE @FileID INT
DECLARE @FileIDMin INT
DECLARE @FileIDMax INT
DECLARE @Folder VARCHAR(128)
DECLARE @CmdShell BIT
CREATE TABLE #Files
(
FileID INT IDENTITY(1, 1) NOT NULL,
Folder VARCHAR(512) NULL,
FileName VARCHAR(512) NULL,
FileExtension VARCHAR(512) NULL,
FileDate DATETIME NULL,
DirResult VARCHAR(1024) NULL
)
SET NOCOUNT ON
IF @Debug = 1
BEGIN
SELECT 'Parameters',
@RootFolder AS RootFolder,
@FileSuffix AS FileSuffix,
@ProcessSubFolders AS ProcessSubFolders,
@CutOffDate AS CutOffDate,
@CutOffDays AS CutOffDays,
@ForceDeleteForReadonly AS ForceDeleteForReadonly,
@Debug as Debug
END
-- ******************************************************************************************
-- Validate the passed parameters
IF ( @RootFolder = '' )
OR ( @CutOffDate IS NULL
AND @CutOffDays IS NULL
)
BEGIN
SELECT 'Root Folder and CutOffDays / CutOffDate must be passed in'
RAISERROR ('Root Folder and CutOffDays / CutOffDate must be passed in', 16, 1)
RETURN
END
-- ******************************************************************************************
-- ******************************************************************************************
-- Get the DIR results into the table
SELECT @OS_Cmd = 'DIR ' +
CASE
WHEN @OnlyDeleteArchived = 1 THEN '/A:-A '
ELSE ''
END +
'"' + @RootFolder + '" ' +
CASE @ProcessSubFolders
WHEN 1 THEN ' /S'
ELSE ' '
END
IF @Debug = 1
BEGIN
PRINT 'DIR Command ' + @OS_Cmd
END
EXEC pr_SetCMDShell 1, @CmdShell OUTPUT
INSERT #Files ( DirResult )
EXECUTE master.dbo.xp_cmdshell @OS_Cmd
EXEC pr_SetCMDShell @CmdShell
-- ******************************************************************************************
IF @Debug = 1
BEGIN
PRINT 'Completed DIR Command'
SELECT 'DIR Output', *
FROM #Files
END
-- ******************************************************************************************
-- Locate the first 'Directory Of' entry
SELECT @FileIDMin = MIN(FileID)
FROM #Files
WHERE CHARINDEX('DIRECTORY OF', UPPER(DirResult)) > 0
-- ******************************************************************************************
-- ******************************************************************************************
-- Find the last File(s) entry
SELECT @FileIDMax = MAX(FileID)
FROM #Files
WHERE CHARINDEX('FILE(S)', UPPER(DirResult)) > 0
AND FileID < ( SELECT MAX(FileID)
FROM #Files
WHERE CHARINDEX('FILE(S)', UPPER(DirResult)) > 0
)
-- ******************************************************************************************
-- ******************************************************************************************
-- Delete the entries we dont need
DELETE #Files
WHERE FileID < @FileIDMin
OR DirResult IS NULL
OR CHARINDEX('<DIR>', UPPER(DirResult)) > 0
OR CHARINDEX('FILE(S)', UPPER(DirResult)) > 0
OR CHARINDEX('DIR(S)', UPPER(DirResult)) > 0
OR LEFT((LTRIM(UPPER(DirResult))), 6) = 'VOLUME'
OR LEFT((LTRIM(UPPER(DirResult))), 14) = 'FILE NOT FOUND'
OR FileID > @FileIDMax
OR DirResult = 'The system cannot find the path specified.' -- SGB 01/25/2013
-- ******************************************************************************************
-- ******************************************************************************************
-- Find the Folders in the remaining output
UPDATE #Files
SET Folder = LTRIM(RTRIM(REPLACE(DirResult, 'Directory of ', ''))),
DirResult = NULL
WHERE CHARINDEX('DIRECTORY OF', UPPER(DirResult)) > 0
-- ******************************************************************************************
IF @Debug = 1
BEGIN
PRINT 'About to Start Loop to Populate Folders '
SELECT 'Populate Folders Loop Processing Start', *
FROM #Files
END
-- ******************************************************************************************
-- Loop through the output to populate the Folders
WHILE EXISTS ( SELECT *
FROM #Files
WHERE Folder IS NULL )
BEGIN
-- ******************************************************************************************
-- ******************************************************************************************
-- Find the first Folder entry
SELECT @FileIDMin = MIN(FileID)
FROM #Files
WHERE Folder IS NULL
-- ******************************************************************************************
-- ******************************************************************************************
-- Find the next folder entry, or a dummy entry past the end of the table
SELECT @FileIDMax = COALESCE(MIN(FileID), 999999)
FROM #Files
WHERE Folder IS NOT NULL
AND FileID > @FileIDMin
-- ******************************************************************************************
-- ******************************************************************************************
-- Get the folder value for this loop
SELECT @Folder = Folder
FROM #Files
WHERE FileID = ( SELECT MAX(FileID)
FROM #Files
WHERE FileID < @FileIDMin
)
-- ******************************************************************************************
-- ******************************************************************************************
-- Set the folder value for the files in this group
UPDATE #Files
SET Folder = @Folder
WHERE FileID BETWEEN @FileIDMin AND @FileIDMax
AND Folder IS NULL
-- ******************************************************************************************
-- ******************************************************************************************
-- End of the loop
END
-- ******************************************************************************************
IF @Debug = 1
BEGIN
PRINT 'Completed Loop to Populate Folders '
SELECT 'Populate Folders Loop Processing Completed', *
FROM #Files
END
-- ******************************************************************************************
-- Delete the Folder rows
DELETE #Files
WHERE DirResult IS NULL
-- ******************************************************************************************
IF @Debug = 1
BEGIN
PRINT 'DOS File Date Format: ' + CAST(@DOSDateFormat AS VARCHAR)
SELECT 'DOS File Date Format', *
FROM #Files
END
-- ******************************************************************************************
-- Get the OS File Date/Time
UPDATE #Files
SET FileDate = CONVERT(DATETIME, (LTRIM(RTRIM(SUBSTRING(DirResult, 1, 20))) +
CASE
WHEN UPPER(RIGHT((LTRIM(RTRIM(SUBSTRING(DirResult, 1, 20)))), 1)) = 'A'
THEN 'M'
WHEN UPPER(RIGHT((LTRIM(RTRIM(SUBSTRING(DirResult, 1, 20)))), 1)) = 'P'
THEN 'M'
ELSE ''
END), @DOSDateFormat),
DirResult = LTRIM(RTRIM(SUBSTRING(DirResult, 21, 512)))
-- ******************************************************************************************
-- ******************************************************************************************
-- Get the filename from the DirResult field
UPDATE #Files
SET FileName = LTRIM(RTRIM(SUBSTRING(DirResult,
( CHARINDEX(' ', DirResult) ), 512)))
-- ******************************************************************************************
-- ******************************************************************************************
-- Delete the entries without a file extension
DELETE #Files
WHERE CHARINDEX('.', FileName) = 0
-- ******************************************************************************************
IF @Debug = 1
BEGIN
PRINT 'Pre-Extract File Extensions '
SELECT 'Pre-Extract File Extensions',
FileExtension = SUBSTRING(FileName,
( LEN(FileName) - ( CHARINDEX('.', ( REVERSE(FileName) )) - 2 ) ),
( CHARINDEX('.', ( REVERSE(FileName) )) - 1 )),
FileExtensionLen = LEN(SUBSTRING(FileName,
( LEN(FileName) - ( CHARINDEX('.', ( REVERSE(FileName) )) - 2 ) ),
( CHARINDEX('.', ( REVERSE(FileName) )) - 1 ))),
*
FROM #Files
ORDER BY LEN(SUBSTRING(FileName,
( LEN(FileName) - ( CHARINDEX('.', ( REVERSE(FileName) )) - 2 ) ),
( CHARINDEX('.', ( REVERSE(FileName) )) - 1 ))) DESC,
SUBSTRING(FileName,
( LEN(FileName) - ( CHARINDEX('.', ( REVERSE(FileName) )) - 2 ) ),
( CHARINDEX('.', ( REVERSE(FileName) )) - 1 )) ASC
END
-- ******************************************************************************************
-- Get the file extension from the filename
UPDATE #Files
SET FileExtension = SUBSTRING(FileName,
( LEN(FileName) - ( CHARINDEX('.', ( REVERSE(FileName) )) - 2 ) ),
( CHARINDEX('.', ( REVERSE(FileName) )) - 1 ))
-- ******************************************************************************************
IF @Debug = 1
BEGIN
PRINT 'Extracted File Extensions '
SELECT 'Extracted File Extensions', *
FROM #Files
END
-- ******************************************************************************************
-- If a File Extension was passed in, delete the file names we aren't interested in
IF @FileSuffix IS NOT NULL
AND @FileSuffix != '*'
AND LTRIM(RTRIM(@FileSuffix)) != ''
BEGIN
DELETE #Files
WHERE FileExtension != @FileSuffix
END
-- ******************************************************************************************
IF @Debug = 1
BEGIN
PRINT 'Final File List With Extensions'
SELECT 'Final File List With Extensions', *
FROM #Files
END
-- ******************************************************************************************
-- Default the DirResult to NULL
UPDATE #Files
SET DirResult = NULL
-- ******************************************************************************************
-- ******************************************************************************************
-- If there is a Date/Time value in the file name, extract it
-- Regular expression for '20YYMMDD HHNN', where YY Between 00 AND 19
UPDATE #Files
SET DirResult = '~1~' + SUBSTRING(FileName,
PATINDEX('%20[01][0123456789][01][0123456789][0123][0123456789][ _][012][0123456789][012345][0123456789]%',
FileName), 13)
WHERE PATINDEX('%20[01][0123456789][01][0123456789][0123][0123456789][ _][012][0123456789][012345][0123456789]%',
FileName) > 0
-- ******************************************************************************************
-- ******************************************************************************************
-- If there is a Date/Time value in the file name, extract it
-- Regular expression for 'MMDD20YY HHNN', where YY Between 00 AND 19
UPDATE #Files
SET DirResult = '~2~' + SUBSTRING(FileName,
PATINDEX('%[01][0123456789][0123][0123456789]20[01][0123456789][ _][012][0123456789][012345][0123456789]%',
FileName), 13)
WHERE PATINDEX('%[01][0123456789][0123][0123456789]20[01][0123456789][ _][012][0123456789][012345][0123456789]%',
FileName) > 0
AND DirResult IS NULL
-- ******************************************************************************************
-- ******************************************************************************************
-- If there is a Date value in the file name, extract it
-- Regular expression for '20YYMMDD', where YY Between 00 AND 19
UPDATE #Files
SET DirResult = '~3~' + SUBSTRING(FileName,
PATINDEX('%20[01][0123456789][01][0123456789][0123][0123456789]%',
FileName), 8)
WHERE PATINDEX('%20[01][0123456789][01][0123456789][0123][0123456789]%',
FileName) > 0
AND DirResult IS NULL
-- ******************************************************************************************
-- ******************************************************************************************
-- If there is a Date value in the file name, extract it
-- Regular expression for 'MMDD20YY', where YY Between 00 AND 19
UPDATE #Files
SET DirResult = '~4~' + SUBSTRING(FileName,
PATINDEX('%[01][0123456789][0123][0123456789]20[01][0123456789]%',
FileName), 8)
WHERE PATINDEX('%[01][0123456789][0123][0123456789]20[01][0123456789]%',
FileName) > 0
AND DirResult IS NULL
-- ******************************************************************************************
-- ******************************************************************************************
-- Reformat the dates to include / and : characters (1)
UPDATE #Files
SET DirResult = SUBSTRING(DirResult, 4, 4) + '/' + SUBSTRING(DirResult, 8, 2)
+ '/' + SUBSTRING(DirResult, 10, 2) + ' ' + SUBSTRING(DirResult, 13, 2)
+ ':' + SUBSTRING(DirResult, 15, 2)
WHERE SUBSTRING(DirResult, 1, 3) = '~1~'
-- ******************************************************************************************
-- ******************************************************************************************
-- Reformat the dates to include / and : characters (2)
UPDATE #Files
SET DirResult = SUBSTRING(DirResult, 4, 2) + '/' + SUBSTRING(DirResult, 6, 2)
+ '/' + SUBSTRING(DirResult, 8, 4) + ' ' + SUBSTRING(DirResult, 13, 2)
+ ':' + SUBSTRING(DirResult, 15, 2)
WHERE SUBSTRING(DirResult, 1, 3) = '~2~'
-- ******************************************************************************************
-- ******************************************************************************************
-- Reformat the dates to include / and : characters (3)
UPDATE #Files
SET DirResult = SUBSTRING(DirResult, 4, 4) + '/' + SUBSTRING(DirResult, 8, 2)
+ '/' + SUBSTRING(DirResult, 10, 2)
WHERE SUBSTRING(DirResult, 1, 3) = '~3~'
-- ******************************************************************************************
-- ******************************************************************************************
-- Reformat the dates to include / and : characters (4)
UPDATE #Files
SET DirResult = SUBSTRING(DirResult, 4, 2) + '/' + SUBSTRING(DirResult, 6, 2)
+ '/' + SUBSTRING(DirResult, 8, 4)
WHERE SUBSTRING(DirResult, 1, 3) = '~4~'
-- ******************************************************************************************
-- ******************************************************************************************
-- The File Date overrides the OS File date
UPDATE #Files
SET FileDate = CAST(DirResult AS DATETIME)
WHERE ISDATE(DirResult) = 1
-- ******************************************************************************************
-- ******************************************************************************************
-- Calculate the CutOffDate, if CutOffDays was passed in
IF @CutOffDays IS NOT NULL
AND @CutOffDays != 0
BEGIN
IF @CutOffDays > 0
BEGIN
SELECT @CutOffDays = -1 * @CutOffDays
END
SELECT @CutOffDate = DATEADD(DAY, @CutOffDays, GETDATE())
END
-- ******************************************************************************************
IF @Debug = 1
BEGIN
PRINT 'CutOff Date' + CONVERT(VARCHAR(32), @CutOffDate)
SELECT 'CutOff Date', @CutOffDate AS CutOffDate
END
-- ******************************************************************************************
-- Get rid of the rows where the FileDate isn't outside the cutoff
IF @Debug = 1
BEGIN
PRINT 'File List before CutOff date applied'
SELECT 'File List before CutOff date applied', *
FROM #Files
END
DELETE #Files
WHERE FileDate !< @CutOffDate
IF @Debug = 1
BEGIN
PRINT 'File List with CutOff date applied'
SELECT 'File List with CutOff date applied', *
FROM #Files
END
-- ******************************************************************************************
-- ******************************************************************************************
-- Loop through the files to be deleted
WHILE EXISTS ( SELECT *
FROM #Files )
BEGIN
-- ******************************************************************************************
-- ******************************************************************************************
-- Generate the DELETE command for the current file
SET ROWCOUNT 1
SELECT @OS_Cmd = 'DEL /Q' +
CASE @ForceDeleteForReadonly
WHEN 0 THEN ' '
ELSE ' /F '
END +
'"' + Folder + '\' + FileName + '" ',
@FileID = FileID
FROM #Files
SET ROWCOUNT 0
-- ******************************************************************************************
-- ******************************************************************************************
-- Delete the file
IF @Debug = 1
BEGIN
PRINT 'Delete Command ' + @OS_Cmd
SELECT 'Delete Command', @OS_Cmd
END
EXEC pr_SetCMDShell 1, @CmdShell OUTPUT
EXECUTE master.dbo.xp_cmdshell @OS_Cmd
EXEC pr_SetCMDShell @CmdShell
--PRINT @OS_Cmd
IF @Debug = 1
BEGIN
PRINT 'Delete Completed'
END
-- ******************************************************************************************
-- ******************************************************************************************
-- Delete the row just processed
DELETE #Files
WHERE FileID = @FileID
-- ******************************************************************************************
-- ******************************************************************************************
-- End of WHILE loop
END
-- ******************************************************************************************
END
GO