Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Core Java Code Examples

Kill - Remove files by name

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Kill - Remove files by name import java.io.*; //DANGEROUS Program to remove files matching a name in a directory public class KillFilesByName { public static void main(String[] argv) { if (argv.length != 2) { System.err.println("usage: KillFilesByName dirname pattern"); System.exit(1); } File dir = new File(argv[0]); if (!dir.exists()) { System.out.println(argv[0] + " does not exist"); return; } String patt = argv[1]; String[] info = dir.list(); for (int i=0; i File n = new File(argv[0] + dir.separator + info[i]); if (!n.isFile()) { // skip ., .., other directories, etc. continue; } if (info[i].indexOf(patt) == -1) { // name doesn't match continue; } System.out.println("removing " + n.getPath()); if (!n.delete()) System.err.println("Couldn't remove " + n.getPath()); } } }