Skip to content

Instantly share code, notes, and snippets.

@cburnette
Last active July 28, 2018 06:34
Show Gist options
  • Save cburnette/f58d30bf9d2edd2b6d80 to your computer and use it in GitHub Desktop.
Save cburnette/f58d30bf9d2edd2b6d80 to your computer and use it in GitHub Desktop.
This code snippet will connect to a Box account and loop through all the folders recursively, printing out all the filenames, while highlighting in red files that have a modified_at date older than the specified threshold. In other words, it highlights files that haven't been changed in a while.
//requires .NET 4.5
using Box.V2;
using Box.V2.Auth;
using Box.V2.Config;
using Box.V2.Exceptions;
using Box.V2.Models;
using Nito.AsyncEx;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
const string CLIENT_ID = "YOUR_CLIENT_ID";
const string CLIENT_SECRET = "YOUR_CLIENT_SECRET";
const string DEV_ACCESS_TOKEN = "YOUR_DEV_ACCESS_TOKEN";
const string REFRESH_TOKEN = "THIS_CAN_ANY_RANDOM_STRING";
static readonly DateTime MODIFIED_AT_CUTOFF = new DateTime(2013, 7, 1);
static BoxClient client;
static void Main(string[] args)
{
//http://blog.stephencleary.com/2012/02/async-console-programs.html
try
{
var config = new BoxConfig(CLIENT_ID, CLIENT_SECRET, new Uri("http://localhost"));
var session = new OAuthSession(DEV_ACCESS_TOKEN, REFRESH_TOKEN, 3600, "bearer");
client = new BoxClient(config, session);
AsyncContext.Run(() => MainAsync());
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
}
Console.WriteLine();
Console.Write("Press return to exit...");
Console.ReadLine();
}
static async Task MainAsync()
{
BoxFolder root = await client.FoldersManager.GetInformationAsync("0");
await LoadFolder(root);
}
static async Task LoadFolder(BoxFolder folder)
{
try
{
var fullPath = folder.PathCollection.Entries.Aggregate("/", (prefix, f) => prefix + f.Name + "/");
Console.WriteLine(fullPath + folder.Name);
var items = await client.FoldersManager.GetFolderItemsAsync(folder.Id, 100, 0, new List<string>() { BoxFolder.FieldName, BoxFolder.FieldPathCollection, BoxFolder.FieldModifiedAt });
var folders = items.Entries.OfType<BoxFolder>().ToList();
var files = items.Entries.OfType<BoxFile>().ToList();
files.ForEach((f) =>
{
var currColor = Console.ForegroundColor;
if (f.ModifiedAt.HasValue && f.ModifiedAt.Value < MODIFIED_AT_CUTOFF)
{
Console.ForegroundColor = ConsoleColor.Red;
}
Console.WriteLine("--> {0}",f.Name);
Console.ForegroundColor = currColor;
});
Console.WriteLine();
await folders.ForEachAsync(1,(f) => LoadFolder(f));
}
catch (BoxException bex)
{
var currColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Error.WriteLine(bex.StatusCode);
Console.ForegroundColor = currColor;
}
}
}
public static class Extensions
{
//http://blogs.msdn.com/b/pfxteam/archive/2012/03/05/10278165.aspx
public static Task ForEachAsync<T>(this IEnumerable<T> source, int degreeOfParallelism, Func<T, Task> body)
{
return Task.WhenAll(
from partition in Partitioner.Create(source).GetPartitions(degreeOfParallelism)
select Task.Run(async delegate
{
using (partition)
while (partition.MoveNext())
await body(partition.Current);
}));
}
}
}
@SourGrass
Copy link

Hi Chad,

I keep getting the error: "Could not load file or assembly 'Nito.AsyncEx.Enlightenment, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified."

I did a NuGet update but the error is still there. I am a newbie with Box, try to see how the Box app working. Thanks much. An Nguyen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment