List all files on my computer and sort by size

Recently, I have encountered a problem that my hard drive is becoming completely unpleasant, but, having gone through my personal files and deleting / moving all oversized video files, I still have a small amount of ROM. So I put my brain to the programmer’s work and decided that instead of carefully going through each folder and subfolder on my own and using the right-click function + Windows Properties to see how big the file is and whether it should be supported, I could write a simple code that will search for every file on my computer, throw it into the list by its full path and put its size next to it, and then sort it from the maximum to the minimum by file size. So I jumped online, started researching, and that’s when it all hit me a fan. I found a lot of code snippets that work for their assigned task, but whenever I try to use them, I encounter boats of build errors. However, the most promising thing I have found so far:

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 apply to any subdirectory. I was looking for how to grab files from subdirectories, but trying to integrate these code snippets with this problem turned out to be more problems than I could imagine. In a rare case when light was seen at the end of the tunnel, my program touched a directory that was apparently protected by the administrator privilege (I am the only user and, therefore, the administrator of my computer) and threw errors such as chimpanzees to the zoo throwing feces .

So, in general, I'm looking for help: -A program that looks for every file on my computer (I assume that starting from the "C: /" drive, I can access everything) -Selects each file, its size and path to this file and issues it to the list / array / independently -Sorts it by file size from maximum to smallest -Installs this list in a .txt file

In the last part, I do not need help, as I am well acquainted with the Streamwriter class. I can even get through sorting by file size using a quasi-simple parsing algorithm that I can do on the fly if my list / array / etc of files / paths / sizes all match the same patterns and can be converted to strings . Thus, approximately 90.23% of my problems are simply getting all the files, hitting or ignoring and saving password protected folders (I think that ignoring them would be better, since I very much doubt that anything in the protected folder should be ever deleted). Getting the paths and sizes of all these files and their organization.

+4
source share
3 answers

Do I need to be in C #? Try this from the command line: dir c: /B /OS /S /4 /ad > fileList.txt

The dir command lists the files, / B deletes a bunch of guff information, / OS displays the files in order (in (S) Size (-) in descending order and goes through all the subdirectories because of / S. The bit just sets the year for the files to four digits, if you have some of the last century as well as / ad weed directory listings.

+5
source

Try another GetFiles overload:

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

In addition, it will be more efficient 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); } 
+6
source

So, I put the brain of the programmer to work and decided that instead of carefully going through each folder and> the subfolder myself and using the right-click function + Windows Properties, to see how big the file is and regardless of whether to support it, I could write a simple code that will search for every file on my computer, throw it into the list by its full path and put its size next to it, and then sort it from the maximum to minimum file size.

While this is fun programming, if your goal is to solve the problem, there is a good utility that will do it for you: WinDirStat

+1
source

Source: https://habr.com/ru/post/1440966/


All Articles