国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

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

2024-05-04 23:42:40
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了如何批量清理系統臨時文件(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/C++、 php 、python 、java )

二、語言:C/C++

開發環境:vs 2013

代碼總行數: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:/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;}

運行效果圖:

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

三、語言: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()) . '<br/>';//先遍歷匹配查找再循環刪除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/>';}//計時結束echo date("H:i:s",time()) . '<br/>'; 

運行效果圖:

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

四、語言: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<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("該目錄不存在!");}// 計時結束nowTime=new Date(); System.out.println("開始計時:"+ time.format(nowTime)); }}

運行效果圖:

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

五、語言: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')); 

運行效果圖:

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



注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 舞钢市| 镇原县| 且末县| 福泉市| 洪泽县| 米林县| 岫岩| 韶山市| 诸暨市| 荣成市| 岳阳市| 叙永县| 徐闻县| 密山市| 南川市| 长葛市| 洛扎县| 开封市| 孝义市| 沁阳市| 江安县| 岳西县| 宜良县| 和静县| 巴马| 大港区| 阜平县| 历史| 延边| 乐业县| 赣州市| 涡阳县| 商都县| 彰化市| 芒康县| 瑞昌市| 云霄县| 广昌县| 北宁市| 天津市| 大关县|