列出计算机上的所有文件并按大小排序 [英] Listing all files on my computer and sorting by size

查看:155
本文介绍了列出计算机上的所有文件并按大小排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我遇到了一个问题,我的硬盘驱动器令人讨厌地装满了,但是在浏览了我的个人文件并删除/移动了所有超大的视频文件后,我仍然可以使用少量的ROM。因此,我投入了程序员的心血,然后决定,我可以自己仔细研究每个文件夹和子文件夹,而不是使用Windows的右键单击+属性功能来查看文件的大小以及是否值得保留,编写一个简单的代码,搜索我计算机上的每个文件,将其完整路径名放入列表,然后将其文件大小放在其旁边,然后按文件大小从大到小排序。
所以我跳到网上开始研究,然后一切都对我产生了影响。我发现有很多代码片段可用于其指定的任务,但是每当我自己尝试使用它们时,都会遇到大量的构建错误。也就是说,到目前为止,我发现的最有希望的东西是:

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.

因此,总的来说,我寻求帮助的是:
-用于搜索计算机上每个文件的程序(我是假设以 C:/驱动器开头是我可以访问所有内容的位置)
-获取每个文件,文件的大小以及该文件的路径,并将其扔到列表/数组/任何
上-按文件大小从大到小排序
-将此列表放入.txt文件

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

最后一部分我实际上不需要帮助,因为我对Streamwriter类非常熟悉。如果我的文件/路径/大小的列表/数组/等都符合相同的模式并且可以转换为字符串,那么我甚至可以通过准简单的解析算法对文件大小进行排序,从而搞砸我的方法。因此,大约90.23%的问题仅仅是获取所有文件,进入或忽略并继续受Admin保护的文件夹(我认为最好忽略它们,因为我高度怀疑应该删除受保护的文件夹中的任何内容。获取所有这些文件的路径和大小,并进行组织。

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.

推荐答案

它必须在c#中吗?从命令提示符处尝试以下操作:
dir c:/ B / O-S / S / 4 / a-d> fileList.txt

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

dir 命令列出文件,/ B删除一堆文件info OS,/ OS以(S)大小(-)降序显示文件,并通过/ S遍历所有子目录。/ 4位仅将文件的年份设置为四位数,以防万一世纪,/ ad清除目录列表。

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.

这篇关于列出计算机上的所有文件并按大小排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆