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

Windows 11 Dev Insider Preview Build 26120.3291 Released, Here is What’s New and Fixed

UPDATE: Windows 11 Insider Preview build 26120.3291 (KB5052080) released to Dev channel. Today Microsoft released a new Insider Preview buil...