2

I would like to perform the following windows command under java :

rmdir /s C:\Main\Second\Third

Y

The problem is that there is 2 commands to run, one for the remove and one as confirmation (a simple "Y", as you can see).

I know it's feasable but I didn't manage to make it.

        String rmCommand = "cmd.exe /c rmdir /s /q C:\\Main\\Second\\Third";
        
        Runtime runt = Runtime.getRuntime();
        Process process = runt.exec(rmCommand);

So my question is : How to run these 2 above commands in prompt thanks to JAVA code.

EDIT : I modified a bit the initial code according to some proposals but it still doesn't work ... I don't know what to do.

Community
  • 1
  • 1

3 Answers3

3

rmdir isn't a program - it's part of the command shell. So you need to run something like:

"cmd.exe /c rmdir /s C:\\Main\\Second\\Third"

(For a production version, you'd want to make sure you got the right cmd.exe, of course.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I see, thank you for your fast answer. But question : What if the cmd.exe ask for a confirmation ? Here I need to put "Y" in cmd to confirm the delete. – user1618814 Aug 23 '12 at 06:23
  • @user1618814 It is better to add this details in your question to make it more clearly – Alex K Aug 23 '12 at 06:25
  • @ Alex k : As written : "So my question is : How to run these 2 above commands in prompt thanks to JAVA code."I also edited my post, sorry if it wasn't clear enough – user1618814 Aug 23 '12 at 06:26
  • 1
    You can use /q to not make it ask, I think. – Thilo Aug 23 '12 at 06:37
  • I don't see where could be my mistake. A file F1 = new File("BLABLA") has not to be closed, isn't it ? – user1618814 Aug 23 '12 at 06:53
  • Does not have to be your program that locked the file, can be anything on your system. – Thilo Aug 23 '12 at 12:06
  • @user1618814: You talk about two "commands" - but Y isn't a command. And as Thilo said, you should be able to use `/q` to avoid the prompt. If you can't delete the file, that's a *completely* different problem. – Jon Skeet Aug 23 '12 at 12:07
1

To avoid being asked for confirmation, add /Q to your rmdir command: http://www.computerhope.com/rmdirhlp.htm

But it is a very bad practice to do it with rmdir. If you don't want to do the recusrsive deletion stuff yourself, use Apache Commons FileUtils. And it's not the question of rmdir being enough or not, it's the question of using the tool you have the right way. If I understand you correctly, the problem you have is that java cannot delete open files, but rmdir does. Seems more like a problem with your code leaving files open, isn't it?

Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
1

Putting aside the question about whether its good practice or not, the answer to your question is as follows: create a file containing the single character Y. Let's say its called y.txt.

Then your command would be:

cmd.exe /c rmdir /s C:\\Main\\Second\\Third < y.txt
Wouter J
  • 41,455
  • 15
  • 107
  • 112
Ian
  • 11
  • 1