I need to copy a directory form one source form multiple destination. For example, i copied a file from my C: drive and paste it into many external drives like E:,F:,G:…etc. For this process i used the following code,
set src=%~1
:Loop
shift
set dest=%~1
if "%dest%"=="" goto :EOF
xcopy "%src%" "%dest%" /E
goto Loop
i saved this code snippet as .bat in system32 folder. Then i use this command as
C:\Windows\System32>mcopy C:\Users\FSSD\Desktop\Screenshot E: F:
i executed this command in my command prompt it executed successfully.
Then i tried to implement this command in my java application.
My java code is,
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MultipleCopy{
public static void main(String args[]) {
String exe_Cmd = "c:/Windows/System32/mcopy C:/Users/FSSD/Desktop/Screenshot E: F:";
Runtime r = Runtime.getRuntime();
ProcessBuilder p = new ProcessBuilder(new String[] { "cmd.exe", "/C",
exe_Cmd });
Process pro;
try {
pro = p.start();
InputStream is = pro.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("IO Exception" + e.getMessage());
}
}
}
it was executed without any errors but the files won’t copied.
My eclipse IDE’s console’s output is:
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>set src=C:/Users/FSSD/Desktop/Screenshot
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>shift
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>set dest=E:
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>if "E:" == "" goto :EOF
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>xcopy "C:/Users/FSSD/Desktop/Screenshot" "E:" /E
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>goto Loop
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>shift
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>set dest=F:
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>if "F:" == "" goto :EOF
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>xcopy "C:/Users/FSSD/Desktop/Screenshot" "F:" /E
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>goto Loop
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>shift
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>set dest=
C:\Users\FSSD\IndigoWorkSpace\Multi_Copy>if "" == "" goto :EOF
In this, what is my mistake, how can i get the exact output. Reply me as soon as possible. Thanks in advance…!
Parameters to ProcessBuilder may not contain spaces, instead of
you need
But I still would rather use a Java solution, like I suggested in the comments already 😉