0

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);


}

}

Alan
  • 9,331
  • 14
  • 52
  • 97
  • What's the exception? The main problem (I can think of off the top of my head) is that the many of the directories you are trying to copy are actually "links" (more precisely, there normally junctions), which Java/the IO Commons may not be able to handle – MadProgrammer Jul 20 '12 at 23:17
  • I am not at the computer anymore with the code, but I believe it was IO Exception.IO for MyMusic (can't remember the specific about the error). I tried deleting the MyMusic Folder to see what would happen and then I got something along the lines of IOException (Access Denied to My Music). Is there anyway to get links to My Documents that is not "Linked" or has junctions. I just strictly want the content in the My Documents Folder... – Alan Jul 20 '12 at 23:44

1 Answers1

1

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)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Hello MadProgrammer, Thanks for going through all that detail in explaining that to me. I did have it at \\Documents for both, but it threw the IO Exception too as you said because of the junctions. I will definitely look into the list of files and copying them manually. What did you mean when you said "that would leave some files behind and also mean breaking the links that would otherwise be available" Do you mean it would break the link to MyMusic? That would be great if that's the case. Thanks again! – Alan Jul 21 '12 at 13:35
  • @AlanShih, yeah, you're going to lose any of the junctions (my musc, videos & pictures in my example) – MadProgrammer Jul 21 '12 at 20:11