Listing all files on my computer and sorting by si

2019-02-18 13:52发布

问题:

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.

回答1:

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.



回答2:

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);
}


回答3:

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