0

The goal of the program is to organize the images based on the date they were taken on. I got everything to work but moving is giving me a lot of issues. when I move it, it always works for the firs 2 image files and then starts giving the error message: "The process cannot access the file because it is being used by another process." on the rest of them and I have tried a lot of different ways of moving it and it always moves the first two and doesn't work for the rest. What am I doing wrong?

Here is the code I use for the mover:

// Moves each image file to its respective folder based on the date of creation
private static void moveImages(String mainPath, String[] fileDates, String[] fileNames) 
{
    // Loops through moving each file in the array that contains the dates of each file
    for(int i = 0; i < fileDates.length; i++)
    {
        String fromFile = mainPath + "\\" + fileNames[i];
        String toFile = mainPath + "\\" + directoryName(fileDates[i]) + "\\" + fileNames[i];
        
        Path source = Paths.get(fromFile);
        Path target = Paths.get(toFile);
        
        try
        {       
            Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);

            System.out.println("File " + fileNames[i] + " moved successfully"); 
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

Edit:

The main method I used for this code is this:

public static void main(String[] args) 
{
    //Stores the path name input by the user for future reference
    String mainPath = userInput();

    //Creates an instance of the data that is collected from the images in the mainPath
    GetData data = new GetData();

    //Creates a reference to the collection of the names of all the images in the mainPath 
    String[] names = data.getNames(mainPath);

    //Creates a reference to the collection of the dates of all the images in the mainPath
    String[] dates = data.getDates(mainPath, names);

    //Create a unique directory for each date extracted from images if it doesn't already exist
    createDirectory(mainPath, dates);

    // Move each image to its respectful directory depending on its creation date
    moveImages(mainPath, dates, names);

    // Lets the user know the program is done running 
    System.out.println("End of program");            
}

and the error it prints out is the following:

java.nio.file.FileSystemException: C:\Users\Owner\Desktop\try\IMG_7100.CR2 -> C:\Users\Owner\Desktop\try\Jul 28, 2019\IMG_7100.CR2: The process cannot access the file because it is being used by another process.

at java.base/sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at java.base/sun.nio.fs.WindowsFileCopy.move(Unknown Source)
at java.base/sun.nio.fs.WindowsFileSystemProvider.move(Unknown Source)
at java.base/java.nio.file.Files.move(Unknown Source)
at Organize.moveImages(Organize.java:149)
at Organize.main(Organize.java:29)
Tsion
  • 1
  • 2
  • Please edit your question to show the complete stack trace of the exception. It would also be helpful if you could write a main() method which calls your function and illustrates the problem. – tgdavies Nov 30 '20 at 03:55
  • Make sure you close streams if used in the methods you have not shown - get Dates/Names – DuncG Nov 30 '20 at 08:39

0 Answers0