3

i'm trying in a loop to move files after they are loaded and processed...when I test moving file part individually it works but when I do it all at once it does not work.

Bellow works fine, but moves directories also but I want only the file to be moved.

public class moveFiles {

    public static void main(String[] args) {
        String getFilesFrom = "D:\\show\\from";
        String destDir = "D:\\show\\to\\";

        File srcFile = new File(getFilesFrom);

        srcFile.renameTo(new File(destDir, srcFile.getName())); 

    }

}

The code that I have which is not working the moving part is bellow.

for (File child : file.listFiles()) {
    if(extensionFilter.accept(child)) { 
        fr = new FileReader(child);
        cm.copyIn("COPY ct"+addExtraZero+month+" FROM STDIN WITH DELIMITER ',' csv", fr);   
    } else {
        System.out.println("No File is elgible to be loaded");
        break;
    }
    getNumberOfFilesProcessed++;
    System.out.println("Loading now " + child.getName());
    child.renameTo(new File(moveFilesTo, child.getName()));
}
System.out.println("Number of files Loaded is: " + getNumberOfFilesProcessed);

The above code is:

  • get files from source directory,
  • loaded it in database
  • print files name that it loads
  • get count of files loaded

which all above works but the last part which is to move files to other directory after loading is not working bellow is the section of files that suppose to move the file the loop.

child.renameTo(new File(moveFilesTo, child.getName()));

scratching my heads for two hours any help will be appreciated.

Alin Pandichi
  • 955
  • 5
  • 15
hi4ppl
  • 595
  • 1
  • 6
  • 21
  • Why do you have a `break;` in there? Is it possible you wanted a `continue;`? – daiscog Aug 09 '16 at 08:23
  • Hello, because I have extensionFilter to check if files don't match to the filter it should break the code... – hi4ppl Aug 09 '16 at 08:25
  • So you want it to break out of the loop as soon as a single file which doesn't match the filter is found? Currently, if the first file in the list doesn't match the filter, the whole loop is stopped and no other files are processed. – daiscog Aug 09 '16 at 08:26
  • Hello, yes I got your point that part I was testing and it does exactly what you say it does... but the moving part is not working completely – hi4ppl Aug 09 '16 at 08:31
  • So your program outputs "Loading now [filename]" but does not move that file? – daiscog Aug 09 '16 at 08:35
  • Yes, everything works but not the moving files to another path I have given... the functionality of that part I check in different class it works but not this part... – hi4ppl Aug 09 '16 at 09:36
  • As an aside, at the moment your code will quit half way if one of the files doesn't copy over. It might be better to check all the files and then move all the files, to avoid half-completing the job. – Drgabble Aug 09 '16 at 09:54
  • Hello, yes good idea but how can that be done should I put it outside of loop? – hi4ppl Aug 09 '16 at 09:59
  • Is `moveFilesTo` an absolute or relative path? If it's relative, it may not be pointing exactly where you'd expect. – daiscog Aug 09 '16 at 11:50
  • i'm using variable to store paths bellow is exactly how it's : String getFilesFrom = "D:\\ct\\"; String moveFilesTo = "D:\\ctl2\\"; – hi4ppl Aug 09 '16 at 11:54

1 Answers1

1

From description of File.renameTo() (emphasis mine):

The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful

Add:

if( !child.renameTo(new File(moveFilesTo, child.getName())) )
    System.out.println("Could not move file");

Or try using move(Path, Path, CopyOption...) method, as this has more options (using File.toPath()).

daiscog
  • 11,441
  • 6
  • 50
  • 62
Drgabble
  • 618
  • 5
  • 17
  • i'm getting this message now(Could not move file) ... but how come this works on the first sample of code I showed... it's the same filesystem only directory is different...the only different is the loop.. – hi4ppl Aug 09 '16 at 09:58
  • file already exists? – Drgabble Aug 09 '16 at 09:59
  • no the destination is completely empty... – hi4ppl Aug 09 '16 at 10:00
  • I don't want to use different approach as I have to re-write the program from scratch...as the approach I showed above works as single class but when using with this class in the loop it does not work... – hi4ppl Aug 10 '16 at 03:16
  • I don't think it requires rewriting from scratch, just replace the line with `move(child.toPath(), new File(moveFilesTo, child.getName()).toPath());` – Drgabble Aug 10 '16 at 08:52