Download All Files in Folder

GuidesDownloadsDownload All Files in Folder
Edit this page

Download All Files in Folder

Sometimes an application might want to download all files for a folder. To do so with the SDKs and the CLI requires traversing the folder tree, finding every file and downloading it accordingly.

.NET
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Box.V2.Config;
using Box.V2.JWTAuth;

namespace BoxDownloadAllFiles {
  class Program {
    static void Main (string[] args) {
      ExecuteMainAsync ().Wait ();
    }

    private static async Task ExecuteMainAsync () {
      using (FileStream fs = new FileStream ($"./config.json", FileMode.Open)) {
        var session = new BoxJWTAuth (BoxConfig.CreateFromJsonFile (fs));
        var client = session.AdminClient (session.AdminToken ());
        var folderId = "987654321";
        var folder = await client.FoldersManager.GetInformationAsync (folderId);
        var folderName = folder.Name;
        var localFolderPath = Path.Combine (Directory.GetCurrentDirectory (), folderName);
        ResetLocalFolder (localFolderPath);

        var items = await client.FoldersManager.GetFolderItemsAsync (folderId, 1000, autoPaginate : true);
        var fileDownloadTasks = new List<Task> ();
        var files = items.Entries.Where (i => i.Type == "file");
        foreach (var file in files) {
          fileDownloadTasks.Add (client.FilesManager.DownloadStreamAsync (file.Id).ContinueWith ((t) => {
            var localFile = File.Create (Path.Combine (localFolderPath, file.Name));
            return t.Result.CopyToAsync (localFile);
          }));
        }
        await Task.WhenAll (fileDownloadTasks);
      }
    }

    private static void ResetLocalFolder (string localFolderPath) {
      if (!Directory.Exists (localFolderPath)) {
        Directory.CreateDirectory (localFolderPath);
      } else {
        foreach (var file in Directory.EnumerateFiles (localFolderPath)) {
          File.Delete (Path.Combine (localFolderPath, file));
        }
        Directory.Delete (localFolderPath);
        Directory.CreateDirectory (localFolderPath);
      }
    }
  }
}
Java
package com.box;

import com.box.sdk.BoxConfig;
import com.box.sdk.BoxDeveloperEditionAPIConnection;
import com.box.sdk.BoxFile;
import com.box.sdk.BoxFolder;
import com.box.sdk.BoxItem;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Playground {

 public static void main(String[] args) throws Exception {
  Path configPath = Paths.get("config.json");
  Path currentDir = Paths.get("").toAbsolutePath();
  try (BufferedReader reader = Files.newBufferedReader(configPath, Charset.forName("UTF-8"))) {
   BoxConfig boxConfig = BoxConfig.readFrom(reader);
   BoxDeveloperEditionAPIConnection client = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(boxConfig);
   String folderId = "987654321";
   BoxFolder folder = new BoxFolder(client, folderId);
   String folderName = folder.getInfo().getName();
   Path localFolderPath = currentDir.resolve(Paths.get(folderName));
   if (!Files.exists(localFolderPath)) {
    localFolderPath = Files.createDirectory(localFolderPath);
   } else {
    localFolderPath = resetLocalFolder(localFolderPath);
   }

   for (BoxItem.Info itemInfo: folder) {
    if (itemInfo instanceof BoxFile.Info) {
     BoxFile.Info fileInfo = (BoxFile.Info) itemInfo;
     BoxFile file = new BoxFile(client, fileInfo.getID());
     String localFilePath = localFolderPath.resolve(Paths.get(fileInfo.getName())).toAbsolutePath().toString();
     FileOutputStream stream = new FileOutputStream(localFilePath);
     file.download(stream);
     stream.close();
    }
   }

  }
 }

 static Path resetLocalFolder(Path localFolderPath) throws IOException {
  Files.list(localFolderPath).forEach(file -> {
   System.out.println(file.getFileName());
   try {
    Files.delete(file.toAbsolutePath());
   } catch (IOException e) {}
  });
  Files.delete(localFolderPath);
  localFolderPath = Files.createDirectory(localFolderPath);
  return localFolderPath;
 }
}
Node
"use strict";
const box = require("box-node-sdk");
const fs = require("fs");
const util = require("util");
const path = require("path");
let configFile = fs.readFileSync("config.json");
configFile = JSON.parse(configFile);

let session = box.getPreconfiguredInstance(configFile);
let client = session.getAppAuthClient("enterprise");
client._useIterators = true;

let folderId = "987654321";
let folderName;
let localFolderPath;
client.folders
  .get(folderId, null)
  .then(folderInfo => {
    folderName = folderInfo.name;

    return client.folders.getItems(folderId, { limit: 1000 });
  })
  .then(folderItemsIterator => {
    return autoPage(folderItemsIterator);
  })
  .then(folderItems => {
    console.log(folderName);
    console.log(folderItems.length);
    let files = folderItems.filter(item => {
      return item.type === "file";
    });
    console.log(files);
    localFolderPath = createLocalFolder(folderName);
    let downloadPromises = [];
    files.forEach(file => {
      downloadPromises.push(
        client.files.getReadStream(file.id, null).then(stream => {
          let output = fs.createWriteStream(
            path.join(localFolderPath, file.name)
          );
          stream.pipe(output);
        })
      );
    });
    return Promise.all(downloadPromises);
  })
  .then(() => {
    console.log("Downloaded all files...");
    console.log(fs.readdirSync(localFolderPath));
  });

function createLocalFolder(folderName) {
  let localFolderName = path.join(__dirname, folderName);
  try {
    fs.mkdirSync(localFolderName);
  } catch (e) {
    if (e.code === "EEXIST") {
      resetLocalFolder(localFolderName);
      fs.mkdirSync(localFolderName);
    } else {
      throw e;
    }
  }
  return localFolderName;
}

function resetLocalFolder(localFolderName) {
  if (fs.existsSync(localFolderName)) {
    fs.readdirSync(localFolderName).forEach(localFileName => {
      console.log(localFileName);
      fs.unlinkSync(path.join(localFolderName, localFileName));
    });
    fs.rmdirSync(localFolderName);
  }
}

function autoPage(iterator) {
  let collection = [];
  let moveToNextItem = () => {
    return iterator.next().then(item => {
      if (item.value) {
        collection.push(item.value);
      }
      if (item.done !== true) {
        return moveToNextItem();
      } else {
        return collection;
      }
    });
  };
  return moveToNextItem();
}

It is important to remember that an application needs to have the permissions to access and download the files and folders in question. When the authenticated user does not have access to any of the files or folders, a HTTP 404 Not Found error will occur.

Lear more about User Types in our guides on authentication.