Search code examples
javabox-apibox

Box API Java: How can I move a file into another folder?


So I am trying to move a file into another folder. The box api says the following

To move a folder, update the ID of its parent.

But there doesn't seem to be a method on the BoxItem.Info to setID(), like there is setName()

I'm trying to do something similar to the api example here

BoxFile file = new BoxFile(api, fileID);
BoxFile.Info info = file.new Info();
String parentID = info.getParent().getID();

BoxFolder parentFolder = new BoxFolder(api, parentID);
BoxFolder.Info parentInfo = parentFolder.new Info();

parentInfo.setID(newID); // Method doesn't exist

parentFolder.updateInfo(parentInfo);

It also seems strange to me that there isn't a file method to simply .move(), it exists for folders.


Solution

  • Instead of using the updateInfo method, use move(BoxFolder destination) on the file to be moved with the destination folder.

    Code Sample:

    String fileID = "1234";
    String destinationFolderID = "5678";
    BoxFile file = new BoxFile(api, fileID);
    BoxFolder destinationFolder = new BoxFolder(destinationFolderID);
    file.move(destinationFolder)
    

    Also, to avoid name conflicts in the destination folder, you can optionally provide a new name for the file to move(BoxFolder destination, String newName). The file will be placed into the destination folder with the new name.

    Code Sample:

    String fileID = "1234";
    String destinationFolderID = "5678";
    BoxFile file = new BoxFile(api, fileID);
    BoxFolder destinationFolder = new BoxFolder(destinationFolderID);
    file.move(destinationFolder, "Vacation Photo (1).jpg");