1
s = Environment.GetEnvironmentVariable("UserProfile") + "\\Pictures";
            string[] photosfiles = Directory.GetFiles(t, "*.*", SearchOption.AllDirectories);
            for (int i = 0; i < s.Length; i++)
            {

                File.Copy(photosfiles[i], tempphotos + "\\" + Path.GetFileName(photosfiles[i]), true);

            }

This will copy the files from one directory to another. I want to check all the time inside the FOR loop for the destination directory size. For example first its copying one file to check the file size if its less then 50mb continue.

Next itertion in the loop after copying the second file check for the two files in the destination directory size if both files size is less then 50mb continue. And so on untill its reaching 50mb then stop the loop.

DanielVest
  • 823
  • 4
  • 20
  • 39
  • 5
    [FileInfo.Length Property](http://msdn.microsoft.com/en-us/library/system.io.fileinfo.length.aspx) – Tim Aug 08 '13 at 08:10

2 Answers2

1

You could count the size of the directory before you start copying the files across and then add the size of each file as you copy it or you could recalculate the size of the directory after each file copy. I think the latter option would likely be more accurate but also much less efficient depending on the size of the files you're copying (if they're very small you'll end up counting them all very many times).

To get the size of a directory use:

public static long DirSize(DirectoryInfo d) 
{    
    long Size = 0;    
    // Add file sizes.
    FileInfo[] fis = d.GetFiles();
    foreach (FileInfo fi in fis) 
    {      
        Size += fi.Length;    
    }
    // Add subdirectory sizes.
    DirectoryInfo[] dis = d.GetDirectories();
    foreach (DirectoryInfo di in dis) 
    {
        Size += DirSize(di);   
    }
    return(Size);  
}

Function used as example here: http://msdn.microsoft.com/en-us/library/system.io.directory(v=vs.100).aspx

So your code would look like:

for (int i = 0; i < photosfiles.Length; i++)
{
    FileInfo fi(photosfiles[i]);

    DirectoryInfo d = new DirectoryInfo(tempphotos);
    long dirSize = DirSize(d);

    //if copying the file would take the directory over 50MB then don't do it
    if ((dirSize + fi.length) <= 52428800)
        fi.CopyTo(tempphotos + "\\" + fi.Name)
    else
        break;
}
Russell Keane
  • 537
  • 1
  • 6
  • 12
  • This is working. Now how do i check the size if its getting to 50mb ? this is the code: for (int i = 0; i < photosfiles.Length; i++) { File.Copy(photosfiles[i], tempphotos + "\\" + Path.GetFileName(photosfiles[i]), true); DirectoryInfo d = new DirectoryInfo(tempphotos); DirSize(d); } I added: DirSize(d); how do i check now in the loop if the size is 50mb now its in bytes. – DanielVest Aug 08 '13 at 08:35
  • From bytes you simply divide by 1024 twice (or 1048576). Or you could compare against 52428800 (50MB in bytes). The latter option will be much more accurate. – Russell Keane Aug 08 '13 at 08:40
0

You can take help of the below piece of code:

string[] sizes = { "B", "KB", "MB", "GB" };
    double len = new FileInfo(filename).Length;
    int order = 0;
    while (len >= 1024 && order + 1 < sizes.Length) {
        order++;
        len = len/1024;
    }

    // Adjust the format string to your preferences. For example "{0:0.#}{1}" would
    // show a single decimal place, and no space.
    string result = String.Format("{0:0.##} {1}", len, sizes[order]);

OR

static String BytesToString(long byteCount)
{
    string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
    if (byteCount == 0)
        return "0" + suf[0];
    long bytes = Math.Abs(byteCount);
    int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
    double num = Math.Round(bytes / Math.Pow(1024, place), 1);
    return (Math.Sign(byteCount) * num).ToString() + suf[place];
}

Both answers are from link How do I get a human-readable file size in bytes abbreviation using .NET?

Community
  • 1
  • 1