How to clean up temporary system files in batches (languages: C#, C/C++, php, python, java),
The language debate has been around for a long time. Let’s do some IO experiments (traversal Files with more than 9G, deleted in batches), try to use facts to compare who is better and who is worse. Operating system: win7 64-bit, file package size: 9.68G.
1. Language: C#
Development environment: vs 2013
Total number of lines of code: 43 lines
Time taken: 7 seconds
Code:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespaceBatchDelete { class Program { static void Main(string[] args) { //Input directory e:\tmp string path; Console.WriteLine("Enter the directory to be cleaned:"); path = Console.ReadLine(); // start the timer Console.WriteLine("Start timing:"+DateTime.Now.ToString("HH:mm:ss")); // First traverse the matching search and then loop to delete if (Directory.Exists(path)) { Console.Write("Deleting"); foreach (string fileName in Directory.GetFileSystemEntries(path)) { if (File.Exists(fileName) && fileName.Contains("cachegrind.out")) { File.Delete(fileName); } } Console.WriteLine(""); } else { Console.WriteLine("This directory does not exist!"); } // Timer ends Console.WriteLine("End time:" + DateTime.Now.ToString("HH:mm:ss")); Console.ReadKey(); } } }
Operation renderings:
2. Language: C/C++
Development environment: vs 2013
Total number of lines of code: 50 lines
Time taken: 36 seconds
Code:
#include #include #include #include #include #include #include using namespace std; int main(int argc, char * argv[]) { //Input directory e:\tmp string strPath; cout <<"Enter the directory to be cleaned:" <<endl; getline(cin, strPath); // start the timer SYSTEMTIME sys_time; //Declare variables GetLocalTime(&sys_time); //Set the variable value to local time printf("Start timing: %02d:%02d:%02d\n", sys_time.wHour,sys_time.wMinute,sys_time.wSecond); // First traverse the matching search and then loop to delete 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 <path()) && boost::contains(item_begin->path().string(), "cachegrind.out")) { fs::remove(item_begin->path()); } } } cout <<"" <<endl; } else { cout <<"This directory does not exist!" <<endl; } // Timer ends GetLocalTime(&sys_time); printf("Timeout: %02d:%02d:%02d\n", sys_time.wHour, sys_time.wMinute, sys_time.wSecond); system("pause"); return 0; }
Operation renderings:
3. Language: PHP
Development environment: Phpstorm
Total number of lines of code: 32 lines
Time taken: 13 seconds
Code:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 16-1-29 * Time: 9:31 AM */ date_default_timezone_set('prc'); //Input directory e:\tmp $path = 'e:\tmp'; //start the timer echo date("H:i:s",time()) . '
'; //First traverse the matching search and then loop to delete if(is_dir($path)) { echo "Deleting"; $mydir = dir($path); while($file = $mydir->read()) { if(file_exists("$path/$file") && strpos($file, 'cachegrind.out') === 0) { unlink("$path/$file"); } } echo '
'; } else { echo "This directory does not exist!" . '
'; } //End of timer echo date("H:i:s",time()) . '
';
Operation renderings:
4. Language: Java
Development environment: eclipse
Total number of lines of code: 43 lines
Time taken: 10 seconds
Code:
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); //Input directory e:\tmp String path = null; System.out.println("Enter the directory to be cleaned:"); path = s.next(); // start the timer Date nowTime=new Date(); SimpleDateFormat time=new SimpleDateFormat("HH:mm:ss"); System.out.println("Start timing:"+ time.format(nowTime)); // First traverse the matching search and then loop to delete File dir = new File(path); if(dir.exists()){ System.out.print("Deleting"); 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("This directory does not exist!"); } // Timer ends nowTime=new Date(); System.out.println("Start timing:"+ time.format(nowTime)); } }
Operation renderings:
5. Language: Python 3.3.5
Development environment: IDLE
Total number of lines of code: 20 lines
Time taken: 10 seconds
Code:
# -*- coding: utf-8 -*- import datetime import os # Input directory e:\tmp path = input("Enter the directory to be cleaned:\n"); # start the timer print("Start timing:",datetime.datetime.now().strftime('%H:%M:%S')); # First traverse the matching search and then delete in a loop if(os.path.exists(path)): print("Deleting"); 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("This directory does not exist!"); # End of timer print("End time:",datetime.datetime.now().strftime('%H:%M:%S'));
Operation renderings:
Articles you may be interested in:
- PowerShell script to clean up temporary folders that are a specified number of days ago. Implementation code
http://www.bkjia.com/PHPjc/1098285.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1098285.htmlTechArticleHow to clean up system temporary files in batches (languages: C#, C/C++, php, python, java). The language debate has been going on for a long time. Let’s do some IO experiments (traverse more than 9G files and delete them in batches). ..