Reputation: 5
In my project, i need to segregate all .java files in a folder and paste it in a separate folder. I figured out that the below mentioned command works for this purpose
for /f "delims==" %k in ('dir C:\Project\downloads\*.java /s /b') do copy "%k" C:\Project\javaRepo
In the above command, the Source Folder : C:\Project\downloads\
Destination folder to copy all .java files : C:\Project\javaRepo
I tried using the following command in JAVA,
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(.....);
But I am not clear how to put the "for" command as an argument for rt.exec.
I tried creating a batch file as follows, but it doesn't work.
@echo off
for /f "delims==" %k in ('dir C:\Project\downloads\*.java /s /b') do copy "%k" C:\Project\javaRepo
Can you let me know if my approach is correct, or is there any other better alternative? Suggestions / ideas are most welcome.
Upvotes: 0
Views: 1517
Reputation: 5
Thank you guys for new suggestions.. I looked into them as well. However, I able to implement the same commandline code later on... My code is as follows
public class copyJava {
public static void main(String args[]) throws IOException,
InterruptedException {
Process p = null;
String[] command = {
"cmd",
"/c",
"for /f \"delims==\" %k in ('dir C:\\Project\\workspace\\downloads\\*.java /s /b') do copy \"%k\" C:\\Project\\workspace\\javaRepo" };
ProcessBuilder copyFiles = new ProcessBuilder(command);
copyFiles.redirectErrorStream(true);
p = copyFiles.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
do {
line = reader.readLine();
if (line != null) {
System.out.println(line);
}
} while (line != null);
reader.close();
p.waitFor();
}
}
It works good now.. :)
Upvotes: 0
Reputation: 595
Imagine a Java "clippy" the helpful paperclip:
It looks like you're trying to organise a java project!
Perhaps you should be using a build tool like ant or gradle. These can do those low level tasks in a very compact and convenient way.
If you must do it from Java, you can even use ant, say, as a library which can do these sorts of operations for you.
Or if you're willing to reorganise your project and follow the conventions maven may also be a more automatic solution.
Upvotes: 1
Reputation: 24732
If you are in Java you don't need all this trickery. Just use for loop and copy files. You can call copy via exec for each file or you can copy using pure Java which would be lot better imho.
Upvotes: 1