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

Disable Microsoft Account Notifications in Windows 10 Settings and Start Menu

If you want to get rid of “Sign in to your Microsoft Account” and other similar account-related notifications from Settings homepage and Sta...