1

I want to upload new file under a given path if path does not exist , i want to create that path by creating directories, and then upload that file, If path exist i just want to put my file on that path.

for. e.g. 'pandora' is name of my main folder on google drive i want to do all the directory and file manipulations under it.

folderPath = /panodra/augusta/weber/

fileName = DEC

in following method i am trying to find out 'pandora' folder but it is returning empty item list.

private static boolean isFolderExist(String folderPath, String fileName) throws IOException {

            Files.List request = drive.files().list().setQ(
                "mimeType='application/vnd.google-apps.folder' and trashed=false and title='pandora' ");
            FileList files = request.execute();
            List<File> fl1 = files.getItems();
            System.out.println(fl1);
            System.out.println(files);
            return false;
     }
Clay
  • 4,700
  • 3
  • 33
  • 49
axnet
  • 5,146
  • 3
  • 25
  • 45

1 Answers1

1

For the Folder part, there is no clear way to get this if you check the Drive documentation. What can I give you is a related SO question that can do it but it uses the Google Drive Android API. So just check it and get the idea on how to do it.

For the Files part, you can check the Search for Files part of the Google Drive documentation to search the files. It uses the files.list method of the Drive API that accepts q parameter which is a search query combining one or more search clauses.

Here is a sample code on the documentation that shows how to perform a search of image files.

String pageToken = null;
do {
FileList result = driveService.files().list()
.setQ("mimeType='image/jpeg'")
.setSpaces("drive")
.setFields("nextPageToken, files(id, name)")
.setPageToken(pageToken)
.execute();
for(File file: result.getFiles()) {
System.out.printf("Found file: %s (%s)\n",
file.getName(), file.getId());
}
pageToken = result.getNextPageToken();
} while (pageToken != null);
Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31