Results 1 to 3 of 3
  1. #1
    Krooger is offline Member
    Join Date
    Nov 2008
    Posts
    43
    Rep Power
    0

    Question 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:
    Java Code:
    new File("output.txt").delete();
    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.

    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.

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    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

  3. #3
    Krooger is offline Member
    Join Date
    Nov 2008
    Posts
    43
    Rep Power
    0

    Default

    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

  1. Infix to Prefix
    By Sasarai in forum Advanced Java
    Replies: 4
    Last Post: 12-08-2010, 03:57 PM
  2. Replies: 2
    Last Post: 04-20-2009, 08:00 AM
  3. Postfix into prefix and vice versa
    By sfe23 in forum New To Java
    Replies: 9
    Last Post: 02-19-2009, 10:37 PM
  4. Replies: 3
    Last Post: 09-26-2008, 06:05 PM
  5. Help with Prefix cnt
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •