Results 1 to 3 of 3
- 11-15-2009, 11:11 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 43
- Rep Power
- 0
Best way to delete all files with a prefix?
So when I start my program I need to delete any Files it may have created last run. There will always be one called output.txt so I just use:
There can also be any number of files that will Start with the word phase and then be followed by their phase number and individual ID numbers with the extention .txt.Java Code:new File("output.txt").delete();
What is the best way to delete all files with the folder that begin with the prefix "phase"?
I was going to just search for phase files squentialy since they are increasingly numbered and delete them untill I can not find anymore but there must be a better way.
Thank you for any help.
- 11-16-2009, 12:38 AM #2
Are the files in a specific folder? Are you trying to delete the entire folder? Or do you want to only delete specific files?
Regardless, check out the file.listFiles() method. If the "file" specifies a directory, the returned list is a File array of ALL files in that folder.
From then, you can run a loop and delete only the files that you want. Or, you could use one of the other listFiles methods, which filters the list to only include what you want.Last edited by CodesAway; 11-16-2009 at 12:43 AM.
CodesAway - codesaway.info
writing tools that make writing code a little easier
- 11-16-2009, 01:09 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 43
- Rep Power
- 0
Worked Great Thanks!
If anyone else is intrested this is what I did;
Java Code://Delete the file that will always be there new File(args[6]).delete(); //Get a list of Files in my current Dir File[] dirFiles = new File(".").listFiles(); //Search Through the list for (int i=0; i<dirFiles.length; i++) //If the Files starts with the word "phase" if (dirFiles[i].getName().startsWith("phase", 0)) //Delete This file new File(dirFiles[i].getName()).delete();
Similar Threads
-
Infix to Prefix
By Sasarai in forum Advanced JavaReplies: 4Last Post: 12-08-2010, 03:57 PM -
How to delete a JLabel by using the keyboard 'delete' key?
By Suren in forum AWT / SwingReplies: 2Last Post: 04-20-2009, 08:00 AM -
Postfix into prefix and vice versa
By sfe23 in forum New To JavaReplies: 9Last Post: 02-19-2009, 10:37 PM -
How do we "Loop" through subfolders to delete files?
By Zrob in forum New To JavaReplies: 3Last Post: 09-26-2008, 06:05 PM -
Help with Prefix cnt
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks