Run cmd command from java

import java.io.*;

import java.util.*;

public class CmdVerJavaDemo{

BufferedReader x=new BufferedReader(new InputStreamReader(System.in));

Runtime myrun;

Process output;

public static void main(String args[])throws IOException

{

(new CmdVerJavaDemo()).runCmdCommand("dir *.*");

}

public void runCmdCommand(String cmd)throws IOException

{

System.out.println("Your Cmd Command is "+cmd);

myrun=Runtime.getRuntime();

output=myrun.exec("cmd /c "+cmd);

String outStr="";

String fDate="";

String fName="";

x=new BufferedReader(new
InputStreamReader(output.getInputStream()));

for(int i=0;i<=100 && outStr!=null;i++)

{

outStr=x.readLine();

if(outStr==null)

{

System.out.println("File Data has null
.");

}

else

{

System.out.println(outStr);

StringTokenizer strToken=new
StringTokenizer(outStr);

int countT=strToken.countTokens();

if(i>=6)

{


fDate=strToken.nextToken();


fName=strToken.nextToken();


fName=strToken.nextToken();


fName=strToken.nextToken();


fName=strToken.nextToken();


fDate=fDate.replaceAll("/","-");


System.out.println("File Name :"+fName);


System.out.println("File Date :"+fDate);


output=myrun.exec("cmd /c md backup");


output=myrun.exec("cmd /c copy .\\*.* .\\backup");


output=myrun.exec("cmd /c md "+fDate);

File
fileName=new File(fName);


if((fileName.isDirectory()))


System.out.println("File Name :"+fName+" is Directory");

else


{


System.out.println("File Name :"+fName+" is File");


output=myrun.exec("cmd /c copy .\\"+fName+" .\\"+fDate);


}

}

}

}

}

}

No comments:

Post a Comment

If you have any question please let me know

[Software Update] Mozilla Firefox 125.0.2 Stable Released, Here is What’s New and Fixed

UPDATE: Release of Mozilla Firefox 125.0.2 stable version to public. Good news for Mozilla Firefox users! Mozilla has released Firefox 125.0...