目录

Starting a File Scanning Task

Use Cases

Enterprise Data Guard Kit allows apps to scan files in a common directory or at specified paths to obtain the file list.

API Description

For details about these APIs, please refer to the API Reference.

API Description
startFileScanTask(type: CommonDirScanType, callback: ScanFileCallback, batchNum?: number): void Scans a common directory and returns the result using a callback.
startFileScanTask(path: string, callback: ScanFileCallback, batchNum?: number): void Scans a specified directory and returns the result using a callback.

Development Procedure

  1. Import required modules.

    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { fileGuard } from '@kit.EnterpriseDataGuardKit';
  2. Initialize the guard object of FileGuard and declare the callback function for returning the scanning result.

    • Scan files in the common directory and check the scanning result.

      const TAG: string = 'FileGuard_Scan';
      const DOMAIN: number = 0x0000;
      
      /**
       * Start to scan files in a public directory.
       */
      function startFileScanTaskUnderCommonDir() {
        let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
        let onReceiveFileList: (files: string[]) => void = (files: Array<string>) => {
          files.forEach((value: string) => {
            hilog.info(DOMAIN, TAG, `Succeeded in getting file: ${value}.`);
          })
        };
        let onCompleteScanTask: (count: number) => void = (count: number) => {
          hilog.info(DOMAIN, TAG, `Succeeded in getting count: ${count}.`);
        };
        let scanFileCallback: fileGuard.ScanFileCallback = {
          onReceiveFileList: onReceiveFileList,
          onTaskCompleted: onCompleteScanTask
        };
        guard.startFileScanTask(fileGuard.CommonDirScanType.MEDIA_ONLY, scanFileCallback);
      }
    • Scan files in a specified directory under the common directory and check the scanning result.

      const TAG: string = 'FileGuard_Scan';
      const DOMAIN: number = 0x0000;
      
      // ...
      /**
       * Start to scan files in a specified directory.
       */
      function startFileScanTaskUnderSpecifiedDir() {
        let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
        let path: string = '/data/service/el2/test';
        let onReceiveFileList: (files: string[]) => void = (files: Array<string>) => {
          files.forEach((value: string) => {
            hilog.info(DOMAIN, TAG, `Succeeded in getting file: ${value}.`);
          })
        };
        let onCompleteScanTask: (count: number) => void = (count: number) => {
          hilog.info(DOMAIN, TAG, `Succeeded in getting count: ${count}.`);
        };
        let scanFileCallback: fileGuard.ScanFileCallback = {
          onReceiveFileList: onReceiveFileList,
          onTaskCompleted: onCompleteScanTask
        };
        guard.startFileScanTask(path, scanFileCallback);
      }