如何批量清理系統(tǒng)臨時文件(語言:C#、 C/C++、 php 、python 、java )
語言之爭由來已久,下面做一些IO實驗(遍歷9G多的文件,批量刪除),盡量用事實來比較誰優(yōu)誰劣。操作系統(tǒng):win7 64 位,文件包大小:9.68G。
一、語言:C#
開發(fā)環(huán)境:vs 2013
代碼總行數(shù):43行
耗時:7秒
代碼:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BatchDelete { class Program { static void Main(string[] args) { // 輸入目錄 e:\tmp string path; Console.WriteLine("輸入要清理的目錄:"); path = Console.ReadLine(); // 開始計時 Console.WriteLine("開始計時:"+DateTime.Now.ToString("HH:mm:ss")); // 先遍歷匹配查找再循環(huán)刪除 if (Directory.Exists(path)) { Console.Write("正在刪除"); foreach (string fileName in Directory.GetFileSystemEntries(path)) { if (File.Exists(fileName) && fileName.Contains("cachegrind.out")) { File.Delete(fileName); } } Console.WriteLine(""); } else { Console.WriteLine("該目錄不存在!"); } // 計時結(jié)束 Console.WriteLine("結(jié)束計時:" + DateTime.Now.ToString("HH:mm:ss")); Console.ReadKey(); } } }
運行效果圖:
二、語言:C/C++
開發(fā)環(huán)境:vs 2013
代碼總行數(shù):50行
耗時:36秒
代碼:
#include <iostream> #include <string> #include <Windows.h> #include <boost\filesystem\operations.hpp> #include <boost\filesystem\path.hpp> #include <boost\filesystem\convenience.hpp> #include <boost\algorithm\string.hpp> using namespace std; int main(int argc, char * argv[]) { // 輸入目錄 e:\tmp string strPath; cout << "輸入要清理的目錄:" << endl; getline(cin, strPath); // 開始計時 SYSTEMTIME sys_time; //聲明變量 GetLocalTime(&sys_time); //將變量值設(shè)置為本地時間 printf("開始計時:%02d:%02d:%02d\n", sys_time.wHour,sys_time.wMinute,sys_time.wSecond); // 先遍歷匹配查找再循環(huán)刪除 namespace fs = boost::filesystem; fs::path full_path(fs::initial_path()); full_path = fs::system_complete(fs::path(strPath, fs::native)); if (fs::exists(full_path)) { cout << "正在刪除" ; fs::directory_iterator item_begin(full_path); fs::directory_iterator item_end; for (; item_begin != item_end; item_begin++) { if (!fs::is_directory(*item_begin)) { if (fs::exists(item_begin->path()) && boost::contains(item_begin->path().string(), "cachegrind.out")) { fs::remove(item_begin->path()); } } } cout << "" << endl; } else { cout << "該目錄不存在!" << endl; } // 計時結(jié)束 GetLocalTime(&sys_time); printf("計時結(jié)束:%02d:%02d:%02d\n", sys_time.wHour, sys_time.wMinute, sys_time.wSecond); system("pause"); return 0; }
運行效果圖:
三、語言:PHP
開發(fā)環(huán)境:Phpstorm
代碼總行數(shù):32行
耗時:13秒
代碼:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 16-1-29 * Time: 上午9:31 */ date_default_timezone_set('prc'); //輸入目錄 e:\tmp $path = 'e:\tmp'; //開始計時 echo date("H:i:s",time()) . '<br/>'; //先遍歷匹配查找再循環(huán)刪除 if(is_dir($path)) { echo "正在刪除"; $mydir = dir($path); while($file = $mydir->read()) { if(file_exists("$path/$file") && strpos($file, 'cachegrind.out') === 0) { unlink("$path/$file"); } } echo '<br/>'; } else { echo "該目錄不存在!" . '<br/>'; } //計時結(jié)束 echo date("H:i:s",time()) . '<br/>';
運行效果圖:
四、語言:Java
開發(fā)環(huán)境:eclipse
代碼總行數(shù):43行
耗時:10秒
代碼:
package com.yejing; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner s = new Scanner(System.in); // 輸入目錄 e:\tmp String path = null; System.out.println("輸入要清理的目錄:"); path = s.next(); // 開始計時 Date nowTime=new Date(); SimpleDateFormat time=new SimpleDateFormat("HH:mm:ss"); System.out.println("開始計時:"+ time.format(nowTime)); // 先遍歷匹配查找再循環(huán)刪除 File dir = new File(path); if(dir.exists()){ System.out.print("正在刪除"); File[] fs = dir.listFiles(); for(int i=0;i<fs.length;i++){ if(!fs[i].isDirectory()){ if(fs[i].isFile() && fs[i].exists() && fs[i].getName().contains("cachegrind.out")) { fs[i].delete(); } } } System.out.println(""); }else{ System.out.println("該目錄不存在!"); } // 計時結(jié)束 nowTime=new Date(); System.out.println("開始計時:"+ time.format(nowTime)); } }
運行效果圖:
五、語言:Python 3.3.5
開發(fā)環(huán)境:IDLE
代碼總行數(shù):20行
耗時:10秒
代碼:
# -*- coding: utf-8 -*- import datetime import os # 輸入目錄 e:\tmp path = input("輸入要清理的目錄:\n"); # 開始計時 print("開始計時:",datetime.datetime.now().strftime('%H:%M:%S')); # 先遍歷匹配查找再循環(huán)刪除 if(os.path.exists(path)): print("正在刪除"); for parent,dirnames,filenames in os.walk(path): for filename in filenames: targetFile = os.path.join(parent,filename) if (os.path.isfile(targetFile) and "cachegrind.out" in targetFile): os.remove(targetFile)
else:
print("該目錄不存在!"); # 計時結(jié)束 print("結(jié)束計時:",datetime.datetime.now().strftime('%H:%M:%S'));
運行效果圖:
相關(guān)文章
簡單示例AJAX結(jié)合PHP代碼實現(xiàn)登錄效果代碼
比較簡單的通過ajax+php實現(xiàn)登陸功能,這是個簡單的例子,固定字符,實際應用中可以從數(shù)據(jù)庫中讀取2008-07-07詳解PHP中strlen和mb_strlen函數(shù)的區(qū)別
在PHP中,strlen與mb_strlen是求字符串長度的函數(shù),但是對于一些初學者來說,如果不看手冊,也許不太清楚其中的區(qū)別2014-03-03PHP輸出緩沖控制Output Control系列函數(shù)詳解
這篇文章主要介紹了PHP輸出緩沖控制Output Control系列函數(shù)詳解,本文講解了輸出緩沖的簡介、輸出緩沖的作用、php.ini 中的相關(guān)配置項、Output Control 函數(shù)詳解等內(nèi)容,需要的朋友可以參考下2015-07-07PHP中字符與字節(jié)的區(qū)別及字符串與字節(jié)轉(zhuǎn)換示例
在php中字符是可使用多種不同字符方案或代碼頁來表示的抽象實體。字節(jié)是通過網(wǎng)絡傳輸信息(或在硬盤或內(nèi)存中存儲信息)的單位。本文還通過實例給大家介紹了php中字符串與字節(jié)轉(zhuǎn)換示例,感興趣的朋友一起看看吧2016-10-10SSO單點登錄的PHP實現(xiàn)方法(Laravel框架)
這篇文章主要介紹了SSO單點登錄的PHP實現(xiàn)方法(Laravel框架) 的相關(guān)資料,需要的朋友可以參考下2016-03-03