Question

I am learning to write a program to delete log files on servers that are older than specified days. This program will be table based rather than server based. Using UNC the paths for the directory of the log files are stored in the database. I am learning about Directory.GetFiles() method and I'm not sure what the syntax would be the path. I'm thinking it would be workList since it stores the paths. Any help resolving this would be greatly appreciated. My code is below.

//create list to store database contents
static List<LogData> GetWorkList()
{
    List<LogData> logDatas = new List<LogData>();
    LogData logData = new LogData();


    //execute sql query
    //execute database reader
    string sqlQuery = "Select daysToKeep, fileLocation, active from dbo.FileDeletion where fileLocation='@fileLocation';";
    SqlCommand command = new SqlCommand(sqlQuery);
    SqlDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {

        //make a logData, creating a new instance of a class
        logData = new LogData();

        //move stuff from reader into log data, and exp
        logData.DaysToKeep = Convert.ToInt32(reader["daysToKeep"]);
        logData.Active = Convert.ToBoolean(reader["active"]);
        logData.FileLocation = Convert.ToString(reader["fileLocation"]);


        //add logData to list
        logDatas.Add(logData);

    }

    return logDatas;
}

//Get active entries from table, call getWorkList
public static void WorkList(//Not sure of what path to use)
{   
    //get active entries

    List<LogData> workList = GetWorkList();

    foreach()
    {
        if(File.Exists()
        {

        }
    }

    //check to see if date created in directory is older that x number of days
    if(DateTime.Now.Subtract(dt).TotalDays <= 1)
    {
        log.Info("This directory is less than a day old");
    }
    //if file is older than x number of days
    else if (DateTime.Now.Subtract(dt).TotalDays <= //not sure of what variable or property to use)
    {
        File.Delete
    }
    //delete file
}
Was it helpful?

Solution

Assuming that your fileLocation contains not just the path, but the filename as well, then the below should work.

//Get active entries from table, call getWorkList
public static void WorkList(//Not sure of what path to use)
{   
//get active entries

List<LogData> workList = GetWorkList();

foreach(var work in workList)
{
    if(File.Exists(work.fileLocation))
    {
        File.Delete(work.fileLocation);
    }
}

//check to see if date created in directory is older that x number of days
if(DateTime.Now.Subtract(dt).TotalDays <= 1)
{
    log.Info("This directory is less than a day old");
}
//if file is older than x number of days
else if (DateTime.Now.Subtract(dt).TotalDays <= //not sure of what variable or property to use)
{
    File.Delete
}
//delete file

}

OTHER TIPS

I am learning about Directory.GetFiles() method and I'm not sure what the syntax would be the path.

Directory.GetFiles() Appears to be pretty straight forward.

foreach (var myPath in pathsFromDatabase)
{
  string[] files = Directory.GetFiles(myPath)
  Console.WriteLine("Directory {0} contains {1} files.", myPath, files.Length);
}

Would print the number of files in each directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top