locked
get all files in directory and sub-directory.

    Question

  • I'm trying to use StorageFolder.getFilesAsync to retrieve all files in the directory *and* sub-directories.

    The documentation says it should give me the files of all sub-directories if I use a CommonFileQuery. I've tried just passing in a default query like this:

    sourceDir.getFilesAsync(Windows.Storage.Search.CommonFileQuery.defaultQuery).then(function (files) {

    but that doesn't give the files in the sub-directory. Reading about the QueryOptions  I suspect that I have to use this to set the folderDepth to deep but there is no example of this and trying

    var query = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.defaultQuery)
    query.folderDepth = Windows.Storage.Search.FolderDepth.deep;
    sourceDir.getFilesAsync(query).then(function (files) {

    doesn't return the files in sub-directories either.

    How do you get all files within a folder?

    Friday, March 23, 2012 12:24 AM

Answers

  • The issue is that the default CommonFileQuery is shallow. Also, in the above you cannot pass in query to getFilesAsync. You have two ways to accomplish this:

    1.

    var options = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.orderByName, ["*"])
    varquery = sourceDir.createFileQueryWithOptions(options);

    query.getFilesAsync().then(function (files) {

    2.

    sourceDir.getFilesAsync(Windows.Storage.Search.CommonFileQuery.orderByName).then(function (files) {

    Monday, March 26, 2012 11:55 PM

All replies

  • What folder are you trying to get the list of files in?

    Jeff Sanders (MSFT)

    Monday, March 26, 2012 6:08 PM
    Moderator
  • A custom folder which I created under Windows.Storage.ApplicationData.current.localFolder earlier.
    Monday, March 26, 2012 10:59 PM
  • The issue is that the default CommonFileQuery is shallow. Also, in the above you cannot pass in query to getFilesAsync. You have two ways to accomplish this:

    1.

    var options = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.orderByName, ["*"])
    varquery = sourceDir.createFileQueryWithOptions(options);

    query.getFilesAsync().then(function (files) {

    2.

    sourceDir.getFilesAsync(Windows.Storage.Search.CommonFileQuery.orderByName).then(function (files) {

    Monday, March 26, 2012 11:55 PM
  • Thank you very much. That does work.

    The documentation should be updated as it clearly states 'Gets a list of all files in the current folder and its sub-folders.' which obviously isn't true with the default option. I find it a bit strange that I have to say orderByName just to get a deep list of files but oh well. It works.

    Tuesday, March 27, 2012 12:06 AM
  • We've already noticed that documentation issue. Stay tuned as we fine-tune these documents. 

    For reference, when you are using commonFileQueries, you can utilize the documentation here to learn about varying behavior:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.search.commonfilequery.aspx

    As FYI, the following would also work:

    var options = new Windows.Storage.Search.QueryOptions();
    options.folderDepth = Windows.Storage.Search.FolderDepth.deep;
    var query = sourceDir.createFileQueryWithOptions(options);
    query.getFilesAsync().then(function (files) {

    Thanks,

    Justin Cooperman

    Tuesday, March 27, 2012 12:24 AM