Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need Help


Need Help

Author
Message
sql2k8
sql2k8
SSC Veteran
SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)

Group: General Forum Members
Points: 211 Visits: 1115
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
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1062 Visits: 1396
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
SSC Veteran
SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)

Group: General Forum Members
Points: 211 Visits: 1115
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-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: Administrators
Points: 35963 Visits: 18725
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: @way0utwestForum Etiquette: How to post data/code on a forum to get the best help
Kurt W. Zimmerman
Kurt W. Zimmerman
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1062 Visits: 1396
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
SSC Veteran
SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)

Group: General Forum Members
Points: 211 Visits: 1115
Hi Steve,

I don't know Powershell.

Thank You
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: Administrators
Points: 35963 Visits: 18725
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: @way0utwestForum Etiquette: How to post data/code on a forum to get the best help
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