Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing all files on my computer and sorting by size

Recently I've come into the problem that my hard drive is getting obnoxiously full, but after going through my personal files and deleting/moving all of the oversized video files, I still have a rather small amount of ROM available. So I put my programmer brain to work and decided that instead of meticulously going through each folder and subfolder myself and using the right-click + Properties function of Windows to see how big a file is and whether or not its worth keeping around, I could write a simple code that would search for every file on my computer, throw it into a list by its full path name, and put its file size right next to it, then sort it from greatest to least by file size. So I hopped online, started doing the research, and thats when everything hit the fan for me. I've found plentiful code snippets that work for their designated task but whenever I try to utilize them myself I run into boatloads of build errors. That said, the most promising thing that I've found so far is:

const string dir = "C:\\";
string[] fns = Directory.GetFiles(dir);
var Sort = from fn in fns
           orderby new FileInfo(fn).Length descending
           select fn;
foreach (string n in Sort)
    Console.WriteLine(n);

Unfortunately this does not touch any subdirectory. I've looked up how to grab the files out of subdirectories, but trying to integrate those code snippets with this one proved more trouble than I could have imagined. On the rare occasion that light was seen at the end of the tunnel, my program would touch a directory that was apparently protected by Administrator privileged (I am the only user, and thus Administrator of my computer) and tossed out errors like a chimpanzee at a zoo tosses out feces.

So on the whole, what I am seeking assistance with is: -Program that searches every file on my computer (I am assuming that starting with the "C:/" drive is where I can access everything) -Takes each file, its size, and the path to that file and throws it onto a list/array/whatever -Sorts it by file size from greatest to least -Places this list into a .txt file

The last part I actually don't need help with since I am rather familiar with the Streamwriter class. I can even muck my way through sorting by file size with a quasi-simple parsing algorithm I can make on the fly if my list/array/etc of files/paths/sizes all conform to the same patterns and can be converted into strings. So roughly 90.23% of my issues are simply getting all the files, getting into or ignoring-and-continuing Admin protected folders (I think ignoring them would be best since I highly doubt anything in a protected folder should ever be deleted. Ever.) Getting the paths and sizes of all of those files, and organizing them.

like image 803
Zach Avatar asked Oct 20 '12 14:10

Zach


People also ask

How do you list files and folders with respective size?

To list all files and sort them by size, use the -S option. By default, it displays output in descending order (biggest to smallest in size). You can output the file sizes in human-readable format by adding the -h option as shown. And to sort in reverse order, add the -r flag as follows.

How can I see all folders by size?

Open a file explorer window and right-click on the 'Name' field at the top. You'll see some options – specifically, options, that let you pick what sort of info you want to see about your folders. Select Size and the property will appear on the far right of your window.

How do I get a list of files in a folder and subfolders with size?

Substitute dir /A:D. /B /S > FolderList. txt to produce a list of all folders and all subfolders of the directory. WARNING: This can take a while if you have a large directory.


1 Answers

Try another overload of GetFiles:

string[] fns = Directory.GetFiles(dir, "*", SearchOption.AllDirectories);

Also it will be more efficiently if you use EnumerateFiles:

const string dir = "C:\\";
var Sort = Directory.EnumerateFiles(dir, "*", SearchOption.AllDirectories)
                    .OrderByDescending(f => new FileInfo(f).Length);
foreach (string n in Sort)
{
        Console.WriteLine(n);
}

To avoid exceptions:

const string dir = "C:\\";
var fileInfos = new List<FileInfo>();
GetFiles(new DirectoryInfo(dir), fileInfos);
fileInfos.Sort((x, y) => y.Length.CompareTo(x.Length));
foreach (var f in fileInfos)
{
    Console.WriteLine(f.FullName);
}

private static void GetFiles(DirectoryInfo dirInfo, List<FileInfo> files)
{
    // get all not-system subdirectories
    var subDirectories = dirInfo.EnumerateDirectories()
        .Where(d => (d.Attributes & FileAttributes.System) == 0);
    foreach (DirectoryInfo subdirInfo in subDirectories)
    {
        GetFiles(subdirInfo, files);
    }
    // ok, now we added files from all subdirectories
    // so add non-system files from this directory
    var filesInCurrentDirectory = dirInfo.EnumerateFiles()
        .Where(f => (f.Attributes & FileAttributes.System) == 0);
    files.AddRange(filesInCurrentDirectory);
}
like image 176
tukaef Avatar answered Nov 15 '22 11:11

tukaef