如何批量清理系統(tǒng)臨時(shí)文件(語(yǔ)言:C#、 C/C++、 php 、python 、java )
語(yǔ)言之爭(zhēng)由來(lái)已久,下面做一些IO實(shí)驗(yàn)(遍歷9G多的文件,批量刪除),盡量用事實(shí)來(lái)比較誰(shuí)優(yōu)誰(shuí)劣。操作系統(tǒng):win7 64 位,文件包大?。?.68G。
一、語(yǔ)言:C#
開(kāi)發(fā)環(huán)境:vs 2013
代碼總行數(shù):43行
耗時(shí):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();
// 開(kāi)始計(jì)時(shí)
Console.WriteLine("開(kāi)始計(jì)時(shí):"+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("該目錄不存在!");
}
// 計(jì)時(shí)結(jié)束
Console.WriteLine("結(jié)束計(jì)時(shí):" + DateTime.Now.ToString("HH:mm:ss"));
Console.ReadKey();
}
}
}
運(yùn)行效果圖:

二、語(yǔ)言:C/C++
開(kāi)發(fā)環(huán)境:vs 2013
代碼總行數(shù):50行
耗時(shí):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);
// 開(kāi)始計(jì)時(shí)
SYSTEMTIME sys_time; //聲明變量
GetLocalTime(&sys_time); //將變量值設(shè)置為本地時(shí)間
printf("開(kāi)始計(jì)時(shí):%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;
}
// 計(jì)時(shí)結(jié)束
GetLocalTime(&sys_time);
printf("計(jì)時(shí)結(jié)束:%02d:%02d:%02d\n", sys_time.wHour, sys_time.wMinute, sys_time.wSecond);
system("pause");
return 0;
}
運(yùn)行效果圖:

三、語(yǔ)言:PHP
開(kāi)發(fā)環(huán)境:Phpstorm
代碼總行數(shù):32行
耗時(shí):13秒
代碼:
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 16-1-29
* Time: 上午9:31
*/
date_default_timezone_set('prc');
//輸入目錄 e:\tmp
$path = 'e:\tmp';
//開(kāi)始計(jì)時(shí)
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/>';
}
//計(jì)時(shí)結(jié)束
echo date("H:i:s",time()) . '<br/>';
運(yùn)行效果圖:

四、語(yǔ)言:Java
開(kāi)發(fā)環(huán)境:eclipse
代碼總行數(shù):43行
耗時(shí):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();
// 開(kāi)始計(jì)時(shí)
Date nowTime=new Date();
SimpleDateFormat time=new SimpleDateFormat("HH:mm:ss");
System.out.println("開(kāi)始計(jì)時(shí):"+ 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("該目錄不存在!");
}
// 計(jì)時(shí)結(jié)束
nowTime=new Date();
System.out.println("開(kāi)始計(jì)時(shí):"+ time.format(nowTime));
}
}
運(yùn)行效果圖:

五、語(yǔ)言:Python 3.3.5
開(kāi)發(fā)環(huán)境:IDLE
代碼總行數(shù):20行
耗時(shí):10秒
代碼:
# -*- coding: utf-8 -*-
import datetime
import os
# 輸入目錄 e:\tmp
path = input("輸入要清理的目錄:\n");
# 開(kāi)始計(jì)時(shí)
print("開(kāi)始計(jì)時(shí):",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("該目錄不存在!");
# 計(jì)時(shí)結(jié)束
print("結(jié)束計(jì)時(shí):",datetime.datetime.now().strftime('%H:%M:%S'));
運(yùn)行效果圖:

相關(guān)文章
PHP cURL初始化和執(zhí)行方法入門級(jí)代碼
這篇文章主要介紹了PHP cURL初始化和執(zhí)行方法入門級(jí)代碼,本文直接給出代碼示例,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-05-05
簡(jiǎn)單示例AJAX結(jié)合PHP代碼實(shí)現(xiàn)登錄效果代碼
比較簡(jiǎn)單的通過(guò)ajax+php實(shí)現(xiàn)登陸功能,這是個(gè)簡(jiǎn)單的例子,固定字符,實(shí)際應(yīng)用中可以從數(shù)據(jù)庫(kù)中讀取2008-07-07
PHP實(shí)現(xiàn)linux命令tail -f
tail 命令從指定點(diǎn)開(kāi)始將文件寫到標(biāo)準(zhǔn)輸出.使用tail命令的-f選項(xiàng)可以方便的查閱正在改變的日志文件,tail -f filename會(huì)把filename里最尾部的內(nèi)容顯示在屏幕上,并且不但刷新,使你看到最新的文件內(nèi)容.接下來(lái)通過(guò)本文給大家介紹PHP實(shí)現(xiàn)linux命令tail -f,需要的朋友參考下2016-02-02
thinkPHP框架樂(lè)觀鎖和悲觀鎖實(shí)例分析
這篇文章主要介紹了thinkPHP框架樂(lè)觀鎖和悲觀鎖,結(jié)合實(shí)例形式分析了框架樂(lè)觀鎖和悲觀鎖的原理及thinkPHP相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-10-10
詳解PHP中strlen和mb_strlen函數(shù)的區(qū)別
在PHP中,strlen與mb_strlen是求字符串長(zhǎng)度的函數(shù),但是對(duì)于一些初學(xué)者來(lái)說(shuō),如果不看手冊(cè),也許不太清楚其中的區(qū)別2014-03-03
PHP輸出緩沖控制Output Control系列函數(shù)詳解
這篇文章主要介紹了PHP輸出緩沖控制Output Control系列函數(shù)詳解,本文講解了輸出緩沖的簡(jiǎn)介、輸出緩沖的作用、php.ini 中的相關(guān)配置項(xiàng)、Output Control 函數(shù)詳解等內(nèi)容,需要的朋友可以參考下2015-07-07
PHP中字符與字節(jié)的區(qū)別及字符串與字節(jié)轉(zhuǎn)換示例
在php中字符是可使用多種不同字符方案或代碼頁(yè)來(lái)表示的抽象實(shí)體。字節(jié)是通過(guò)網(wǎng)絡(luò)傳輸信息(或在硬盤或內(nèi)存中存儲(chǔ)信息)的單位。本文還通過(guò)實(shí)例給大家介紹了php中字符串與字節(jié)轉(zhuǎn)換示例,感興趣的朋友一起看看吧2016-10-10
SSO單點(diǎn)登錄的PHP實(shí)現(xiàn)方法(Laravel框架)
這篇文章主要介紹了SSO單點(diǎn)登錄的PHP實(shí)現(xiàn)方法(Laravel框架) 的相關(guān)資料,需要的朋友可以參考下2016-03-03

