फ़ाइलें और फ़ोल्डर खोजना

Drive के किसी उपयोगकर्ता की सभी या सिर्फ़ कुछ फ़ाइलें और फ़ोल्डर वापस करने के लिए, files.list तरीके का इस्तेमाल करें.

आपके पास संसाधन के कुछ तरीकों, जैसे कि files.get और files.update के लिए ज़रूरी fileId को वापस पाने के लिए, files.list तरीके का इस्तेमाल करने का भी विकल्प होता है.

मौजूदा उपयोगकर्ता की 'मेरी ड्राइव' पर मौजूद सभी फ़ाइलें और फ़ोल्डर खोजें

सभी फ़ाइलों और फ़ोल्डर को वापस करने के लिए, बिना किसी पैरामीटर के files.list का इस्तेमाल करें.

मौजूदा उपयोगकर्ता की 'मेरी ड्राइव' पर मौजूद खास फ़ाइलें या फ़ोल्डर खोजना

फ़ाइलों या फ़ोल्डर का कोई खास सेट खोजने के लिए, files.list के साथ क्वेरी स्ट्रिंग q फ़ील्ड का इस्तेमाल करें. इससे, एक या उससे ज़्यादा खोज के लिए शब्दों को मिलाकर, फ़ाइलों को फ़िल्टर किया जा सकता है.

क्वेरी स्ट्रिंग में ये तीन हिस्से होते हैं:

query_term operator values

जगह:

  • query_term, क्वेरी के लिए इस्तेमाल किया गया शब्द या वह फ़ील्ड है जिसे खोजना है. शेयर की गई ड्राइव को फ़िल्टर करने में इस्तेमाल होने वाले क्वेरी टर्म देखने के लिए, खोज क्वेरी के लिए इस्तेमाल होने वाले शब्द और ऑपरेटर देखें.

  • operator क्वेरी शब्द की शर्त बताता है. यह देखने के लिए कि क्वेरी के हर शब्द के साथ कौनसे ऑपरेटर इस्तेमाल किए जा सकते हैं, क्वेरी ऑपरेटर देखें.

  • values वे वैल्यू हैं जिनका इस्तेमाल आपको खोज के नतीजों को फ़िल्टर करने के लिए करना है.

उदाहरण के लिए, नीचे दी गई क्वेरी स्ट्रिंग, खोज को सिर्फ़ फ़ोल्डर दिखाने के लिए फ़िल्टर करती है:

q: mimeType = 'application/vnd.google-apps.folder'

नीचे दिए गए उदाहरण में बताया गया है कि आप JPEG फ़ाइलों के नाम और आईडी के हिसाब से खोज के नतीजों को फ़िल्टर करने के लिए, क्लाइंट लाइब्रेरी का इस्तेमाल कैसे कर सकते हैं. इस उदाहरण में, image/jpeg टाइप की फ़ाइलों के नतीजों को कम करने के लिए, mimeType क्वेरी टर्म का इस्तेमाल किया गया है. इस उदाहरण में, spaces को drive पर सेट किया गया है, ताकि खोज को Drive स्पेस तक सीमित किया जा सके. जब nextPageToken, null लौटाता है, तो और नतीजे नहीं दिखते.

Java

drive/snippets/drive_v3/src/main/java/SearchFile.java
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/* Class to demonstrate use-case of search files. */
public class SearchFile {

  /**
   * Search for specific set of files.
   *
   * @return search result list.
   * @throws IOException if service account credentials file not found.
   */
  public static List<File> searchFile() throws IOException {
           /*Load pre-authorized user credentials from the environment.
           TODO(developer) - See https://developers.google.com/identity for
           guides on implementing OAuth2 for your application.*/
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
        .createScoped(Arrays.asList(DriveScopes.DRIVE_FILE));
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
        credentials);

    // Build a new authorized API client service.
    Drive service = new Drive.Builder(new NetHttpTransport(),
        GsonFactory.getDefaultInstance(),
        requestInitializer)
        .setApplicationName("Drive samples")
        .build();

    List<File> files = new ArrayList<File>();

    String pageToken = null;
    do {
      FileList result = service.files().list()
          .setQ("mimeType='image/jpeg'")
          .setSpaces("drive")
          .setFields("nextPageToken, items(id, title)")
          .setPageToken(pageToken)
          .execute();
      for (File file : result.getFiles()) {
        System.out.printf("Found file: %s (%s)\n",
            file.getName(), file.getId());
      }

      files.addAll(result.getFiles());

      pageToken = result.getNextPageToken();
    } while (pageToken != null);

    return files;
  }
}

Python

drive/snippets/drive-v3/file_snippet/search_file.py
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def search_file():
  """Search file in drive location

  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """
  creds, _ = google.auth.default()

  try:
    # create drive api client
    service = build("drive", "v3", credentials=creds)
    files = []
    page_token = None
    while True:
      # pylint: disable=maybe-no-member
      response = (
          service.files()
          .list(
              q="mimeType='image/jpeg'",
              spaces="drive",
              fields="nextPageToken, files(id, name)",
              pageToken=page_token,
          )
          .execute()
      )
      for file in response.get("files", []):
        # Process change
        print(f'Found file: {file.get("name")}, {file.get("id")}')
      files.extend(response.get("files", []))
      page_token = response.get("nextPageToken", None)
      if page_token is None:
        break

  except HttpError as error:
    print(f"An error occurred: {error}")
    files = None

  return files


if __name__ == "__main__":
  search_file()

Node.js

drive/snippets/drive_v3/file_snippets/search_file.js
/**
 * Search file in drive location
 * @return{obj} data file
 * */
async function searchFile() {
  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app
  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/drive',
  });
  const service = google.drive({version: 'v3', auth});
  const files = [];
  try {
    const res = await service.files.list({
      q: 'mimeType=\'image/jpeg\'',
      fields: 'nextPageToken, files(id, name)',
      spaces: 'drive',
    });
    Array.prototype.push.apply(files, res.files);
    res.data.files.forEach(function(file) {
      console.log('Found file:', file.name, file.id);
    });
    return res.data.files;
  } catch (err) {
    // TODO(developer) - Handle error
    throw err;
  }
}

PHP

drive/snippets/drive_v3/src/DriveSearchFiles.php
use Google\Client;
use Google\Service\Drive;
function searchFiles()
{
    try {
        $client = new Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope(Drive::DRIVE);
        $driveService = new Drive($client);
        $files = array();
        $pageToken = null;
        do {
            $response = $driveService->files->listFiles(array(
                'q' => "mimeType='image/jpeg'",
                'spaces' => 'drive',
                'pageToken' => $pageToken,
                'fields' => 'nextPageToken, files(id, name)',
            ));
            foreach ($response->files as $file) {
                printf("Found file: %s (%s)\n", $file->name, $file->id);
            }
            array_push($files, $response->files);

            $pageToken = $response->pageToken;
        } while ($pageToken != null);
        return $files;
    } catch(Exception $e) {
       echo "Error Message: ".$e;
    }
}

खोज को सिर्फ़ फ़ोल्डर तक सीमित करने के लिए, क्वेरी स्ट्रिंग का इस्तेमाल करके MIME टाइप को q: mimeType = 'application/vnd.google-apps.folder' पर सेट करें

MIME टाइप के बारे में ज़्यादा जानकारी के लिए, Google Workspace और Google Drive में काम करने वाले MIME टाइप देखें.

क्वेरी स्ट्रिंग के उदाहरण

यह टेबल कुछ बुनियादी क्वेरी स्ट्रिंग दिखाती है. असल कोड, खोज के लिए इस्तेमाल की गई क्लाइंट लाइब्रेरी के हिसाब से अलग-अलग होता है.

आपको क्या क्वेरी करनी है उदाहरण
"hello" नाम वाली फ़ाइलें name = 'hello'
"नमस्ते" और "अलविदा" शब्दों वाले नाम वाली फ़ाइलें name contains 'hello' and name contains 'goodbye'
नाम वाली ऐसी फ़ाइलें जिनमें "hello" शब्द नहीं है not name contains 'hello'
ऐसे फ़ोल्डर जो Google के ऐप्लिकेशन हैं या जिनका MIME टाइप मौजूद है mimeType = 'application/vnd.google-apps.folder'
ऐसी फ़ाइलें जो फ़ोल्डर नहीं हैं mimeType != 'application/vnd.google-apps.folder'
ऐसी फ़ाइलें जिनमें "अहम" टेक्स्ट मौजूद है और ट्रैश में मौजूद है fullText contains 'important' and trashed = true
ऐसी फ़ाइलें जिनमें "hello" शब्द शामिल है fullText contains 'hello'
ऐसी फ़ाइलें जिनमें "hello" शब्द नहीं है not fullText contains 'hello'
ऐसी फ़ाइलें जिनमें "hello world" वाक्यांश होता है fullText contains '"hello world"'
क्वेरी वाली ऐसी फ़ाइलें जिनमें "\" वर्ण मौजूद है (उदाहरण के लिए, "\authors") fullText contains '\\authors'
कलेक्शन में मौजूद आईडी वाली फ़ाइलें, जैसे कि parents कलेक्शन '1234567' in parents
किसी संग्रह के ऐप्लिकेशन डेटा फ़ोल्डर में मौजूद फ़ाइलें 'appDataFolder' in parents
ऐसी फ़ाइलें जिनके लिए उपयोगकर्ता "test@example.org" के पास फ़ाइल में बदलाव करने की अनुमति है 'test@example.org' in writers
ऐसी फ़ाइलें जिनके लिए "group@example.org" ग्रुप के सदस्यों के पास फ़ाइल में बदलाव करने की अनुमति है 'group@example.org' in writers
ऐसी फ़ाइलें जिनमें दी गई तारीख के बाद बदलाव किए गए हैं modifiedTime > '2012-06-04T12:00:00' // default time zone is UTC
अनुमति वाले उपयोगकर्ता के साथ शेयर की गई फ़ाइलें, जिनके नाम में "हैलो" लिखा हो sharedWithMe and name contains 'hello'
ऐसी फ़ाइलें जिन्हें किसी के साथ या डोमेन के साथ शेयर नहीं किया गया है (सिर्फ़ निजी या कुछ खास उपयोगकर्ताओं या ग्रुप के साथ शेयर की गई) visibility = 'limited'
ऐसी इमेज या वीडियो फ़ाइलें जिनमें किसी खास तारीख के बाद बदलाव किया गया हो modifiedTime > '2012-06-04T12:00:00' and (mimeType contains 'image/' or mimeType contains 'video/')

कस्टम फ़ाइल प्रॉपर्टी वाली फ़ाइलें खोजना

कस्टम फ़ाइल प्रॉपर्टी वाली फ़ाइलों को खोजने के लिए, कुंजी और वैल्यू के साथ appProperties खोज क्वेरी का इस्तेमाल करें. उदाहरण के लिए, 8e8aceg2af2ge72e78 की वैल्यू वाली additionalID नाम की कस्टम फ़ाइल प्रॉपर्टी खोजने के लिए:

appProperties has { key='additionalID' and value='8e8aceg2af2ge72e78' }

कस्टम फ़ाइल प्रॉपर्टी के बारे में ज़्यादा जानने के लिए, कस्टम फ़ाइल प्रॉपर्टी जोड़ना लेख पढ़ें.

किसी खास लेबल या फ़ील्ड वैल्यू वाली फ़ाइलें खोजें

खास लेबल वाली फ़ाइलें खोजने के लिए, खास लेबल आईडी के साथ labels खोज क्वेरी का इस्तेमाल करें. उदाहरण के लिए: 'labels/LABEL_ID' in labels

किसी खास लेबल आईडी के बिना फ़ाइलें खोजने के लिए: Not 'labels/LABEL_ID' in labels

फ़ील्ड की खास वैल्यू के हिसाब से भी फ़ाइलें खोजी जा सकती हैं. उदाहरण के लिए, टेक्स्ट वैल्यू वाली फ़ाइलें खोजने के लिए: labels/LABEL_ID.text_field_id = 'TEXT'

ज़्यादा जानकारी के लिए, खास लेबल या फ़ील्ड वैल्यू वाली फ़ाइलें खोजें देखें.

कॉर्पस खोजें

files.list को कॉल करने वाली खोजों में, डिफ़ॉल्ट रूप से user कॉर्पस का इस्तेमाल होता है.Google Workspace डोमेन के साथ शेयर की गई फ़ाइलों जैसे दूसरे कॉर्पस को खोजने के लिए, corpora पैरामीटर का इस्तेमाल करें.

एक ही क्वेरी में एक से ज़्यादा कॉर्पस (कॉर्पोरेशन) की खोज की जा सकती है. हालांकि, अगर मिला-जुला कॉर्पस बहुत बड़ा हो, तो अधूरे नतीजे मिल सकते हैं. अगर incompleteSearch नतीजा true है, तो सभी दस्तावेज़ नहीं लौटाए गए हैं.