Connecting Tech Pros Worldwide Forums | Help | Site Map

"copyDirectory" multiple files-folder into new one fails

Newbie
 
Join Date: Jul 2008
Posts: 4
#1: Jul 22 '08
Hi!
I want to write an application on windows xp, which iterates n times a model script (exe) and copies every single run the results (multiple files such as text, maps, subfolders,etc) from the existing folder into a new one. The code works, but no files nor folders are copied. This is how far as I got being an absolute beginner in Java. Thanks for any help,
CB

[i]import java.io.*;

public class PCRmain {

public static void main(String[] args) throws InterruptedException
{ /* specify number of model iterations... */

try {
File sim = new File("D:/eclipse/Hydro/sim");
File source = new File ("D:/eclipse/Hydro/PCRaster");

for (int j = 1; j < Integer.parseInt(nb)+1; j++)
{ /* call PCRaster and specify model file .mod and path */

String command = "C:/PCRaster/apps/pcrcalc -f";
String arg = "D:/PCRaster/workspace/OPTI_test/OPTItest.mod";
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command + " " + arg);
process.waitFor();


/* Copies results for every single run into "sim" folders */

if (source.isDirectory())
{ if (!sim.exists())
{/* create j result folders called "sim" */
String folder_name = "sim_" + j;
String directory = "D:/eclipse/Hydro/" + folder_name;
File dir = new File(directory);
dir.mkdir();

String files[] = source.list();
for(int i = 0; i < files.length; i++)
{ copyDirectory(new File(source, files[i]), new File(sim, files));}}
else
{ if(source.exists())
{System.out.println("File or directory does not exist.");
System.exit(0);}
else {
InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(sim);

/* Transfer bytes from in to out*/
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);}
in.close();
out.close();}
}}}}

catch(IOException e)
{ javax.swing.JOptionPane.showConfirmDialog(null,
"The .exe can't be found.","Verify path.",
javax.swing.JOptionPane.PLAIN_MESSAGE);
return;
}}

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Jul 22 '08

re: "copyDirectory" multiple files-folder into new one fails


The File.renameTo method should be useful here.
Newbie
 
Join Date: Jul 2008
Posts: 4
#3: Jul 22 '08

re: "copyDirectory" multiple files-folder into new one fails


Quote:

Originally Posted by r035198x

The File.renameTo method should be useful here.

Thanks for the quick reply!
I tried the following in my iteration loop, which I suppose is not correctly implemented. Could you kindly give me another hint?

/* Copies results for every single run into "sim" folders */
{ if (!sim.exists())
{/* create j result folders called "sim" */
String folder_name = "sim_" + j;
String directory = "D:/eclipse/Hydro/" + folder_name;
File dir = new File(directory);

dir.mkdir();
renameTo (source, dir) ;
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Jul 22 '08

re: "copyDirectory" multiple files-folder into new one fails


Quote:

Originally Posted by chrisbirk

Thanks for the quick reply!
I tried the following in my iteration loop, which I suppose is not correctly implemented. Could you kindly give me another hint?

/* Copies results for every single run into "sim" folders */
{ if (!sim.exists())
{/* create j result folders called "sim" */
String folder_name = "sim_" + j;
String directory = "D:/eclipse/Hydro/" + folder_name;
File dir = new File(directory);

dir.mkdir();
renameTo (source, dir) ;

That obviously doesn't compile right?
Expand|Select|Wrap|Line Numbers
  1. File c = new File("/root/Desktop/stocks");
  2.  c.renameTo( new File("/root/Desktop/temp/stocks"));
Will copy the folder stocks and all it's contents from the desktop to the temp folder on the desktop.
Newbie
 
Join Date: Jul 2008
Posts: 4
#5: Jul 24 '08

re: "copyDirectory" multiple files-folder into new one fails


Hi!
thanks again for your help. I can now rename folders and its content from another directory into a new one, but renaming the working folder ("PCRaster" - where the model writes the results in) to the newly created sim_n folder for every iteration step is still not working. As I don't get any error messages when compiling and setting the code up and as I'm new to Java still not knowing about limitations or exceptions I'd like to ask for another hint.
Cheers,
CB

File source = new File ("D:/Eclipse/Hydro/PCRaster");
source.renameTo (new File ("D:/Eclipse/Hydro/sim~"));
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#6: Jul 24 '08

re: "copyDirectory" multiple files-folder into new one fails


Why don't you post the code you have now and explain what you want it to do.

P.S The icon for code tags when posting code is the one with the # symbol.
Newbie
 
Join Date: Jul 2008
Posts: 4
#7: Jul 25 '08

re: "copyDirectory" multiple files-folder into new one fails


Hi,
here's the code with the problem copying the results for every iteration into a newly created "sim" folder.
Cheers,
CB

Expand|Select|Wrap|Line Numbers
  1. import java.io.*; 
  2. public class PCRmain {
  3.     public static void main(String[] args) throws InterruptedException
  4.     {
  5.         /* specify number of model iterations */
  6.  
  7.         String nb = "0";
  8.         System.out.println("type number of iterations :");
  9.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         try { nb = br.readLine();
  12.                System.out.println("nb = " + nb);}
  13.         catch ( IOException error ) {
  14.             System.err.println("no number");
  15.             return;
  16.         } 
  17.         try
  18.         {            
  19.             for (int j = 1; j < Integer.parseInt(nb)+1; j++)
  20.             {            
  21.             /* call model and specify model file .mod and path */
  22.  
  23.             String command = "C:/PCRaster/apps/pcrcalc -f";
  24.             String arg ="D:/PCRaster/workspace/OPTI_test/OPTItest.mod";
  25.             Runtime runtime = Runtime.getRuntime();
  26.             Process process = runtime.exec(command + " " + arg);
  27.             process.waitFor();
  28.  
  29.             /* create j result folders called "sim" */
  30.  
  31.             String folder_name = "sim_" + j;
  32.             String directory = "D:/Eclipse/Hydro/" + folder_name;
  33.             File dir = new File(directory);
  34.             dir.mkdir();
  35.  
  36.             /* Copy results for j runs into "sim" folders */
  37.  
  38.             File source = new File ("D:/Eclipse/Hydro/PCRaster");
  39.             source.renameTo (new File ("D:/Eclipse/Hydro/sim~"))    ;                    }}
  40.  
  41.         catch(IOException e)
  42.         { javax.swing.JOptionPane.showConfirmDialog(null,
  43.         "The .exe can't be found.","Verify path.",
  44.         javax.swing.JOptionPane.PLAIN_MESSAGE);
  45.         return;
  46.         }}    
  47.     } 
Reply