当前位置:Gxlcms > php框架 > 如何批量清理系统临时文件(语言:C#、 C/C++、 php 、python 、java )

如何批量清理系统临时文件(语言:C#、 C/C++、 php 、python 、java )

时间:2021-07-01 10:21:17 帮助过:41人阅读

语言之争由来已久,下面做一些IO实验(遍历9G多的文件,批量删除),尽量用事实来比较谁优谁劣。操作系统:win7 64 位,文件包大小:9.68G。

一、语言:C#

开发环境:vs 2013

代码总行数:43行

耗时:7秒

代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BatchDelete
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. // 输入目录 e:\tmp
  14. string path;
  15. Console.WriteLine("输入要清理的目录:");
  16. path = Console.ReadLine();
  17. // 开始计时
  18. Console.WriteLine("开始计时:"+DateTime.Now.ToString("HH:mm:ss"));
  19. // 先遍历匹配查找再循环删除
  20. if (Directory.Exists(path))
  21. {
  22. Console.Write("正在删除");
  23. foreach (string fileName in Directory.GetFileSystemEntries(path))
  24. {
  25. if (File.Exists(fileName) && fileName.Contains("cachegrind.out"))
  26. {
  27. File.Delete(fileName);
  28. }
  29. }
  30. Console.WriteLine("");
  31. }
  32. else
  33. {
  34. Console.WriteLine("该目录不存在!");
  35. }
  36. // 计时结束
  37. Console.WriteLine("结束计时:" + DateTime.Now.ToString("HH:mm:ss"));
  38. Console.ReadKey();
  39. }
  40. }
  41. }

运行效果图:


二、语言:C/C++

开发环境:vs 2013

代码总行数:50行

耗时:36秒

代码:

  1. #include <iostream>
  2. #include <string>
  3. #include <Windows.h>
  4. #include <boost\filesystem\operations.hpp>
  5. #include <boost\filesystem\path.hpp>
  6. #include <boost\filesystem\convenience.hpp>
  7. #include <boost\algorithm\string.hpp>
  8. using namespace std;
  9. int main(int argc, char * argv[])
  10. {
  11. // 输入目录 e:\tmp
  12. string strPath;
  13. cout << "输入要清理的目录:" << endl;
  14. getline(cin, strPath);
  15. // 开始计时
  16. SYSTEMTIME sys_time; //声明变量
  17. GetLocalTime(&sys_time); //将变量值设置为本地时间
  18. printf("开始计时:%02d:%02d:%02d\n", sys_time.wHour,sys_time.wMinute,sys_time.wSecond);
  19. // 先遍历匹配查找再循环删除
  20. namespace fs = boost::filesystem;
  21. fs::path full_path(fs::initial_path());
  22. full_path = fs::system_complete(fs::path(strPath, fs::native));
  23. if (fs::exists(full_path))
  24. {
  25. cout << "正在删除" ;
  26. fs::directory_iterator item_begin(full_path);
  27. fs::directory_iterator item_end;
  28. for (; item_begin != item_end; item_begin++)
  29. {
  30. if (!fs::is_directory(*item_begin))
  31. {
  32. if (fs::exists(item_begin->path()) && boost::contains(item_begin->path().string(), "cachegrind.out"))
  33. {
  34. fs::remove(item_begin->path());
  35. }
  36. }
  37. }
  38. cout << "" << endl;
  39. }
  40. else
  41. {
  42. cout << "该目录不存在!" << endl;
  43. }
  44. // 计时结束
  45. GetLocalTime(&sys_time);
  46. printf("计时结束:%02d:%02d:%02d\n", sys_time.wHour, sys_time.wMinute, sys_time.wSecond);
  47. system("pause");
  48. return 0;
  49. }

运行效果图:


三、语言:PHP

开发环境:Phpstorm

代码总行数:32行

耗时:13秒

代码:

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 16-1-29
  6. * Time: 上午9:31
  7. */
  8. date_default_timezone_set('prc');
  9. //输入目录 e:\tmp
  10. $path = 'e:\tmp';
  11. //开始计时
  12. echo date("H:i:s",time()) . '<br/>';
  13. //先遍历匹配查找再循环删除
  14. if(is_dir($path))
  15. {
  16. echo "正在删除";
  17. $mydir = dir($path);
  18. while($file = $mydir->read())
  19. {
  20. if(file_exists("$path/$file") && strpos($file, 'cachegrind.out') === 0)
  21. {
  22. unlink("$path/$file");
  23. }
  24. }
  25. echo '<br/>';
  26. }
  27. else
  28. {
  29. echo "该目录不存在!" . '<br/>';
  30. }
  31. //计时结束
  32. echo date("H:i:s",time()) . '<br/>';

运行效果图:


四、语言:Java

开发环境:eclipse

代码总行数:43行

耗时:10秒

代码:

  1. package com.yejing;
  2. import java.io.File;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.Scanner;
  6. public class Test {
  7. public static void main(String[] args) {
  8. Scanner s = new Scanner(System.in);
  9. // 输入目录 e:\tmp
  10. String path = null;
  11. System.out.println("输入要清理的目录:");
  12. path = s.next();
  13. // 开始计时
  14. Date nowTime=new Date();
  15. SimpleDateFormat time=new SimpleDateFormat("HH:mm:ss");
  16. System.out.println("开始计时:"+ time.format(nowTime));
  17. // 先遍历匹配查找再循环删除
  18. File dir = new File(path);
  19. if(dir.exists()){
  20. System.out.print("正在删除");
  21. File[] fs = dir.listFiles();
  22. for(int i=0;i<fs.length;i++){
  23. if(!fs[i].isDirectory()){
  24. if(fs[i].isFile() && fs[i].exists() && fs[i].getName().contains("cachegrind.out"))
  25. {
  26. fs[i].delete();
  27. }
  28. }
  29. }
  30. System.out.println("");
  31. }else{
  32. System.out.println("该目录不存在!");
  33. }
  34. // 计时结束
  35. nowTime=new Date();
  36. System.out.println("开始计时:"+ time.format(nowTime));
  37. }
  38. }

运行效果图:

五、语言:Python 3.3.5

开发环境:IDLE

代码总行数:20行

耗时:10秒

代码:

  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. import os
  4. # 输入目录 e:\tmp
  5. path = input("输入要清理的目录:\n");
  6. # 开始计时
  7. print("开始计时:",datetime.datetime.now().strftime('%H:%M:%S'));
  8. # 先遍历匹配查找再循环删除
  9. if(os.path.exists(path)):
  10. print("正在删除");
  11. for parent,dirnames,filenames in os.walk(path):
  12. for filename in filenames:
  13. targetFile = os.path.join(parent,filename)
  14. if (os.path.isfile(targetFile) and "cachegrind.out" in targetFile):
  15. os.remove(targetFile)

else:

  1. print("该目录不存在!");
  2. # 计时结束
  3. print("结束计时:",datetime.datetime.now().strftime('%H:%M:%S'));

运行效果图:

人气教程排行