Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
developersSave
3 years agoHelpful | Level 6
Move files and Folders
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 ...
- 3 years ago
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
developersSave
3 years agoHelpful | 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
Здравко
3 years agoLegendary | 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.
- developersSave3 years agoHelpful | Level 6
Hi Здравко
does it works also for folders? Can I move them in other folders?
Thank you - Здравко3 years agoLegendary | 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). ... - developersSave3 years agoHelpful | Level 6
Hi Здравко
that's my method for folders, but it doesn't work (to and from path are both id:etcetc):
public static void moveFolder(SuiteConfiguration suiteConfiguration,
String fromPath,String toPath) throws RelocationErrorException, DbxException, InterruptedException {
DbxClientV2 dropboxClient = getV2Client(suiteConfiguration);
List<RelocationPath> entries = new ArrayList<RelocationPath>();
entries.add(new RelocationPath(fromPath, toPath));
RelocationBatchV2Launch moveBatchJob = dropboxClient.files().moveBatchV2(entries);
RelocationBatchV2JobStatus moveBatchCheck = null;
while (moveBatchCheck == null || moveBatchCheck.isInProgress()) {
// check this occasionally until it's done
moveBatchCheck = dropboxClient.files().moveBatchCheckV2(moveBatchJob.getAsyncJobIdValue());
Thread.sleep(10000);
System.out.println(moveBatchCheck);
}
//MoveV2Builder withAllowSharedFolder = dropboxClient.files().moveV2Builder("id:TMSaeghsn0AAAAAAAACV2Q/ios.png", "id:TMSaeghsn0AAAAAAAACV2w/ios.png").withAllowOwnershipTransfer(true).withAllowSharedFolder(true).withAutorename(true);
//RelocationResult result = withAllowSharedFolder.start();
// RelocationResult result = dropboxClient.files().moveV2(fromPath, toPath);
// return result.getMetadata().getPathDisplay();
}
Thank you!
- Здравко3 years agoLegendary | Level 20
developersSave wrote:...
that's my method for folders, but it doesn't work (to and from path are both id:etcetc):
...
Здравкоwrote:... 'id' can be use as path in all cases where existing object is expected to be there. ...
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). ...
developersSave, Read more careful what I have described already. Where you got the target ID from??? First of all there is no such file/folder yet! Next, the ID is the same, only name and/or path changes, so cannot be different source and target IDs. Be more careful!
By the way, it's meaningless a batch operation for single move.
- developersSave3 years agoHelpful | Level 6
hi Здравко ,
I get my folders and files id from database, I saved them after upload.If I need to move a sub-folder under my-app general folder, what have I to do?
Example:
Applications/My-App/folder-1/folder-2I've to move "folder-2" under "My-App" folder, but I don't have any id for this one.
Thank you! - Здравко3 years agoLegendary | Level 20
developersSave wrote:...
I've to move "folder-2" under "My-App" folder, but I don't have any id for this one.
...developersSave, You don't need id for your app namespace root folder. It's just empty. Pass an empty string instead of real ID. 😉 The concatenation can proceed without change.
- developersSave3 years agoHelpful | 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
About Discuss Dropbox Developer & API
Make connections with other developers
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!