October 7, 2013 at 9:09 am
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,
October 7, 2013 at 9:36 am
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();
}
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply