散分,C#的速度的确快,原来用Ruby做同样的事(太慢)
贴出源码,本人初学,不足之处敬请指出,谢谢!
//按照文件大小降序排,统计某个目录下的所有文件(递归方式)
using System;
using System.Collections;
using System.IO;
using System.Text;
class Test {
private static ArrayList al = new ArrayList();
private static string savePath = "d:/log.txt";
private static int FileCount = 0;
private static int DirCount = 0;
public static int Main(string[] args) {
string myDir = "";
Console.Write("请输入要列表统计的目录:");
myDir = Console.ReadLine().Trim();
if ( !myDir.EndsWith("/") ) {
myDir += "/";
}
if ( !Directory.Exists(myDir) ) {
goError("目录“ + myDir + ”不存在");
}
Console.WriteLine("正在分析数据...");
//把所有的文件和目录存储进ArrayList al中
ListDir(myDir);
//输出
PrintArrayList(myDir);
//释放一些变量
al = null;
savePath = null;
//结束
Console.WriteLine("按任意键结束...");
Console.ReadLine();
return 0;
}
//打印并存储
private static int PrintArrayList(string myDir) {
Console.Write("若要保存结果请输入文件名,否则请按回车键,按y代表:{0}:", savePath);
string readPath = Console.ReadLine().Trim().ToLower();
al.Sort(new MySort());
IEnumerator lem = al.GetEnumerator();
if ( !readPath.Equals("") ) {
if ( readPath != "y" ) {
savePath = readPath;
}
try {
StreamWriter sw = new StreamWriter(savePath, false, Encoding.Default);
sw.WriteLine("统计:" + myDir + " 结果如下\r\n---------------------");
while (lem.MoveNext()) {
sw.WriteLine(lem.Current);
Console.WriteLine(lem.Current);
}
sw.WriteLine("总共文件大小:" + Test.getSize(MyData.total));
sw.WriteLine("共有文件:" + Test.FileCount);
sw.WriteLine("共有目录:" + Test.DirCount);
//输出控制台
Console.WriteLine("统计:" + myDir + " 结果如下\r\n---------------------");
Console.WriteLine("总共文件大小:" + Test.getSize(MyData.total));
Console.WriteLine("共有文件:" + Test.FileCount);
Console.WriteLine("共有目录:" + Test.DirCount);
sw.Close();
sw = null;
} catch (Exception e) {
return goError("出现异常:" + e.ToString());
}
} else {
while (lem.MoveNext()) {
Console.WriteLine(lem.Current);
}
//输出控制台
Console.WriteLine("统计:" + myDir + " 结果如下\r\n---------------------");
Console.WriteLine("总共文件大小:" + Test.getSize(MyData.total));
Console.WriteLine("共有文件:" + Test.FileCount);
Console.WriteLine("共有目录:" + Test.DirCount);
}
return 0;
}
//递归存入列表“al”中
private static int ListDir(string thisDir) {
//这是一个隐藏系统的什么东东,读到这里的时候会出错,所以把它过滤了
if ( thisDir.EndsWith("System Volume Information/") ) {
return 0;
}
DirectoryInfo di = new DirectoryInfo(thisDir);
FileInfo[] files = di.GetFiles();
string[] dirs = Directory.GetDirectories(thisDir);
//循环文件
foreach ( FileInfo file in files ) {
Test.FileCount++;
al.Add(new MyData(thisDir + file, file.Length));
}
//循环目录
foreach ( string dir in dirs ) {
Test.DirCount++;
al.Add(new MyData(dir + "/", 0));
ListDir(dir + "/");
}
files = null;
dirs = null;
return 0;
}
//转换字节(byte, kb, mb, gb)
internal static string getSize(long sn) {
string size;
double n = Double.Parse(sn.ToString());
size = n.ToString() + " byte";
if ( n >= 1024 ) {
n /= 1024;
size = n.ToString() + " KB";
}
if ( n >= 1024 ) {
n /= 1024;
size = n.ToString() + " MB";
}
if ( n >= 1024 ) {
n /= 1024;
size = n.ToString() + " GB";
}
return size;
}
//输出错误信息
private static int goError(string errorStr) {
Console.WriteLine(errorStr);
return 1;
}
}
//实现IComparable中的CompareTo方法,为了ArrayList类的排序
class MyData : IComparable {
private string key;
private long val;
public static long total;
//构造器
public MyData(string key, long val) {
this.key = key;
this.val = val;
MyData.total += val;
}
//重载
public int CompareTo(object o) {
if ( o is MyData ) {
MyData tmp = (MyData) o;
return val.CompareTo(tmp.val);
}
throw new ArgumentException("错误的对象传递");
}
//重载ToString()方法
public override string ToString() {
return this.key + "\tSize: " + Test.getSize(this.val);
}
}
//重载IComparer中的Compare的方法,以便排序操作
class MySort : IComparer {
int IComparer.Compare(object x, object y) {
return Comparer.Default.Compare(y, x);
}
}