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


Need Help


Need Help

Author
Message
sql2k8
sql2k8
Right there with Babe
Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)

Group: General Forum Members
Points: 775 Visits: 1129
Hi ,


I am trying to delete files in a folder except the last created file in the folder.


can you please help me with the logic.


The files in the folder are sql server audit files with the extension .sqlaudit


I cannot choose the files based on date because if the auditing data is not present on todays date . the old files can be deleted.


I am looking for a logic which should look at the files in the folder and delete all the files in that folder except the last created file in that folder.


I am using SQL Server 2008 R2 Enterprise edition RTM 64 bit on windows server 2008 R2 Enterprise edition sp1 64 bit


Thank You,
Kurt W. Zimmerman
Kurt W. Zimmerman
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2574 Visits: 1400
I am not clear with what the criteria you are using for deleting. Maybe an example could help.

Kurt

Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY

http://www.linkedin.com/in/kurtwzimmerman
sql2k8
sql2k8
Right there with Babe
Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)

Group: General Forum Members
Points: 775 Visits: 1129
Hi,

Example: Audit_DBA_991AEE46-71F7-470D-B63C-2EC208FD3303_0_130126053692760000.sqlaudit

Auditing file looks like this. when i go to folder i could see date and time the file was modified.

My auditing files doesn't contain the datetime. i have to check in the folder (M:\Auditing\DBA ) containing the files for the date and time.

I have to delete the files in the folder name M:\Auditing\DBA to save the disk space.


Thank You,
Steve Jones
Steve Jones
SSC Guru
SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)

Group: Administrators
Points: 143412 Visits: 19424
I might look through our scripts for something similar: http://www.sqlservercentral.com/search/?q=delete+old+backups&t=s&sort=relevance

Powershell is how I'd probably do this now, though VBScript works well. You can easily manipulate files and delete all but the most recent.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Kurt W. Zimmerman
Kurt W. Zimmerman
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2574 Visits: 1400
Does your instance of SQL Server have xp_cmdshell enabled? If so you can capture the result of the DOS command "DIR" into a table variable containing the date and time. Here is a snippet of code that I use for looking for database backups using this method:


set @FullyQualifiedPath = @BackupPath + '\' + @Database_Name
set @DirectoryCmd = 'dir /b /o:-d ' + @FullyQualifiedPath + '\*.bak'

delete from @DatabaseBackups

insert into @DatabaseBackups (BackupFileName)
exec xp_cmdshell @DirectoryCmd

delete from @DatabaseBackups
where BackupFileName is null or
charindex(@TodayDate, BackupFileName) = 0





Here is the table definition for @DatabaseBackups:


declare @DatabaseBackups table (database_id int
,BackupFileName varchar(max))




This gives you an idea what you can do. You can capture the date/time of the files then process what ever you want.

Hope this helps.

Kurt

Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY

http://www.linkedin.com/in/kurtwzimmerman
sql2k8
sql2k8
Right there with Babe
Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)Right there with Babe (775 reputation)

Group: General Forum Members
Points: 775 Visits: 1129
Hi Steve,

I don't know Powershell.

Thank You
Steve Jones
Steve Jones
SSC Guru
SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)

Group: Administrators
Points: 143412 Visits: 19424
sql2k8 (10/7/2013)
Hi Steve,

I don't know Powershell.

Thank You


This is a good chance to learn. It's not terribly difficult and you have a specific project here to work on. Do a little research and googling.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
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