tags:

views:

108

answers:

3

When I am saving a file to the filesystem, I need to store it in chronological order (only three level deep). Year -> Month -> Day -> then store file. (2010 -> June -> 01-06-2010 -> file1.txt. If folders are already in File System then don't create them just save the file.

Whats the best approach?

+3  A: 

Probably not the best, but a quick one.. Run with c:\temp, and get C:\temp\2010\juni\08-06-2010. Locale dependent month name btw..

    public static DirectoryInfo GetCreateMyFolder(string baseFolder)
    {
        var now = DateTime.Now;
        var yearName = now.ToString("yyyy");
        var monthName = now.ToString("MMMM");
        var dayName = now.ToString("dd-MM-yyyy");

        var folder = Path.Combine(baseFolder,
                       Path.Combine(yearName,
                         Path.Combine(monthName,
                           dayName)));

        return Directory.CreateDirectory(folder);
    }
simendsjo
The Directory.Exists() test is unnecessary - CreateDirectory does this anyway.
Jason Williams
Your right. Also, CreateDirectory returns the newly created DirectoryInfo
simendsjo
+1  A: 
DateTime d = DateTime.Now;
String s = Path.Combine(d.Year.ToString(), d.ToString("MMMM"), d.ToString("dd-MM-yyyy"), "file1.txt");
if (!Directory.Exists(s)) Directory.CreateDirectory(s);

For different date formats, this is a good resource: http://blog.stevex.net/string-formatting-in-csharp/

Obviously, you should combine this path to the main path you will be saving the files in (such as: String s2 = Path.Combine("C:\\Test", s);).

drharris
IIRC, `Directory.CreateDirectory(String);` can only create a folder one level deep.
Nate Bross
@Nate: CreateDirectory will ensure the whole path exists. It also does nothing if the directory already exists, so the Directory.Exists() test is unnecessary.
Jason Williams
@Nate Actually, Directory.CreateDirectory will create each directory in the path if it doesn't exist. Read the remark after the first table http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory%28VS.71%29.aspx
Waleed Al-Balooshi
I think what Nate means is that `Path.Combine` accepts two string parameters only.
RedFilter
Not in my .NET. There are overloads for 2, 3, and 4 parameters, as well as a params[] array. This code compiles and runs just fine.
drharris
Those overloads are (finally!) introduced in .net 4.0. Up to 3.5 you are stuck with the Path.Combine(Path.Combine(Path.Combine ... http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx
SWeko
A: 
string basePath = @"c:\temp";
var myDate = DateTime.Now;
DirectoryInfo di = Directory.CreateDirectory(Path.Combine(basePath, myDate.Year.ToString()));
di = Directory.CreateDirectory(Path.Combine(di.FullName, myDate.ToString("MMMM")));
di = Directory.CreateDirectory(Path.Combine(di.FullName, myDate.ToString("dd-MM-yyyy")));

You can also do it with one CreateDirectory call, in this fashion:

string basePath = @"c:\temp";
var myDate = DateTime.Now;
Directory.CreateDirectory(Path.Combine(Path.Combine(Path.Combine(basePath, myDate.Year.ToString()), myDate.ToString("MMMM")), myDate.ToString("dd-MM-yyyy")));
RedFilter