The moose likes Beginning Java and the fly likes dos commands within java program??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "dos commands within java program???" Watch "dos commands within java program???" New topic

dos commands within java program???

jake mullet

Joined: May 09, 2001
Posts: 15
Im using a WindowsNT system and I want my java program to to go into a diretory where three files exist. File1.txt, File2.txt, and Newbie.txt. I want to issue an command that deleted File1 and File2 and then renames Newbie.txt to File1.txt. Is this possible? I was looking up Runtime but its a little confusing
public Process exec(String del File1.txt);
public Process exec(String del File2.txt);
public Process exec(String ren Newbie.txt File1.txt);
Kinda confused. any help would be great. Thanks JM
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Runtime.getRuntime().exec("cmd /K del File1.txt");
(the older version of windows use the word "command")
You can also do:
Runtime.getRuntime().exec("cmd /K start run.bat");

"JavaRanch, where the deer and the Certified play" - David O'Meara
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

You could also do this in a platform-independent way with

Junilu - [How to Ask Questions] [How to Answer Questions]
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
I am not sure this is what you are looking for, anyway here is an example that how to delete a file from a local directory. I modified it a little bit from my original version. I suggest you to check if the file already exist before to do the deletion, and you might like to delete the bat file after execution too. This is the only way I know how to delete a file.
I hope someone knows a better way to that and share with us.
Let me know if this helps?
import java.util.*;
public class DeleteFile
public DeleteFile(File fullFileNameAndDir) throws IOException
PrintWriter reportDeleteFileWriter = new PrintWriter(new FileWriter
(fullFileNameAndDir.getParent() + ".bat" , true));
//create dos command to delete the file from local dir
reportDeleteFileWriter.println( "del " + fullFileNameAndDir);
System.out.println("del " + fullFileNameAndDir );
catch(IOException ioreportDelPrintBatE)
System.out.println("IO Exception in report delete bat ." );
System.out.println(" reportPrintBat IO: " + ioreportDelPrintBatE.toString());
Runtime d = Runtime.getRuntime();
d.exec( fullFileNameAndDir.getParent() + ".bat" );
System.out.println("Successfully execute file");
public static void main(String[] args) throws IOException
File strReportDir = new File("C:\\temp\\Mindy\\test.txt");
new DeleteFile(strReportDir);
catch(IOException eMain)
} //end of class

[This message has been edited by Mindy Wu (edited June 22, 2001).]
I agree. Here's the link:
subject: dos commands within java program???
It's not a secret anymore!