0

I want to delete a directory/file programmatically in java using org.apache.commons.exec.CommandLine.

The directory that I am trying to delete has a space character and because of that it seems I am unable to delete that.

import org.apache.commons.exec.CommandLine

try {
   CommandLine command = CommandLine.parse("rm -rf ");
   File targetDir = new File(getFinalSourceDirectory());

   command.addArgument(dlpath);  // dlpath = "tmp/dir to rmv/OSAAT - KT"

} catch (Exception ex) {
        java.util.logging.Logger.getLogger(SubversionConnector.class.getName()).log(Level.SEVERE, null, ex);
    }

Though the exit status is returning 0, it is not deleting the directory "OSSAT - KT"

I tried to escape the space by \\\  or %20, etc. but nothing worked for me.

Gabriel
  • 734
  • 11
  • 26
Shibankar
  • 806
  • 3
  • 16
  • 40
  • Have you tried also to escape the '-' ? – V15I0N May 20 '15 at 21:03
  • @V15I0N : Do i have to escape '-' also ? if yes, what is the correct way to escape both the '-' and Space ? – Shibankar May 20 '15 at 21:05
  • I'm not sure but I would try this as '-' is probably misinterpreted as option. – V15I0N May 20 '15 at 21:17
  • tried rm -rf -- "tmp/file to del/OSSAT - KT". Still not working..! – Shibankar May 20 '15 at 21:38
  • What is `CommandLineExec`? I would have just done `command.addArgument(targetDir)` and then `new DefaultExecutor().execute(command)` (or cleaner code to that effect), as `CommandLine` is supposed to deal with the quoting of the parameter appropriately. – Anya Shenanigans May 20 '15 at 21:44
  • 3
    Wouldn't `File.delete()` be a better way to remove a file programmatically? Or if it needs to be recursive, commons-io lets you do `FileUtils.deleteDirectory(new File("My Folder"));` – David Ehrmann May 20 '15 at 22:07
  • You could use the Apache commons `FileUtilis.deleteDirectory()` as mentioned in [another answer](http://stackoverflow.com/a/779555/17300) ... I don't know `CommandLine`, but if it actually invokes a shell could you add single quotes around the dlpath value? e.g. `dlpath = "'" + dlpath + "'";`? – Stephen P May 20 '15 at 23:42
  • @StephenP : i tried with dlpath = "'" + dlpath + "'"; but its not working. – Shibankar May 21 '15 at 07:39
  • Can you try with an absolute path (starting with a /) ? – Walter A May 21 '15 at 08:46
  • @DavidEhrmann: Actually the requirement is to delete both file and directory recursively. File.delete(); and FileUtilis.deleteDirectory(); works fine as it is doesn't bother with spaces. Thank you for your suggestion. Cheers ...!!! – Shibankar May 21 '15 at 09:56
  • I'm not sure why you'd want to use a third-party library to do something that a simple [ProcessBuilder](https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html) can do. Regardless, you shouldn't use a system command, and you shouldn't use the obsolete File class. Instead, use [Files.walkFileTree](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#walkFileTree-java.nio.file.Path-java.nio.file.FileVisitor-); there is even a complete example in the [documentation for the FileVisitor class](https://docs.oracle.com/javase/8/docs/api/java/nio/file/FileVisitor.html). – VGR May 22 '15 at 02:38
  • Just to your code: you call 'addArgument' to 'command' but where do you execute the command? Perhaps "rm -rf " is also a problem, because the executable is probably named "rm" and "-rf" is the first command line argument to it? – V15I0N May 25 '15 at 20:07

0 Answers0