• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

dos commands within java program???

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Sheriff
Posts: 16068
267
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Maybe he went home and went to bed. And took this tiny ad with him:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic