Home > Mobile >  Using the Dropbox api interface to remove files older than x days old
Using the Dropbox api interface to remove files older than x days old

Time:11-10

I wrote an app that uploads a zipped file of our data to a folder that is specific to the company uploading the data. This program is working without issue.

I then wrote another app that pulls that data down to the customer's site as an onsite backup. This was working well until the file count reached approx 700 zip files. I am requesting a list of zip files in a specific directory, parsing out the name which includes the date/tim created, and getting the oldest file. When I their api, it's not returning the complete list, which doesn't include the latest zip file.

My code:

private static string GetFileFromDropbox()
{

  try
  {
    var client = CreateDropBoxInstance();
    var pathtofiles = $"/{Settings.Default.CompanyCode}";

    //var list = await client.Files.ListFolderGetLatestCursorAsync(pathtofiles);
    
    // changed 11/9/22 by Eric since it was not returning all files
    //var list = client.Files.ListFolderAsync(pathtofiles);
    var list = client.Files.ListFolderAsync(pathtofiles, true, true, false);
    
    var newlist = new List<string>();

    foreach (var l in list.Result.Entries)
    {
      if (!l.IsFile) continue;
      newlist.Add(l.PathLower.Replace($"/{Settings.Default.CompanyCode.ToLower()}/", string.Empty));
    }

    var dict = new Dictionary<DateTime, string>();
    foreach (var fn in newlist)
    {
      // filename: AAB.20211126234249.zip
      var parse = fn.Split('.');
      if (parse.Length != 3) continue;

      var date = parse[1];
      // cc yy mm dd hh mm   ss
      // 20 21 11 26 23 42   49
      // 01 23 45 67 89 1011 1213

      if (date.Length != 14) continue;

      var cc = int.Parse(date.Substring(0, 2));
      var yy = int.Parse(date.Substring(2, 2));
      var mm = int.Parse(date.Substring(4, 2));
      var dd = int.Parse(date.Substring(6, 2));
      var hh = int.Parse(date.Substring(8, 2));
      var mi = int.Parse(date.Substring(10, 2));
      var ss = int.Parse(date.Substring(12, 2));
      var filedate = new DateTime(cc   yy, mm, dd, hh, mi, ss);
      dict.Add(filedate, fn);

    }

    var qry = (from x in dict
               orderby x.Key descending
               select x.Value).FirstOrDefault();
    return qry;
  }
  catch (Exception e)
  {
    Console.WriteLine(e);
    throw;
  }
}

My issue is that I'm not getting a complete list of the files in the specified directory so I end up with the wrong file. This makes my program pretty much useless since it's not getting the right file.

How do I go about getting the complete list of files in the directory instead of a partial list?

CodePudding user response:

The ListFolder interface is paginated, and you are not guaranteed to get everything returned in a single call to ListFolderAsync. You'll need to implement ListFolderContinueAsync as well.

Refer to the ListFolderAsync and ListFolderContinueAsync documentation for more information

  • Related