杀杀杀

这两天公司闹蠕虫,会在每个目录里放一个_desktop.ini文件。杀毒软件可以杀掉病毒,但是不会清除文件。今天自己机器也感染了,写了个小工具做这个清除工作,竟然删掉了十一万个残留文件 。代码如下:
import java.io.File;
import java.io.FileFilter;

public class FileCleaner {
	//file name to be deleted
	private String fileName = null;
	//deleted file number counter
	private int counter = 0;
	
	public FileCleaner(String fileName){
		if(fileName == null || fileName.length() <= 0)
			throw new IllegalArgumentException();
		
		this.fileName = fileName;
	}
	
	/**
	 * entry method
	 * 
	 * @param path directory to be processed
	 */
	public void cleanFile(String path){
		File dir = new File(path);
		
		if(!dir.isDirectory())
			return;
		
		processDir(dir);
	}
	
	/**
	 * returns the deleted file number
	 * 
	 * @return
	 */
	public int getDeletedFileCount(){
		return counter;
	}

	private void processDir(File tmp) {
		System.out.println("Start to check " + tmp.getAbsolutePath());
		File[] acceptedFiles = tmp.listFiles(new FileFilter(){
			public boolean accept(File f){
				if(f.isDirectory() || fileName.equals(f.getName()))
					return true;
				else
					return false;
			}
		});
		
		if(acceptedFiles != null && acceptedFiles.length > 0){
			for(int n=0;n<acceptedFiles.length;n++){
				if(acceptedFiles[n].isDirectory()){
					processDir(acceptedFiles[n]);
				}
				else{
					if(fileName.equals(acceptedFiles[n].getName())){
						acceptedFiles[n].delete();
						counter++;
						
						System.out.println(tmp.getAbsolutePath() + "\\" + fileName + " is deleted!");
					}
				}
			}
		}
	}
	
	//----------------------------------------------------------
	
	private static String help(){
		StringBuffer help = new StringBuffer(128);
		
		help.append("Delete all files with given name in the specified directory and its subdirectories. \n\n");
		help.append("command [file name] [dir] [dir] [dir] \n\n");
		help.append("For example: \n");
		help.append("    java -jar FileCleaner.jar _desktop.ini c:/ d:/ e:/");
		
		
		return help.toString();
	}
	
	public static void main(String[] args){
		if(args == null || args.length <= 1){
			System.err.println(help());
			System.exit(0);
		}
		
		FileCleaner fc = new FileCleaner(args[0]);
		
		for(int n=1;n<args.length;n++){
			fc.cleanFile(args[n]);
		}
		
		System.out.println("Finish deleting " + args[0] + ", total " + fc.getDeletedFileCount() + " deleted!");
	}
}

你可能感兴趣的:(C++,c,C#,F#,Ruby)