Question

I am trying to use the Apache Common's FileUtil to use CopyDirectory to transfer files onto other disks. I am however getting an error when it comes to transfering My Documents. Also, I don't know why, but when I select a specific path like C:\Users[UserName]\Documents it doesn't give me JUST the contents in Documents it gives me a few other folders in the User's Folder... I don't know why this is. I also IOException when I run CopyDirectory on MyDocuments... when it's trying to copy MyMusic (which I don't know why it's doing that in the first place.

In my code it copies Favorites and Desktop decently... though it seems to have changed the folder in Favorites "Favorites Toolbar" to "Links" folder.

Any help would be great. Thanks!

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.io.*;

public class TransferJet {
public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
public static void main(String[] args) throws IOException {

        System.out.print("Please enter the username:");
        String user = in.readLine();

        System.out.print("Please enter the drive letter of the old primary partition:");
        String oldPrimaryLetter = in.readLine();
        System.out.print("Please enter the drive letter of the old secondary partition:");
        String oldSecondaryLetter = in.readLine();

        System.out.print("Please enter the drive letter of the new primary partition:");
        String newPrimaryLetter = in.readLine();
        System.out.print("Please enter the driver letter of the new secondary partition:");
        String newSecondaryLetter = in.readLine();


        if(user.equals("") || oldSecondaryLetter.equals("") || oldPrimaryLetter.equals("") || newPrimaryLetter.equals("") || newSecondaryLetter.equals(""))
        {
            System.out.println("Invalid directory or file name.");
            System.exit(0);
        }

        String newFiles = "";
        String oldFiles = "";
        File source = null;
        File destination = null;

        System.out.println("Moving Favorites...");
        oldFiles = oldPrimaryLetter + ":\\Users\\" + user + "\\Favorites";
        //String oldFilesA = "C:\\Users\\ashih\\Favorites";
        //String newFilesA = "G:\\Users\\ashih\\Favorites";
        newFiles = newPrimaryLetter + ":\\Users\\" + user + "\\Favorites";
        source = new File(oldFiles);
        destination = new File(newFiles);
        FileUtils.copyDirectory(source, destination);


        System.out.println("Moving Desktop...");
        oldFiles = oldPrimaryLetter + ":\\Users\\" + user + "\\Desktop";
        newFiles = newPrimaryLetter + ":\\Users\\" + user + "\\Desktop";
        source = new File(oldFiles);
        destination = new File(newFiles);
        FileUtils.copyDirectory(source, destination);

        System.out.println("Moving My Documents...");
        oldFiles = oldPrimaryLetter + ":\\Users\\" + user + "\\";
        newFiles = newPrimaryLetter + ":\\Users\\" + user + "\\Documents";
        source = new File(oldFiles);
        destination = new File(newFiles);
        FileUtils.copyDirectory(source, destination);

        System.out.println("Moving Old Secondary to New Secondary...");
        oldFiles = oldSecondaryLetter + ":\\";
        newFiles = newSecondaryLetter + ":\\";
        source = new File(oldFiles);
        destination = new File(newFiles);
        FileUtils.copyDirectory(source, destination);


}

}

Was it helpful?

Solution

The line:

oldFiles = oldPrimaryLetter + ":\\Users\\" + user + "\\";
newFiles = newPrimaryLetter + ":\\Users\\" + user + "\\Documents";

Doesn't look right, should it be

oldFiles = oldPrimaryLetter + ":\\Users\\" + user + "\\Documents";
newFiles = newPrimaryLetter + ":\\Users\\" + user + "\\Documents";

That would at least explain why you're copying music :P

I did a quick test and could replicate the issue. When I had a look at directory listing I found

20/12/2010  06:56 PM    <JUNCTION>     My Music [C:\Users\swhitehead\Music]
20/12/2010  06:56 PM    <JUNCTION>     My Pictures [C:\Users\swhitehead\Pictures]
20/12/2010  06:56 PM    <JUNCTION>     My Videos [C:\Users\swhitehead\Videos]

Are "hidden" JUNCTION folders. When I asked Apache commons-io to list the contents the documents folder, these folders where missing.

I need to run a check against the source to find out more

UPDATE I've done some more testing. It appears that Java can not handle JUNCTIONS (or short cuts for that matter). The Commons-IO is throwing an Exception because when it tries to list the contents the "My Music" directory, it returns a null value.

I did this quick test to try it out:

    File myMusic = new File(oldFiles + File.separator + "My Music");
    System.out.println(myMusic);
    System.out.println(myMusic.isDirectory());
    System.out.println(myMusic.isAbsolute());
    System.out.println(myMusic.isHidden());
    System.out.println(myMusic.getCanonicalFile());
    System.out.println(myMusic.getAbsoluteFile());

    File[] lstFiles = myMusic.listFiles();
    if (lstFiles == null) {

        System.out.println("Can not list...");

    } else {

        for (File file : lstFiles) {

            System.out.println(file);

        }

    }

It will print "Can not list..." every time.

As for a solution...you could ask for a list of files (as we've seen this at least works) and then copy each file individually using the FileUtils.copyFile method instead, but that would leave some files behind (and also mean breaking the links that would otherwise be available).

The only way I can think to allow you to (in some way) perseve this functioanlity is to execute an OS process to do the work for (recreate the Junctions and/or list them)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top