Table of Contents
如何批量清理系统临时文件(语言:C#、 C/C++、 php 、python 、java ),
您可能感兴趣的文章:
Home php教程 php手册 如何批量清理系统临时文件(语言:C#、 C/C++、 php 、python 、java ),

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

Jun 13, 2016 am 08:46 AM
document clean up system

如何批量清理系统临时文件(语言: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();
}
}
}
Copy after login

运行效果图:


二、语言: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;
}
Copy after login

运行效果图:


三、语言: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/>'; 
Copy after login

运行效果图:


四、语言: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)); 
}
}
Copy after login

运行效果图:

五、语言: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)
Copy after login

else:

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

运行效果图:

您可能感兴趣的文章:

  • PowerShell脚本清理指定天数前的临时文件夹实现代码
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CUDA's universal matrix multiplication: from entry to proficiency! CUDA's universal matrix multiplication: from entry to proficiency! Mar 25, 2024 pm 12:30 PM

General Matrix Multiplication (GEMM) is a vital part of many applications and algorithms, and is also one of the important indicators for evaluating computer hardware performance. In-depth research and optimization of the implementation of GEMM can help us better understand high-performance computing and the relationship between software and hardware systems. In computer science, effective optimization of GEMM can increase computing speed and save resources, which is crucial to improving the overall performance of a computer system. An in-depth understanding of the working principle and optimization method of GEMM will help us better utilize the potential of modern computing hardware and provide more efficient solutions for various complex computing tasks. By optimizing the performance of GEMM

What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. Mar 21, 2024 pm 09:17 PM

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box &quot;Error 0x80004005: Unspecified Error&quot; will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

Huawei's Qiankun ADS3.0 intelligent driving system will be launched in August and will be launched on Xiangjie S9 for the first time Huawei's Qiankun ADS3.0 intelligent driving system will be launched in August and will be launched on Xiangjie S9 for the first time Jul 30, 2024 pm 02:17 PM

On July 29, at the roll-off ceremony of AITO Wenjie's 400,000th new car, Yu Chengdong, Huawei's Managing Director, Chairman of Terminal BG, and Chairman of Smart Car Solutions BU, attended and delivered a speech and announced that Wenjie series models will be launched this year In August, Huawei Qiankun ADS 3.0 version was launched, and it is planned to successively push upgrades from August to September. The Xiangjie S9, which will be released on August 6, will debut Huawei’s ADS3.0 intelligent driving system. With the assistance of lidar, Huawei Qiankun ADS3.0 version will greatly improve its intelligent driving capabilities, have end-to-end integrated capabilities, and adopt a new end-to-end architecture of GOD (general obstacle identification)/PDP (predictive decision-making and control) , providing the NCA function of smart driving from parking space to parking space, and upgrading CAS3.0

How to transfer files from Quark Cloud Disk to Baidu Cloud Disk? How to transfer files from Quark Cloud Disk to Baidu Cloud Disk? Mar 14, 2024 pm 02:07 PM

Quark Netdisk and Baidu Netdisk are currently the most commonly used Netdisk software for storing files. If you want to save the files in Quark Netdisk to Baidu Netdisk, how do you do it? In this issue, the editor has compiled the tutorial steps for transferring files from Quark Network Disk computer to Baidu Network Disk. Let’s take a look at how to operate it. How to save Quark network disk files to Baidu network disk? To transfer files from Quark Network Disk to Baidu Network Disk, you first need to download the required files from Quark Network Disk, then select the target folder in the Baidu Network Disk client and open it. Then, drag and drop the files downloaded from Quark Cloud Disk into the folder opened by the Baidu Cloud Disk client, or use the upload function to add the files to Baidu Cloud Disk. Make sure to check whether the file was successfully transferred in Baidu Cloud Disk after the upload is completed. That's it

What is hiberfil.sys file? Can hiberfil.sys be deleted? What is hiberfil.sys file? Can hiberfil.sys be deleted? Mar 15, 2024 am 09:49 AM

Recently, many netizens have asked the editor, what is the file hiberfil.sys? Can hiberfil.sys take up a lot of C drive space and be deleted? The editor can tell you that the hiberfil.sys file can be deleted. Let’s take a look at the details below. hiberfil.sys is a hidden file in the Windows system and also a system hibernation file. It is usually stored in the root directory of the C drive, and its size is equivalent to the size of the system's installed memory. This file is used when the computer is hibernated and contains the memory data of the current system so that it can be quickly restored to the previous state during recovery. Since its size is equal to the memory capacity, it may take up a larger amount of hard drive space. hiber

Which version of Apple 16 system is the best? Which version of Apple 16 system is the best? Mar 08, 2024 pm 05:16 PM

The best version of the Apple 16 system is iOS16.1.4. The best version of the iOS16 system may vary from person to person. The additions and improvements in daily use experience have also been praised by many users. Which version of the Apple 16 system is the best? Answer: iOS16.1.4 The best version of the iOS 16 system may vary from person to person. According to public information, iOS16, launched in 2022, is considered a very stable and performant version, and users are quite satisfied with its overall experience. In addition, the addition of new features and improvements in daily use experience in iOS16 have also been well received by many users. Especially in terms of updated battery life, signal performance and heating control, user feedback has been relatively positive. However, considering iPhone14

Always new! Huawei Mate60 series upgrades to HarmonyOS 4.2: AI cloud enhancement, Xiaoyi Dialect is so easy to use Always new! Huawei Mate60 series upgrades to HarmonyOS 4.2: AI cloud enhancement, Xiaoyi Dialect is so easy to use Jun 02, 2024 pm 02:58 PM

On April 11, Huawei officially announced the HarmonyOS 4.2 100-machine upgrade plan for the first time. This time, more than 180 devices will participate in the upgrade, covering mobile phones, tablets, watches, headphones, smart screens and other devices. In the past month, with the steady progress of the HarmonyOS4.2 100-machine upgrade plan, many popular models including Huawei Pocket2, Huawei MateX5 series, nova12 series, Huawei Pura series, etc. have also started to upgrade and adapt, which means that there will be More Huawei model users can enjoy the common and often new experience brought by HarmonyOS. Judging from user feedback, the experience of Huawei Mate60 series models has improved in all aspects after upgrading HarmonyOS4.2. Especially Huawei M

Detailed explanation of the role of .ibd files in MySQL and related precautions Detailed explanation of the role of .ibd files in MySQL and related precautions Mar 15, 2024 am 08:00 AM

Detailed explanation of the role of .ibd files in MySQL and related precautions MySQL is a popular relational database management system, and the data in the database is stored in different files. Among them, the .ibd file is a data file in the InnoDB storage engine, used to store data and indexes in tables. This article will provide a detailed analysis of the role of the .ibd file in MySQL and provide relevant code examples to help readers better understand. 1. The role of .ibd files: storing data: .ibd files are InnoDB storage

See all articles