C# folder and subfolder creation in SSIS RRS feed

  • Question

  • I'm writing a script task in SSIS using C# to create a set of folders and subfolders based on the year/month/day for archiving.
    I'm currently left with one issue which is how to create the variable subfolder paths. The output would be as follows;
    Line 1 creates folder for year (eg, 2018) within C:\temp\
    Line 2 creates folder for month name (eg, October) within the newly created 2018 folder.
    Line 3 creates folder for todays date (eg, 25) and is placed within the new October folder.
    The folder names will obviously change over time so only the initial folder can be specified;

    string pathyear = @"C:\temp\" + DateTime.Today.Year;
    string pathmonth = pathyear + DateTime.Today.ToString("MMMM");
    string pathdate = pathmonth + DateTime.Today.Day;

    Line 1 is fine. And I know a + isn't right for lines 2 and 3 as it will just concatenate the values and add a folder at the same level as 2018, but I can't seem to find the right syntax. I thought it would be something like;

    string pathmonth = pathyear\\DateTime.Today.ToString("MMMM");
    string pathdate = pathmonth\\DateTime.Today.Day;

    Is anyone able to point out the obvious connection I'm missing?

    Thursday, October 25, 2018 3:05 PM


  • Nevermind. Found a re-write that works using Path.Combine

    If anyone requires it the solution is

    var Today = DateTime.Today;
    string pathyear = Today.ToString ("yyyy");
    string pathmonth = Today.ToString ("MMMM");
    string pathdate = Today.ToString ("dd");

    string folder = Path.Combine (@"C:\temp\",
    Path.Combine (pathyear,
    Path.Combine (pathmonth, pathdate)));

    • Marked as answer by lemming3k Thursday, October 25, 2018 4:22 PM
    Thursday, October 25, 2018 4:22 PM