How to change the Directory from command prompt using Java code -
public void verifyfiles(file dir) throws ioexception{ file[] files = dir.listfiles(); (file file : files) { //check if directory if (file.isdirectory()) { //recursively call file list function on new directory verifyfiles(file); } else { try { processbuilder builder = new processbuilder( "cmd.exe", "/c", "cd \" c:\\users\\e843778\\documents\\netbeansprojects\\encryptedfiles\" && c:\\program files\\pgp corporation\\pgp desktop"); builder.redirecterrorstream(true); process p = builder.start(); runtime rt = runtime.getruntime(); string query2 = "cmd /c pgpnetshare -v " + "\"" + file + "\""; process proc = rt.exec(query2); proc.waitfor(); system.out.println("executing for: " + file); bufferedreader in = new bufferedreader(new inputstreamreader(proc.getinputstream())); string line = ""; string s1 =""; while ((line = in.readline()) != null) { system.out.println(line); if(line.contains("all files , folders encrypted")) s1+=line; } system.out.println(s1); }
i have run pgpnetshare command files in given directory using command prompt. that, first need change current directory other directory. not able change directory below code. have checked previous answered questions in stackoverflow.com. none of them helped me.please verify code , please let me know if needs corrections. in advance!!.
that should work:
process process=runtime.getruntime().exec(query2, null, new file("directory path"));
from documentation:
process exec(string[] cmdarray, string[] envp, file dir)
executes specified command , arguments in separate process specified environment , working directory.
you can create .bat file , execute instead.
Comments
Post a Comment