搜索
首页php教程php手册如何批量清理系统临时文件(语言:C#、 C/C++、 php 、python 、java ),

如何批量清理系统临时文件(语言: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:\tmp
string 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 <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); //将变量值设置为本地时间
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秒

代码:

<&#63;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/>'; 

运行效果图:


四、语言: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:\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)); 
// 先遍历匹配查找再循环删除
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)); 
}
}

运行效果图:

五、语言:Python 3.3.5

开发环境:IDLE

代码总行数:20行

耗时:10秒

代码:

# -*- coding: utf-8 -*- 
import datetime
import os
# 输入目录 e:\tmp
path = 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脚本清理指定天数前的临时文件夹实现代码
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具