tags:

views:

275

answers:

6

How do I execute

rd /s /q c:\folder

in Java?

It woks perfectly on the command-line.

+5  A: 

Check the Runtime.exec method, which lets you call external processes. (Bear in mind that you lose some platform independence as this would rely on the machine having the rd command installed and on the path.)

A better option might be to do the same thing in pure Java - the following should be equivalent:

private void deleteDirectory(File directory)
{
   for (File entity : directory.listFiles())
   {
      if (entity.isDirectory())
      {
         deleteDirectory(entity);
      }
      else
      {
         entity.delete();
      }
   }
   directory.delete();
}

deleteDirectory(new File("C:\\folder"));

Adding error-checking as required. :-)

Andrzej Doyle
rd is working fine on the command prompt but not through Runtime.exec() function.
Abdul Khaliq
that's because 'rd' is a built-in command in 'cmd.exe'
Alnitak
ok, but how do i execute it in java any ideas??
Abdul Khaliq
implement it, like i described.
Markus Lausberg
no, do it like I described, it's shorter ;-)
Alnitak
the recursion is cleaner in mine too - only one call to .delete() and no tests in the for() loop :)
Alnitak
+2  A: 

Look at File.delete(String path) method, i.e.:

new File("c:\\folder").delete();

If the /s (recursive) deletion is important, then (untested):

public void deltree(File file) {
    if (file.isDirectory()) {
        for (File f : file.listFiles()) {
            deltree(f);
        }
    }
    file.delete();
}

public void deltree(String path) {
    deltree(new File(path));
}

invoked as:

deltree("c:\\folder");
Alnitak
this does not delete folder which contains files or/and folders.
Abdul Khaliq
overlapping edits - please remove comment.
Alnitak
File.delete() returns a boolean flag indicating whether the delete was successful rather than throwing an IOException; should you be checking this?
Simon Nickerson
well, I _did_ say it was untested ;-)
Alnitak
Alnitak's solution worked for me. (I am checking the result of File.delete() per simonn's comment.)
Nate
+1  A: 

You only can delet empty directories in Java. First you have to delete the files and sub directories.

 public static boolean removeDirectoryRecursively(File dir) {
            if (dir.isDirectory()) {
                String[] children = dir.list();
                for (int i = 0; i < children.length; i++) {
                    boolean success = removeDirectoryRecursively(new File(dir, children[i]));
                    if (!success) {
                        return false;
                    }
                }
            }
            // The directory is now empty so delete it
            return dir.delete();
        }
Markus Lausberg
File.listFiles() is far simpler
Alnitak
+3  A: 

Check out FileUtils in Apache Commons-IO; in particular, the deleteDirectory and deleteQuietly methods, which can both recursively delete a directory.

Simon Nickerson
+1  A: 

If you want to execute it exactly the way you specified, you can do

Process p=Runtime.getRuntime().exec("cmd.exe /k rd /s /q c:\\folder");
Joset
After all "it woks perfectly on command prompt." :)
Harry Lime
this does not work
Abdul Khaliq
+6  A: 

According to dtsazza's answer:

Runtime.getRuntime().exec("cmd.exe /k rd /s /q c:\\folder");

It works perfectly under WinXP SP3. The /k parameter shows, that a command will follow that has to be executed out of the cmd.exe.

Good luck with that!

furtelwart
exactly what i was looking for!
Abdul Khaliq
Although of course you need to double up the backslash (c:\\folder)
Simon Nickerson
Thanks, simonn!
furtelwart