File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

dos commands within java program???

jake mullet
Greenhorn

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"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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);


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

Joined: Feb 26, 2001
Posts: 4462
    
    6

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


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.*;
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dos commands within java program???