This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dos commands within java program???

 
jake mullet
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
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");
and
Runtime.getRuntime().exec(notepad.exe);
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also do this in a platform-independent way with java.io.File.
See http://java.sun.com/j2se/1.3/docs/api/java/io/File.html
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.*;
import java.io.*;
public class DeleteFile
{
public DeleteFile(File fullFileNameAndDir) throws IOException
{
try
{
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 );
reportDeleteFileWriter.close();
}
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");
} //END OF MOVING FILES
public static void main(String[] args) throws IOException
{
File strReportDir = new File("C:\\temp\\Mindy\\test.txt");
try
{
new DeleteFile(strReportDir);
}
catch(IOException eMain)
{
System.out.println(eMain.toString());
}
System.exit(0);
}
} //end of class


[This message has been edited by Mindy Wu (edited June 22, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic