简体   繁体   中英

How to list folders and files inside a specific folder

I have some queries regarding the google drive api. I am following the code from here( https://everyday-be-coding.blogspot.com/ )

  1. How to change default file list length(100) to full file list length from the google drive ?
  2. How to avoid listing of file from the trash of google drive?
  3. How to search and list out the content inside a specific folder with a folder name in the google drive?

These are the issues are i am facing on with google drive api.Please help me out from these issues.

This is the code i am using to list the files

 public static List<GoogleDriveFiles> GetDriveFiles()
    {
        DriveService service = GetService();

        // Define parameters of request.

        FilesResource.ListRequest FileListRequest = service.Files.List();
        FileListRequest.Q = "mimeType = 'application/vnd.google-apps.folder'";

        FileListRequest.Fields = "nextPageToken, files(id, name, size, version, trashed, createdTime)";

        // List files.
        IList<Google.Apis.Drive.v3.Data.File> files = FileListRequest.Execute().Files.Where(c => c.Trashed == false).ToList();
        List<GoogleDriveFiles> FileList = new List<GoogleDriveFiles>();

        if (files != null && files.Count > 0)
        {
            foreach (var file in files)
            {
                GoogleDriveFiles File = new GoogleDriveFiles
                {
                    Id = file.Id,
                    Name = file.Name,
                    Size = file.Size,
                    Version = file.Version,
                    CreatedTime = file.CreatedTime
                };
                FileList.Add(File);
            }
        }
        return FileList;
    }

How to change default file list length(100) to full file list length from the google drive ?

files.list as a default max page size of 100 but you can increase that by sending the pageSize parameter you can set it between 100 - 1000.

After that you will need to use the NextPageToken. You are in luck in that we have a PageStreamer method for this in C#

var pageStreamer = new Google.Apis.Requests.PageStreamer<Google.Apis.Drive.v3.Data.File, FilesResource.ListRequest, Google.Apis.Drive.v3.Data.FileList, string>(
                                                   (req, token) => request.PageToken = token,
                                                   response => response.NextPageToken,
                                                   response => response.Files);

I have a tutorial on how to use it here https://www.daimto.com/list-all-files-on-google-drive/

How to avoid listing of file from the trash of google drive?

File.list has another paramater called the q paramater which will allow you to search for the files you are looking for search

so the following would ignore trashed files.

q = trashed = false

How to search and list out the content inside a specific folder with a folder name in the google drive?

To do this you will need to find the folder you are looking for the eastest way to do that would be to again use the q paramater and search for only folders with the name will help you find the folder you are looking for.

mimeType != 'application/vnd.google-apps.folder' and name = 'hello'

Once you have it save the file.id for that folder

mimeType != 'application/vnd.google-apps.folder' and name = 'hello'

Then you can make another request which will find all of the files in a parent folder

'the file id here' in parents

The best place to test your searches is in the try me on the documentation page here

My tutorials Search files on Google Drive with C#

  1. There is no file listing limit in Google Drive, you can use the nextPage parameter and it will continue in another page when it reaches 100 files.
  2. You can avoid listing the files in Trash by using the query trashed = false .
  3. To search files in a folder, you need the folder Id and the search query 'folder id' in parents .

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM