0

I am making a program which hopefully will organise my desktop because I am god awful at doing that.

I've just learnt how to make a program that will move a particular file from one location to another as well as list all the files within a particular directory so I can determine which files I want to move.

The problem is, is that the file being moved has to be known already in the sense you have to give the file name and extension. What I want to do is make it so the program scans the directory, presents this to the user, asks what they want to sort, the program then looks for all the files which are relevant to what the user wants to sort via a keyword and is then moved to the appropriate folder.

I've tried making a variable which contains the name of the file and adding that to the line of code which didn't work.

    String fileMove = null;

    File folder = new File("/Users/james/Desktop/Desktop2");
    File[] listOfFiles = folder.listFiles();

    System.out.println("The files currently in this directory are:");

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

        System.out.println(listOfFiles[i].getName());


    }
    System.out.println("");
    System.out.println("What files do you want to sort?");

    String userResponse = userInput.nextLine();

    if (userResponse.equals("English")) {


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

            if(listOfFiles[i].getName().contains("English")) {

                fileMove = listOfFiles[i].getName();
            }


        }

        File sourceFile = new File("/Users/james/Desktop/Desktop2/ " + fileMove);

        File destinationFile = new File("/Users/james/Desktop/Desktop2/English " + fileMove);

        System.out.println("Operation complete");

This program was expected to: List the files in a directory, asks the user what files are to be sorted, sort the files.

The program actually: Lists the files in a directory, asks the user what files are to be sorted, n/a

James
  • 1
  • When you write "sort" you mean move? Right now you try to move after the for loop, you need to do it inside the loop if you want to move all loop that matches but first check that `fileMove` is not null. And I don't see the actual move operation at all. – Joakim Danielson Jan 30 '19 at 13:50
  • Are you sure you want to sort (putting them in a specific order) the files? It looks more like filtering to me. – Peter Bruins Jan 30 '19 at 13:50
  • You can use use renameTo() (https://stackoverflow.com/a/4645271/4632482) to actually move a file. – Peter Bruins Jan 30 '19 at 13:52
  • @JoakimDanielson I added a line of code to check fileMove and it does change value – James Jan 30 '19 at 13:54
  • @PeterBruins but the problem is though is that I would need to know the full path. I'm trying to make the program where I don't know the path until I've inputted it. So for example if I wanted to move 'filex' from the folder 'folder1' to 'folder2' within 'folder1' I could write: File sourceFile = new File("/Users/james/Desktop/folder1/filex.jpg"); File destinationFile = new File("/Users/james/Desktop/folder1/folder2/filex.jpg") But the last part of the path I don't know before hand. However, I do know it as a variable so how would I incorporate that? – James Jan 30 '19 at 13:56
  • @James, take a look at `java.nio.file.Path` and `java.nio.file.Files`. It's a good list of utilities to get you started, and `Path` has a good abstraction of name paths. – M. Prokhorov Jan 30 '19 at 13:59

1 Answers1

0

I realised that I forgot an important part of the code :facepalm:

I also added a little bit to it, but it's working now....

    Scanner userInput = new Scanner(System.in);

    String[] fileMove = new String[10];

    File folder = new File("/Users/james/Desktop/Desktop2");
    File[] listOfFiles = folder.listFiles();

    System.out.println("The files currently in this directory are:");

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

        System.out.println(listOfFiles[i].getName());


    }
    System.out.println("");
    System.out.println("What files do you want to sort?");

    String userResponse = userInput.nextLine();

    if (userResponse.equals("English")) {


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

            if(listOfFiles[i].getName().contains("English")) {

                fileMove[i] = listOfFiles[i].getName();


            System.out.println(fileMove[i]);

            System.out.println("/Users/james/Desktop/Desktop2/" + fileMove[i]);

            File sourceFile = new File("/Users/james/Desktop/Desktop2/" + fileMove[i]);

            System.out.println("/Users/james/Desktop/Desktop2/Eng/" + fileMove[i]);

            File destinationFile = new File("/Users/james/Desktop/Desktop2/Eng/" + fileMove[i]);

            try {
                Files.copy(sourceFile.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

            } catch (IOException e) {

                e.printStackTrace();
            }



            System.out.println("Operation complete");

            }

        }
James
  • 1