473,722 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"copyDirect ory" multiple files-folder into new one fails

4 New Member
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 InterruptedExce ption
{ /* 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.parseIn t(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.getRunt ime();
Process process = runtime.exec(co mmand + " " + arg);
process.waitFor ();


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

if (source.isDirec tory())
{ 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(n ew File(source, files[i]), new File(sim, files));}}
else
{ if(source.exist s())
{System.out.pri ntln("File or directory does not exist.");
System.exit(0); }
else {
InputStream in = new FileInputStream (source);
OutputStream out = new FileOutputStrea m(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(IOExcepti on e)
{ javax.swing.JOp tionPane.showCo nfirmDialog(nul l,
"The .exe can't be found.","Verify path.",
javax.swing.JOp tionPane.PLAIN_ MESSAGE);
return;
}}
Jul 22 '08 #1
6 2408
r035198x
13,262 MVP
The File.renameTo method should be useful here.
Jul 22 '08 #2
chrisbirk
4 New Member
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) ;
Jul 22 '08 #3
r035198x
13,262 MVP
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.
Jul 22 '08 #4
chrisbirk
4 New Member
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~"));
Jul 24 '08 #5
r035198x
13,262 MVP
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.
Jul 24 '08 #6
chrisbirk
4 New Member
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.     } 
Jul 25 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2274
by: =?Utf-8?B?bWFnZWxsYW4=?= | last post by:
Hi, I have a very simple routine that i Call this function ~ 50 times in a loop to copy a directory from a src to a destination. On the 2nd call, I get this msg: "...BacgroundWorker is currently busy..." What are my option to resolve this issue...? thx in advance
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.