Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need Help Expand / Collapse
Author
Message
Posted Monday, October 7, 2013 9:09 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, March 4, 2014 8:39 AM
Points: 202, Visits: 1,047
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,
Post #1502202
Posted Monday, October 7, 2013 9:36 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:47 AM
Points: 2,422, Visits: 7,443
In a script component, do something like this (untested): -

            DirectoryInfo directory = new DirectoryInfo("T:\\Test");
FileInfo[] fileList = directory.GetFiles("*.*", SearchOption.AllDirectories);
foreach (FileInfo file in fileList.Where(file => file != fileList.OrderByDescending(currentFile => currentFile.CreationTime).First()))
{
file.Delete();
}




Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1502224
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse