如何批量清理系統臨時檔案(語言:C#、 C/C++、 php 、python 、java ),_PHP教程

來源:互聯網
上載者:User

如何批量清理系統臨時檔案(語言:C#、 C/C++、 php 、python 、java ),


語言之爭由來已久,下面做一些IO實驗(遍曆9G多的檔案,大量刪除),盡量用事實來比較誰優誰劣。作業系統:win7 64 位元,檔案包大小:9.68G。

一、語言:C#

開發環境:vs 2013

代碼總行數: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:\tmpstring path;Console.WriteLine("輸入要清理的目錄:");path = Console.ReadLine();// 開始計時Console.WriteLine("開始計時:"+DateTime.Now.ToString("HH:mm:ss"));// 先遍曆匹配尋找再迴圈刪除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("該目錄不存在!");}// 計時結束Console.WriteLine("結束計時:" + DateTime.Now.ToString("HH:mm:ss"));Console.ReadKey();}}}

運行:


二、語言:C/C++

開發環境:vs 2013

代碼總行數:50行

耗時:36秒

代碼:

#include #include #include #include #include #include #include using namespace std;int main(int argc, char * argv[]){// 輸入目錄 e:\tmpstring strPath;cout << "輸入要清理的目錄:" << endl;getline(cin, strPath);// 開始計時 SYSTEMTIME sys_time; //聲明變數GetLocalTime(&sys_time); //將變數值設定為本地時間printf("開始計時:%02d:%02d:%02d\n", sys_time.wHour,sys_time.wMinute,sys_time.wSecond);// 先遍曆匹配尋找再迴圈刪除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;}// 計時結束GetLocalTime(&sys_time);printf("計時結束:%02d:%02d:%02d\n", sys_time.wHour, sys_time.wMinute, sys_time.wSecond);system("pause");return 0;}

運行:


三、語言:PHP

開發環境:Phpstorm

代碼總行數: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()) . '
';//先遍曆匹配尋找再迴圈刪除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 '
';}else{echo "該目錄不存在!" . '
';}//計時結束echo date("H:i:s",time()) . '
';

運行:


四、語言:Java

開發環境:eclipse

代碼總行數: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:\tmpString 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)); // 先遍曆匹配尋找再迴圈刪除File dir = new File(path);if(dir.exists()){System.out.print("正在刪除");File[] fs = dir.listFiles();for(int i=0;i

運行:

五、語言:Python 3.3.5

開發環境:IDLE

代碼總行數:20行

耗時:10秒

代碼:

# -*- coding: utf-8 -*- import datetimeimport os# 輸入目錄 e:\tmppath = input("輸入要清理的目錄:\n");# 開始計時print("開始計時:",datetime.datetime.now().strftime('%H:%M:%S'));# 先遍曆匹配尋找再迴圈刪除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("該目錄不存在!");# 計時結束print("結束計時:",datetime.datetime.now().strftime('%H:%M:%S')); 

運行:

您可能感興趣的文章:

  • PowerShell指令碼清理指定天數前的臨時檔案夾實現代碼

http://www.bkjia.com/PHPjc/1098285.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1098285.htmlTechArticle如何批量清理系統臨時檔案(語言:C#、 C/C++、 php 、python 、java ), 語言之爭由來已久,下面做一些IO實驗(遍曆9G多的檔案,大量刪除),...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.