最近突然感覺到個人的開發機各種慢,也是啊,從我上大學時起至今,這台老古董跟了我七年多了,還好每年我會親手給它做一兩次的清理和養護,所以它現在依然可以玩命的為我工作。如果我沒記錯,距離我上一次做系統有兩年多了,查看了一下磁盤的情況,window 2003系統 ,系統盤占了45G(滿50G,這個里面還包括4G的虛擬內存),其他硬盤上也堆積了各種文件夾和文件,o(︶︿︶)o 唉,刪了吧,不舍得,要么是自己做的,要么是辛苦弄來的;不刪吧,那就是一堆雜亂無章的垃圾。要怎么辦?
給自己先立下這么個規矩
1.文件夾深度不能超過5
2.不保留0字節文件和文件夾
3.執行磁盤碎片整理(文件壓縮也是把散碎文重組的一個好方式,然后再解壓縮,不懂的就當我是在胡咧咧吧)
究竟這幾步應該如何快速做到呢?
1.文件夾深度不能超過5
cmd 命令有一個 tree , 儂曉得伐?
C:\Users\Administrator>tree "D:\Moontest\Moontest\刪除0大小文件和空文件夾" /f 文件夾 PATH 列表 卷序列號為 A043-7C6D D:\MOONTEST\MOONTEST\刪除0大小文件和空文件夾 │ Program.cs │ 刪除0大小文件和空文件夾.csproj │ ├─bin │ ├─Debug │ │ 刪除0大小文件和空文件夾.exe │ │ 刪除0大小文件和空文件夾.pdb │ │ 刪除0大小文件和空文件夾.vshost.exe │ │ 刪除0大小文件和空文件夾.vshost.exe.manifest │ │ │ └─Release ├─Controls │ MyButton.cs │ ├─obj │ └─x86 │ └─Debug │ │ 1111.csproj.FileListAbsolute.txt │ │ 2048.csproj.FileListAbsolute.txt │ │ DesignTimeResolveAssemblyReferences.cache │ │ DesignTimeResolveAssemblyReferencesInput.cache │ │ GenerateResource.read.1.tlog │ │ GenerateResource.write.1.tlog │ │ ResolveAssemblyReference.cache │ │ 刪除0大小文件和空文件夾.csproj.FileListAbsolute.txt │ │ 刪除0大小文件和空文件夾.exe │ │ 刪除0大小文件和空文件夾.Properties.Resources.resources │ │ │ └─TempPE │ Properties.Resources.Designer.cs.dll │ └─Properties AssemblyInfo.cs Resources.Designer.cs Resources.resx Settings.Designer.cs Settings.settings C:\Users\Administrator>
這個層次分明了,如果實在太多,可以輸入在命令后加上 >> d:\result.txt 即是
tree "D:\Moontest\Moontest\刪除0大小文件和空文件夾" /f >> d:\result.txt
這種方式把結果導出到一個文件中查看。如果深度太大的,可以手動處理下,這個盡量不要寫代碼自動化處理,畢竟你不能把你的思維寫到計算機里面,文件或刪或合並或移位,還是人工的好點,當然我說的是對自己有價值有感情的東西,畢竟某些東西視同己出。
2.不保留0字節文件和文件夾
這個不管我們采用什么樣的系統清理工具,清理工具考慮系統穩定性,一般不會刪除不在“花名冊”中的0字符文件和文件夾。這樣就可能導致我們曾經在安裝刪除時,系統中保留了大量的卸載后余下的垃圾文件(0字符文件和文件夾),久而久之,看到一堆堆我不認識的東西,心里特別不爽,看到一個,干掉一個;但是這種方式也太慢了。要怎么辦呢?自己寫個清理方法吧,呵呵。
我自己提供一下刪除0字符文件和文件夾的小代碼,比較簡單,不解釋代碼了,分成三段,根據喜好索取,我個人偏好Class2

using System; using System.Collections.Generic; using System.IO; namespace 刪除0大小文件和空文件夾 { class Program { /* Class1 和 Class2 的形式是一樣的,只是刪除方式不一樣 Class2 和 Class3 的刪除方式是一樣的,只是形式不一樣 Class1 先檢索出所有文件夾,從末端文件夾開始刪除0字節文件和文件夾 Class2 和 Class3 檢查一個文件夾,先刪除0字節文件,然后檢索文件夾,重復以上過程,最后從末端文件夾開始刪除文件夾 */ static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += (s, e) => { File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "error.log", DateTime.Now.ToString() + "\t" + e.ExceptionObject.ToString() + Environment.NewLine); }; Class1 c1 = new Class1(args); Class2 c2 = new Class2(args); Class3 c3 = new Class3(args); } } public class Class1 { public Class1(string[] args) { Queue<string> queue = new Queue<string>(); Stack<string> stack = new Stack<string>(); if (args.Length > 0) { foreach (var item in args) { queue.Enqueue(item); stack.Push(item); } } else { string rootPath = AppDomain.CurrentDomain.BaseDirectory; queue.Enqueue(rootPath); stack.Push(rootPath); } while (queue.Count > 0) { string curPath = queue.Dequeue(); string[] folders = null; try { folders = Directory.GetDirectories(curPath); } catch { } if (folders != null && folders.Length > 0) { foreach (var item in folders) { queue.Enqueue(item); stack.Push(item); } } } while (stack.Count > 0) { string curPath = stack.Pop(); string[] files = null; try { files = Directory.GetFiles(curPath); } catch { } int filecount = 0; if (files != null && files.Length > 0) { filecount = files.Length; foreach (var item in files) { FileInfo finfo = new FileInfo(item); if (finfo.Length <= 0) { try { finfo.Delete(); filecount--; Console.WriteLine("delete file : " + finfo.FullName); } catch (Exception ex) { Console.WriteLine(ex); } } } } if (filecount == 0) { try { Directory.Delete(curPath); Console.WriteLine("delete folder : " + curPath); } catch (Exception ex) { Console.WriteLine(ex); } } } Console.WriteLine("清理完畢!!!"); Console.ReadKey(); } } public class Class2 { public Class2(string[] args) { Queue<string> queue = new Queue<string>(); Stack<string> stack = new Stack<string>(); if (args.Length > 0) { foreach (var item in args) { queue.Enqueue(item); stack.Push(item); } } else { string rootPath = AppDomain.CurrentDomain.BaseDirectory; queue.Enqueue(rootPath); stack.Push(rootPath); } while (queue.Count > 0) { string curPath = queue.Dequeue(); string[] files = null; try { files = Directory.GetFiles(curPath); } catch { } if (files != null && files.Length > 0) { foreach (var item in files) { FileInfo finfo = new FileInfo(item); if (finfo.Length <= 0) { try { finfo.Delete(); Console.WriteLine("delete file : " + finfo.FullName); } catch (Exception ex) { Console.WriteLine(ex); } } } } string[] folders = null; try { folders = Directory.GetDirectories(curPath); } catch { } if (folders != null && folders.Length > 0) { foreach (var item in folders) { queue.Enqueue(item); stack.Push(item); } } } while (stack.Count > 0) { string curPath = stack.Pop(); try { Directory.Delete(curPath); Console.WriteLine("delete folder : " + curPath); } catch (Exception ex) { Console.WriteLine(ex); } } Console.WriteLine("清理完畢!!!"); Console.ReadKey(); } } public class Class3 { public Class3(string[] args) { if (args.Length == 0) { args = new string[] { AppDomain.CurrentDomain.BaseDirectory }; } foreach (var item in args) Delete(item); Console.WriteLine("清理完畢!!!"); Console.ReadKey(); } public void Delete(string path) { string[] files = null; try { files = Directory.GetFiles(path); } catch { } if (files != null && files.Length > 0) { foreach (var item in files) { FileInfo finfo = new FileInfo(item); if (finfo.Length <= 0) { try { finfo.Delete(); Console.WriteLine("delete file : " + finfo.FullName); } catch (Exception ex) { Console.WriteLine(ex); } } } } string[] folders = null; try { folders = Directory.GetDirectories(path); } catch { } if (folders != null && folders.Length > 0) foreach (var item in folders) Delete(item); try { Directory.Delete(path); Console.WriteLine("delete folder : " + path); } catch (Exception ex) { Console.WriteLine(ex); } } } }
想用拿去編譯一下就好,想清理哪個磁盤或者文件夾,只需把生成程序copy過去run就好,當然也可以加參數指定路徑。例如寫個1.cmd文件,在里面寫入
call ..\這里寫生成程序父路徑\生成程序.exe "c:\" "D:\flash cs6完全自學教程" "E:\Erlang項目" ,
然后運行下1.cmd(別怪小弟太啰嗦)。
現在話題又扯回來了,剛剛我不是還說,很多系統清理工具,為了系統的穩定性,不會刪除非“花名冊”中的0字符文件和文件夾,我現在這么搞,系統穩定性不是大受威脅嗎?
告訴你,如果你用的是個人計算機,盡管刪吧,很多健壯性很好的系統程序都有文件自恢復能力(這一點在開發中很重要,配置文件被誤刪,或者是無法被程序識別,程序要有文件自恢復能力),不怕啦。但是這種方式絕對不允許用來清理正在運行服務器,除非你覺得脖子夠粗,頭掉不下來。
3.執行磁盤碎片整理
這個就簡單了,我喜歡使用defraggler,執行速度非常快,應該和ccleaner出自同一家族;當然也可以使用系統自帶的磁盤整理工具,但是有點偏耗時;如果文件比較大,對連續性要求比較高的話,可以使用壓縮文件的方式。
最終,通過刪除、移動、合並方式。終於把C盤整理出10G剩余空間,其他盤也做了重復文件處理(重復文件處理比較簡單,我這邊是讀取磁盤所有文件,將 文件創建時間和文件大小連接 並 文件路徑,作為key-value插入哈希表,下次遇到重復鍵的,對比下文件特征(當文件創建時間和文件大小相等,從兩個文件中隨機取同位符,匹配是否相等,用來斷定是否同一個文件),是同一個文件刪除重復)。