cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and more here.

Discuss Dropbox Developer & API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: Move files and Folders

Move files and Folders

developersSave
Helpful | Level 6

Hi everyone, 

I've a question:
I need to move files and folders using dropbox API in my Java project. Do you have some code examples? I can't find them online and I don't know if it's possibile do that.

Thank you!

1 Accepted Solution

Accepted Solutions

developersSave
Helpful | Level 6

Hi @Здравко ,

I solved everything in this way (all Paths are made with a DropBox id):

 

public static String moveFile(SuiteConfiguration suiteConfiguration,

String fromPath,String toPath) throws RelocationErrorException, DbxException {

DbxClientV2 dropboxClient = getV2Client(suiteConfiguration);

 "id:TMSaeghsn0AAAAAAAACV2w/ios.png").withAllowOwnershipTransfer(true).withAllowSharedFolder(true).withAutorename(true);

RelocationResult result = dropboxClient.files().moveV2(fromPath, toPath);

return result.getMetadata().getPathDisplay();

}

 

public static void moveFolder(SuiteConfiguration suiteConfiguration,

String fromPath,String toPath) throws RelocationErrorException, DbxException, InterruptedException {

DbxClientV2 dropboxClient = getV2Client(suiteConfiguration);

MoveV2Builder withAllowSharedFolder = dropboxClient.files().moveV2Builder(fromPath,toPath).withAllowOwnershipTransfer(true).withAllowSharedFolder(true).withAutorename(false);

RelocationResult result = withAllowSharedFolder.start();

}

 

 

Thank you for your help

View solution in original post

14 Replies 14

Здравко
Legendary | Level 20

Hi @developersSave,

You can use moveV2​ method to move/rename a file/folder. For different examples showing simple applications (hello world style), take a look here.

Hope this helps.

developersSave
Helpful | Level 6

Hi  @Здравко 

 

That's my code! I'm trying to get my new folder path from it dropbox id using getFolderPath() method, but it doesn't work. 
After that, I use moveFile() method.

 

public static String getFolderPath(SuiteConfiguration suiteConfiguration, String id) throws DbxException {

DbxClientV2 dropboxClient = getV2Move(suiteConfiguration);

String folderPath = "";

try {

Metadata metadata = dropboxClient.files().getMetadata(id);

if (metadata instanceof FolderMetadata) {

folderPath = ((FolderMetadata) metadata).getPathDisplay();

}

} catch (DbxException e) {

e.printStackTrace();

}

 

return folderPath;

}

public static FileMetadata moveFile(SuiteConfiguration suiteConfiguration,

String fromPath,String toPath) throws RelocationErrorException, DbxException {

DbxClientV2 dropboxClient = getV2Move(suiteConfiguration);

RelocationResult result = dropboxClient.files().moveV2(fromPath, toPath);

return (FileMetadata) result.getMetadata();

}

Здравко
Legendary | Level 20

@developersSave wrote:

... I'm trying to get my new folder path from it dropbox id using getFolderPath() method, ...


@developersSave, You don't need to get existing file/folder path from id. The id may be used on path's place instead. So, your 'getFolderPath' is meaningless here. Even more - what about files:


@developersSave wrote:

...

try {

Metadata metadata = dropboxClient.files().getMetadata(id);

if (metadata instanceof FolderMetadata) {

folderPath = ((FolderMetadata) metadata).getPathDisplay();

}

} catch (DbxException e) {

e.printStackTrace();

}

...


'FolderMetadata' Ok, but what about 'FileMetadata'? A valid object will leads to empty file name; of course incorrect when you pass it to 'moveFile'!

By the way, what kind of error your're receiving? There has to be a clear enough description. What do you exactly mean with "it doesn't work"? 🧐

developersSave
Helpful | Level 6

Good morning @Здравко,

 

By applying the firt method I would like to retrieve both the "from" path (the origin folder) and the "to" path (the destination folder), but I'm getting the following error: com.dropbox.core.v2.files.GetMetadataErrorException: Exception in 2/files/get_metadata: {".tag":"path","path":"not_found"}

Once I have retrieved both paths, by applying the second method I'd like to carry out the move.

Could you suggest me a way to do this?

Also, what do you mean exactly when you say "meaningless"? Could you explain a little bit better?

It's the first time I use Dropbox to do this and naybe there is something I'm doing wrong without knowing.

 

Thank you!

Здравко
Legendary | Level 20

@developersSave wrote:

...

By applying the firt method I would like to retrieve both the "from" path (the origin folder) and the "to" path (the destination folder), but I'm getting the following error: com.dropbox.core.v2.files.GetMetadataErrorException: Exception in 2/files/get_metadata: {".tag":"path","path":"not_found"}

...


@developersSave, Such an error shows that something with your id is not correct. Where you got this id from? It can results from incorrect id format (the id always starts with "id:..") or incorrect combining with other symbols.

 


@developersSave wrote:

...

Also, what do you mean exactly when you say "meaningless"? Could you explain a little bit better?

...


The documentation has explained it already:


fromPath - Path in the user's Dropbox to be copied or moved. Must match pattern "(/(.|[\\r\\n])*)|(ns:[0-9]+(/.*)?)|(id:.*)" and not be null.

In other words, as you have passed id to get metadata, in the same way you can pass file/folder id as fromPath parameter (it's the same). 'id' can be use as path in all cases where existing object is expected to be there. In such a way you perform one API call less, so move's work become faster.

 


@developersSave wrote:

...

Once I have retrieved both paths, by applying the second method I'd like to carry out the move.

...


When you have non existing object (the target path), you can combine id and trailing part of the path. For instance if targetId is folder where you want to move your file to and filename.ext is the taget name for the file in that folder, then 'toPath' can look something like $targetId/filename.ext (combining id and path). Again you don't need additional API call -> faster work.

 

Let's get back to beginning. Your id is malformed somehow! Find out how!!! Otherwise nothing else will work. Don't change already received id in any way and use it as is.

Hope this gives direction.

developersSave
Helpful | Level 6

Hi @Здравко 

 

Everytime I create a folder in Dropbox, I save its ID on Database. So, the ID I pass is the one I had saved when creating the folder.

As a result, should the path necessarily be managed with '/general-folder/my-folder'? May I use 'id:AAAAetcetc'?

If I got it right, my 'toPath' would become something like 'id:AAAAectect/my-file.txt', is that correct?

Thank you for you support, really.

Best regards

Здравко
Legendary | Level 20

@developersSave wrote:

...

As a result, should the path necessarily be managed with '/general-folder/my-folder'? May I use 'id:AAAAetcetc'?

...


 

Yes, you can use it. It's matter of your choice. Also there are some differences, that you should take in mind, and that can be a trouble if you are not aware or ignore them. The id doesn't point a file path (!!!), it points file content (i.e. the file itself), doesn't matter where this file resides in directory tree. Opposite, the path point a position in directory tree, but in different moments on the same position (i.e. the same path and same file name) can reside different files. Be careful, otherwise those differences may lead to confusion! If you remove some file or directory and then recreate them anew, you may think they are the same,... Hmm, but with DIFFERENT IDs!!! Follow not only file/folder creations, but deletions too and clear appropriately your database records (hope you know already why).


@developersSave wrote:

...

If I got it right, my 'toPath' would become something like 'id:AAAAectect/my-file.txt', is that correct?

...


Yes, exactly. Of course it's your choice, not something mandatory, but in such a way you avoid one additional call. That's it.

 


@developersSave wrote:

... So, the ID I pass is the one I had saved when creating the folder.

...


Ok, but for some reason, the same ID seems broken! Find out why. Broken id wouldn't work passed to move directly the same as doesn't work for try to get metadata.

Hope this sheds additional light.

developersSave
Helpful | Level 6

Hi @Здравко 

does it works also for folders? Can I move them in other folders?

Thank you

Здравко
Legendary | Level 20

@developersSave wrote:

...

does it works also for folders? Can I move them in other folders?
...



@Здравко wrote:

...
In other words, as you have passed id to get metadata, in the same way you can pass file/folder id as fromPath parameter (it's the same). ...

Need more support?