You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.
Forum Discussion
developersSave
2 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 ...
- 2 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
Здравко
2 years agoLegendary | 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.
- developersSave2 years agoHelpful | 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();
}
- Здравко2 years agoLegendary | 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"? 🧐
- developersSave2 years agoHelpful | 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!
About Discuss Dropbox Developer & API
Make connections with other developers
795 PostsLatest Activity: 4 hours agoIf 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 or Facebook.
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!