#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    43
    Rep Power
    7

    Copying multiple files from one folder to another


    Guys i have been working with Java and each day it's a new error or a new challenge.

    What I'm trying to here is to move files from a specific "C:\\MSGRTS\\IN\\" folder to another folder "C:\\MSGRTS\\OUT\\"

    Firstly i have tested moving a single file from one folder to another and it worked pretty well. Here is the code:

    import java.io.File;

    public class Test {

    public static void main(String[] args) {
    /** Source file to move **/
    File f1 = new File("c:\\MSGRTS\\IN\\RTSE200600040324030MZM.txt");

    /** New location **/
    f1.renameTo(new File("c:\\MSGRTS\\OUT\\RTSE200600040324030MZM.txt"));

    }
    }

    Now what i want to do is to move multiple files (more than one file) to a specific folder and i don't know how.

    NB: Both folders(IN and OUT) are located in the same machine.

    Can somebody help me out please?

    Regards,
    hellboy83
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    339
    Rep Power
    476
    Do you have a list of files or are you trying to move the entire contents of a folder?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    43
    Rep Power
    7
    myscruffy I'm trying to move the entire contents of a folder. From "C:\\MSGRTS\\IN\\" to "C:\\MSGRTS\\OUT\\"

    Regards,
    hellboy83
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2007
    Posts
    1,940
    Rep Power
    3117
    Originally Posted by hellboy83
    myscruffy I'm trying to move the entire contents of a folder. From "C:\\MSGRTS\\IN\\" to "C:\\MSGRTS\\OUT\\"

    Regards,
    hellboy83
    Simple get a list of the files in the folder, loop through each and move them one at a time like you have done in your example.

    To get a list of files in a directory there is method on the File object named listFiles().
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    43
    Rep Power
    7
    Hi,

    jzd i'm able to get a list of files in my directory through the listFiles() method, here is the code for that:

    import java.io.*;

    public class Test {

    public static void main(String[] args) {

    File dir = new File("c:\\MSGRTS\\IN\\");
    File[] files = dir.listFiles();

    for (int i=0; i < files.length; i++){
    System.out.println(files[i].getName());

    }
    }
    }

    Now the idea is to use this same loop to move files from the source folder "c:\\MSGRTS\\IN\\" to the destination folder: "c:\\MSGRTS\\OUT\\". Here's what i have tried to do but it doesn't work at all guys.

    import java.io.*;

    public class Test {

    public static void main(String[] args) {

    File dir = new File("c:\\MSGRTS\\IN\\");
    File[] files = dir.listFiles();

    for (int i=0; i < files.length; i++){
    System.out.println(files[i].getName());
    files[i].renameTo(new File("c:\\MSGRTS\\OUT\\"));
    }
    }
    }

    But it doesn't work. I know i'm missing something here. But what is it?

    Regards,
    hellboy83
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    339
    Rep Power
    476
    The renameTo() method renames the file that which you specify. It looks like you are trying to move a file to a directory.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    43
    Rep Power
    7
    Ok myscruffy. So you're saying that i can't use this method to move/copy multiple files from one folder to another?

    Since i was able to do it with a single file i thought i could do it with multiple files through an iteration loop(in the code below) which doesn't work:

    import java.io.File;

    public class Test {

    public static void main(String[] args) {

    File source = new File("c:\\MSGRTS\\IN\\");
    File dest = new File("c:\\MSGRTS\\OUT\\");
    File[] files = source.listFiles();

    for (int i=0; i < files.length; i++){
    System.out.println(files[i].getName());
    files[i].renameTo(dest);

    }
    }
    }

    If it's really impossible through this method, can you please advice me with the most suitable one.

    Regards,
    hellboy83
  14. #8
  15. No Profile Picture
    rebel with a cause
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2004
    Location
    The Batsh!t Crazy State.
    Posts
    5,817
    Rep Power
    3462
    Your problem is that renameTo(String) is for the path and the filename, not just the path. So instead of
    Code:
    files[i].renameTo(dest);
    try
    Code:
    file[i].renameTo(dest + file[i].getName());

    Comments on this post

    • Yawmark agrees
    Dear God. What is it like in your funny little brains? It must be so boring.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    43
    Rep Power
    7
    Thanks crownjewel82

    I didn't realize that, now the program works just fine. Here is the source code:

    import java.io.File;

    public class Test_2 {

    public static void main(String[] args) {

    File source = new File("c:\\MSGRTS\\IN\\");
    File dest = new File("c:\\MSGRTS\\OUT\\");

    File[] files = source.listFiles();

    for (int i=0; i < files.length; i++){

    System.out.println(source+"\\"+files[i].getName());

    String x=(source+"\\"+files[i].getName());
    String y=(dest + "\\"+ files[i].getName());

    File f1 = new File(x);
    f1.renameTo(new File(y));
    }
    }
    }

    Now instead of running(java Test_2.java) manually whenever somebody drops a file in the source folder I'd like to make it automatic, for example: this java program autoruns itself for 30 seconds and it sleeps for the coming 30 seconds and so on.

    Is that possible to do in Java?

    Regards,
    hellboy83
  18. #10
  19. No Profile Picture
    rebel with a cause
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2004
    Location
    The Batsh!t Crazy State.
    Posts
    5,817
    Rep Power
    3462
    Well in java you could have it run then wait 30 seconds and then run again using principles from threading. But I'd consider attaching it to a scheduled task in the operating system. That way if the machine is rebooted you don't have to remember to start the task again.

    But either way make sure you consider the resources this app is using.
    Dear God. What is it like in your funny little brains? It must be so boring.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2004
    Location
    Washington DC
    Posts
    2,756
    Rep Power
    1571
    I agree with CJ regarding the schedule task for the OS...

    If you want to use java though, Check out Quartz instead of rolling your own scheduler.
    Open for extension, closed for modification
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    43
    Rep Power
    7
    Hi Guys,

    I've managed to do it this way and it works fine! The program runs in a cycle of 10, each cycle takes 3 seconds.

    The files it finds in the IN folder at first it moves them to the OUT folder with no problem but if i put a new set of files different from the first set it doesn't move them but the funny thing is that if i place the ones that were there first it moves to the OUT with no problems.

    How can i solve this problem guys?

    I want the code to move to the OUT folder whatever files are dropped in the IN folder.

    import java.awt.Toolkit;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.io.File;


    public class AnnoyingBeep {

    File source = new File("c:\\MSGRTS\\IN\\");
    File dest = new File("c:\\MSGRTS\\OUT\\");

    File[] files = source.listFiles();

    Toolkit toolkit;
    Timer timer;

    public AnnoyingBeep() {
    toolkit = Toolkit.getDefaultToolkit();
    timer = new Timer();
    timer.schedule(new RemindTask(), 0, //initial delay
    3 * 1000); //subsequent rate
    }

    class RemindTask extends TimerTask {
    int numWarningBeeps = 1;

    public void run() {
    if (numWarningBeeps > 0) {
    toolkit.beep();
    System.out.println("Beep!");
    for (int i=0; i < files.length; i++){

    System.out.println(source+"\\"+files[i].getName());

    String x=(source+"\\"+files[i].getName());
    String y=(dest + "\\"+ files[i].getName());

    File f1 = new File(x);
    f1.renameTo(new File(y));

    }
    numWarningBeeps++;
    } else {
    toolkit.beep();
    System.out.println("Time's up!");
    //timer.cancel(); //Not necessary because we call System.exit
    System.exit(0); //Stops the AWT thread (and everything else)
    }
    }
    }

    public static void main(String args[]) {
    System.out.println("About to schedule task.");
    new AnnoyingBeep();
    System.out.println("Task scheduled.");
    }
    }
  24. #13
  25. No Profile Picture
    rebel with a cause
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2004
    Location
    The Batsh!t Crazy State.
    Posts
    5,817
    Rep Power
    3462
    Look at where you're calling listFiles. If you only call it once then it will only ever look for one set of files.

    Comments on this post

    • stdunbar agrees
    Dear God. What is it like in your funny little brains? It must be so boring.
  26. #14
  27. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2007
    Posts
    1,940
    Rep Power
    3117
    Look at what crownjewel82 said, but and additional little nitpick:

    Instead of:

    for (int i=0; i < files.length; i++){

    Use a for each loop

    for(File file: source.listFiles()){

    Disclaimer, unless you are running a version of java before 1.5
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    43
    Rep Power
    7
    Thanks crownjewel82, jzd and all the other guys who helped me out.

    That's right i was calling listFiles method only once and with that huge but not little nitpick from jzd i got it all working.

    this is where the changes were made to get the program to work as i needed:

    ...
    for (File file: source.listFiles()){

    System.out.println(source+"\\"+file.getName());

    String x=(source+"\\"+file.getName());
    String y=(dest + "\\"+ file.getName());

    File f1 = new File(x);
    f1.renameTo(new File(y));

    }
    ...

    Regards,
    hellboy83

IMN logo majestic logo threadwatch logo seochat tools logo