0

I'm currently tossed into having to try and create a sort of housekeeping utility that we can set up scheduled tasks to run.

I've got it working hardcoded to delete from a specific folder and it deletes everything that's older than 7 days.

I would like some flexibility so I don't have to create a new program everytime we find a new dir that we need to housekeep.

I would like to have a configuration.txt file containing two lines, one being Directory of where to look and delete files and the other line being files older than: x days

I can read the file good enough, I'm just not certain how I would go about creating the strings from the text file.

Below is my current working program, it's really basic stuff.

var files = new DirectoryInfo(@"c:\log").GetFiles("*.log")
foreach (var file in files)
{
    if (DateTime.UtcNow - file.CreationTimeUtc > TimeSpan.FromDays(7))
    {
        File.Delete(file.FullName);
Backs
  • 24,430
  • 5
  • 58
  • 85
Dude85
  • 41
  • 1
  • 5
  • 1
    Show how the planned content looks like and show how you read the file at the moment! – TaW Jan 15 '21 at 11:17
  • 1
    Why not use `app.config` – phuzi Jan 15 '21 at 11:24
  • Does this answer your question? [App.Config file in console application C#](https://stackoverflow.com/questions/10069254/app-config-file-in-console-application-c-sharp) – Fildor Jan 15 '21 at 12:07
  • Why not simply use command line parameters instead? Look at the `params string[] args` parameter to the `Main` method, and grab your information from there, then you don't need this configuration file at all. – Lasse V. Karlsen Jan 15 '21 at 12:51
  • 1
    @LasseV.Karlsen I suggested this in my answer ;) – Fildor Jan 15 '21 at 12:53
  • 1
    I had the page up from before that :( I ctrl+clicked a bunch of questions and then went into a meeting :) Good answer :) – Lasse V. Karlsen Jan 15 '21 at 12:54

2 Answers2

1

If you want it as simple, as it gets: use command line args.

class Program
{
    static void Main(string[] args)
    {
        string path;
        int days;

        if( args.Length < 2 ) return; 
        // ^^ You might want to throw some exception.
        // Or print a "Usage" string.

        path = args[0];
        // TODO: validate path, bail out if invalid

        if( !int.TryParse(args[1], out days) )
        {
             // handle parse error
        }
        // TODO: validate parsed value. I guess it shouldn't be negative or 0.

        DoYourThing( path, days );
    }
}

You can then call this from a batchfile for example.


If you want to handle more than one directory in one call and want to use a config file, you can use app.config. Or just read in and deserialize a configuration model from a json file (among many more options).

Example

config.json:

{
    items: [
       {
           path: "C:\test",
           days: 7
       },
       {
           path: "C:\test2",
           days: 5
       }
    ]
}

ConfigModel.cs

public class ConfigModel
{
    public Item[] Items {get; set;}
}

public class Item
{
    public string Path {get; set;}
    public int Days {get; set;}
}

Read it in:

var config = System.Text.Json.JsonSerializer.Deserialize<ConfigModel>(File.ReadAllText(@".\config.json"));

Then you can simply iterate your config:

foreach( var item in config.Items )
{
    DoYourThing(item.Path, item.Days);
}
Fildor
  • 14,510
  • 4
  • 35
  • 67
0

More info would have been great, but something like this for example:

// Read file lines as strings
string[] lines = System.IO.File.ReadAllLines(@"C:\path\to\file.txt");
foreach (string line in lines) 
{
    string[] segs = s.Split('\t');
    string filename = segs[0];
    string age = segs[1];
    // call the relevant function for deleting files
    doDeletion(filename, age);
}

This is assuming that your file has the following format:

foldername1    7
foldername2    14

Where the spacing between folder names and age is a tab character

EDIT: Noticing now that the question was for a file with a line with foldername, followed by a line with age... in that case this would do the trick:

string[] lines = System.IO.File.ReadAllLines(@"C:\path\to\file.txt");
if (lines.Length >= 2) 
{
    string filename = lines[0];
    string age = lines[1];
    doDeletion(filename, age);
}
kagama
  • 92
  • 1
  • 7