Results 1 to 9 of 9
  1. #1
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    65
    Rep Power
    0

    Default Purge a Directory In Java

    I know that the file.delete() function in java will return a boolean response. So if a directory contains files and/or sub-dirs a false will be returned and nothing will happen. However, I am wanting to delete EVERYTHING in a directory but keep the parent directory in tact. (Reason being, we receive files from a vendor, and more times than none, there is garbage included with the actual .xlsx files that we need. We have a method in place to recursively scan for the .xlsx files and move them to a production directory, but we are currently manually purging the folder.)

    Data structure is like so (of course names are made up) --> How can I in Java recursively delete everything in this folder?
    Java Code:
    Folder structure is similar to
    C:\Garbage\123
    C:\Garbage\456
    C:\Garbage\abc
    C:\Garbage\xyz
    
    With files being similar to:
    C:\Garbage\123\image1.jpg
    C:\Garbage\456\homemovie.mov
    C:\Garbage\abc\cellphonevideo.mp4
    C:\Garbage\abc\cellphonevideo1.mp4
    C:\Garbage\xyz\randomfile.txt
    C:\Garbage\xyz\randomfile2.doc

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Purge a Directory In Java

    Check out the File class in the Java API.

    // do something like this
    public void removeDir(File[] files) {
    for all files f
    if (f is directory) {
    removeDir(f.listfiles());
    } else {
    remove f
    }
    }
    }

    This is just an example algorithm to give you the idea. It does not do any exception handling and
    certainly needs to be tweaked. First write the program so it simply walks the directory and prints
    out the name rather than delete it. And if the OS doesn't put the deleted file in the recycle bin (in Windows)
    you may want to do that manually for recovery purposes.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    65
    Rep Power
    0

    Default Re: Purge a Directory In Java

    Thank you for the reply! I am attempting to do this with SWT and 2 buttons - button 1 will be to move the .xlsx files and button 2 will be to purge the directory. Would I just copy the lines from your post into the
    Java Code:
    handleEvent
    of my class?
    Java Code:
    		button2.setText("Purge Transfer Directory?");
    		button2.addListener(SWT.Selection, new Listener() {
    			public void handleEvent(Event e) {
    				
    			}
    		});
    Sorry, I am still learning the basics. (I can add more of my syntax if that will help)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Purge a Directory In Java

    Sometimes it can take a while to delete files so it is not a good idea to delete them on an Event Dispatch Thread because that ties up the GUI. This is particularly true for Swing. Not certain about SWT but I presume it is the same. So to be safe, just kick off a new thread do do the work. But first, you should focus on simply getting the code to work outside of any GUI. You can incorporate it into the GUI later. The thread can be as simple as:

    Java Code:
    Runnable r = new Runnable() {
       public void run() {
           doSomeWork();
       }
    }
    
    new Thread(r).start();
    Of course, you also need to make certain that while you are deleting the files, other places in your GUI that would use them are
    not trying to. It doesn't have to be complicated. Just making sure you are aware.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    65
    Rep Power
    0

    Default Re: Purge a Directory In Java

    @Jim829 --> I do not have a Java Compiler on this PC, but does this reflect what you were referencing? (untested)
    Java Code:
    		JButton btnPurgeFolder = new JButton("Purge Folder");
    		File purgeDir = new File("C:\\FZDir\\");
    		btnPurgeFolder.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent arg0) {
    				TaskToPurgeDir(purgeDir);
    			}
    		});
    		frame.getContentPane().add(btnPurgeFolder, BorderLayout.EAST);
    	}
    
    	private void TaskToPurgeDir(File purgeDir) {
    		File dir = purgeDir;	
    	    if (dir.isDirectory()) {
    	        File[] files = dir.listFiles();
    	        if (files != null && files.length > 0) {
    	            for (File aFile : files) {
    	                removeDirectory(aFile);
    	            }
    	        }
    	    }
    	}
    	public static void removeDirectory(File dir) {
    	    if (dir.isDirectory()) {
    	        File[] files = dir.listFiles();
    	        if (files != null && files.length > 0) {
    	            for (File aFile : files) {
    	                removeDirectory(aFile);
    	            }
    	        }
    	        dir.delete();
    	    } else {
    	        dir.delete();
    	    }
    	}
    Last edited by jo15765; 07-21-2016 at 08:07 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Purge a Directory In Java

    A suggest for the code while testing it from the VOE.
    Replace the file.delete() statement with a print statement that says the name of the file being deleted. When the messages look good, change to doing the delete.
    Or make it conditional on a boolean:
    Java Code:
      boolean debugging = true;  // controls printing message vs deleting a file
      // ...
    
      if (debugging) {
        //  print message
      }else {
       // do the delete
      }
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    65
    Rep Power
    0

    Default Re: Purge a Directory In Java

    @Norm -
    Good thought, thank you for that idea! Will def implement and put into play when I can get to Eclipse.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Purge a Directory In Java

    Quote Originally Posted by jo15765 View Post
    @Norm -
    Good thought, thank you for that idea! Will def implement and put into play when I can get to Eclipse.
    I suggested that in post #2. It must be my accent.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    65
    Rep Power
    0

    Default Re: Purge a Directory In Java

    Quote Originally Posted by jim829 View Post
    I suggested that in post #2. It must be my accent.

    Regards,
    Jim
    Ahh, looking back at that you did.

    Thank you @jim829 and @Norm

    EDIT....
    And I just tested the above syntax, and it purges the folder exactly as I was needing!

Similar Threads

  1. Need help with java save directory
    By anuraagkb in forum New To Java
    Replies: 1
    Last Post: 01-29-2012, 04:11 PM
  2. How to access Active Directory on Java ???
    By qwerty53 in forum New To Java
    Replies: 7
    Last Post: 07-27-2011, 01:50 PM
  3. How to purge or Delete messages from ActiveMq Topic
    By gupta.asheesh22@gmail.com in forum Forum Lobby
    Replies: 0
    Last Post: 11-26-2009, 09:50 AM
  4. How to create a directory using the library java.io
    By bachtoutou in forum New To Java
    Replies: 3
    Last Post: 05-23-2008, 10:08 PM

Posting Permissions

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