Listing all files on my computer and sorting by size
Asked Answered
D

3

5

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.

Derekderelict answered 20/10, 2012 at 14:23 Comment(0)
L
5

Does it have to be in c#? Try this from a command prompt: dir c: /B /O-S /S /4 /a-d > fileList.txt

The dir command lists files, /B removes a bunch of info guff, /O-S displays the files in Order (by (S)Size (-)descending and goes through all subdirectories due to /S. The /4 bit just sets the year of files to four digits in case you have some from last century, and the /a-d weeds out the directory listings.

Lasonyalasorella answered 20/10, 2012 at 14:31 Comment(3)
This certainly did what you said it would do, but the file list was rather bulky, contained paths to the sub directories themselves, not just the files in them, and failed to sort the entire list of items by file size, but rather by size per sub directory. Helpful, and possibly the go-to for if my entire endeavor fails, but not quite what I wanted. I greatly appreciate the help though :)Derekderelict
The bulkiness of the list I can't help - that's what happens with an obnoxiously full disk :) . add /a-d to remove directories & subdirectories from the list. I'm sorry about it not sorting correctly, I thought that it would. Maybe when you add the /a option. CheersLasonyalasorella
That certainly did help =) Thanks a bunchDerekderelict
G
6

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);
}
Galumph answered 20/10, 2012 at 14:27 Comment(7)
This seems to be a big step in the right direction for what I'm getting, but now the problem becomes the Unauthorized Access Exceptions. I'm vaguely familiar with the try-catch methodology, so I added one but now when it catches the UnauthorizedAccessException, it ends the program.Derekderelict
@Derekderelict hm. try run resulting .exe file as administrator, outside of vsGalumph
Is there a way to get the program to continue the foreach statement after the try-catch saves me?Derekderelict
Hmm, I see how this is supposed to work, but the part with the x and the y gives me an error about "Cannot convert lambda expression" which, when googled, becomes more confusing then it sounds :(Derekderelict
@Zach, sorry, I missed some details. now it's ok.Galumph
almost worked. The UnauthorizedAccessException gets thrown in the foreach statement, before it reaches the try-catch. I'm mucking around with the possibility of finding a way to try the foreach and when it catches the unauthorized exception, it "kicks" or "deletes" that file from the files Directory, if that makes any sense. Then I can just put the entire thing in a While loop that won't end until after the foreach statement is concludedDerekderelict
@Zach, I edited my answer. Now it get all files, that you can access (non-system). I don't know how to access files in non-accessible directory, so you maybe should ask another question here. Regards.Galumph
L
5

Does it have to be in c#? Try this from a command prompt: dir c: /B /O-S /S /4 /a-d > fileList.txt

The dir command lists files, /B removes a bunch of info guff, /O-S displays the files in Order (by (S)Size (-)descending and goes through all subdirectories due to /S. The /4 bit just sets the year of files to four digits in case you have some from last century, and the /a-d weeds out the directory listings.

Lasonyalasorella answered 20/10, 2012 at 14:31 Comment(3)
This certainly did what you said it would do, but the file list was rather bulky, contained paths to the sub directories themselves, not just the files in them, and failed to sort the entire list of items by file size, but rather by size per sub directory. Helpful, and possibly the go-to for if my entire endeavor fails, but not quite what I wanted. I greatly appreciate the help though :)Derekderelict
The bulkiness of the list I can't help - that's what happens with an obnoxiously full disk :) . add /a-d to remove directories & subdirectories from the list. I'm sorry about it not sorting correctly, I thought that it would. Maybe when you add the /a option. CheersLasonyalasorella
That certainly did help =) Thanks a bunchDerekderelict
M
1

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.

While this is a fun programming exercise, if your goal is to solve the problem at hand, there is a nice utility that will do this for you: WinDirStat

Manchukuo answered 20/10, 2012 at 19:39 Comment(1)
Thank you for the link to the utility. The problem isn't severe enough for me to want to download anything to fix it (and, knowing me, screw it up more) and I largely did decide to do it this way to get the programming experience :)Derekderelict

© 2022 - 2024 — McMap. All rights reserved.