locked
get folder(s) in install location, and then enumerate the content

    Question

  • Hi,

    I've included a number of folders and files to be distributed with my windows store apps. For example,

    MyApp contains myData folder. myData contains a number of folder and each folder contains a number of files.

    Based on the documentation, I've managed to get the install location for my app. The next thing is I want to go into myData folder, go into each folder and retrieve all file name and finally store it in a vector.

    Thanks.

    Note: The "FolderEnumeration" sample (scenario2) did use the same process but it's using a 'built-in' folder ie: Pictures. It's quite hard for me to apply/modify the sample code in my app since I'm still learning C++ and having difficult time to understand asynchronous programming.

    Wednesday, April 10, 2013 2:16 AM

Answers

  • Hi A,

    You really need to understand tasks and recursion.  Please read up more on them or you may find yourself in further trouble.  The best resources are here:

    Asynchronous programming (Windows Store apps)

    Asynchronous programming in C++ (Windows Store apps)

    Task Parallelism (Concurrency Runtime)

     

    Please study this code as it well documented:

    task<void> Scenario1::GetFolders(StorageFolder ^theFolder, String ^previousName)
    {
    	//Build the folder name from the previous and current folder name and pass to the next task
    	previousName += "\\" + theFolder->Name;
    
    	//Create a task that gets all the files and pass the previousName and this in
    	return create_task(theFolder->GetFilesAsync())
    		.then([this, previousName](IVectorView<StorageFile^>^ files)
    		{
    			//We now have a list of files in the folder...
    			String^ outputText = ref new String();
    			//Output the name of the folder..
    			outputText = "Folder: " + previousName + "\n";
    			//Iterate over the files (this is a task as well) and output the name
    			std::for_each(begin(files), end(files), [this, &outputText](StorageFile^ file)
    			{
    				outputText += "    File: " + file->Name + "\n";
    			});
    
    			//Output the results (this is the UI thread since we are using tasks)
    			OutputTextBlock->Text += outputText;
    		})
    		.then( [this,theFolder,previousName] (task<void> t )  //We are done with the files now see if this folder has any more folders...
    		{
    			//get this folder's folders
    			create_task(theFolder->GetFoldersAsync())
    			.then([this,previousName](IVectorView<StorageFolder^>^ folders)
    			{
    				//If we have any folders, recursively call ourself in a new task!
    				std::for_each(begin(folders), end(folders), [this,previousName](StorageFolder^ folder)
    				{
    					//Recurse for each folder we found and start again!
    					create_task(GetFolders(folder, previousName));
    				});
    			});
    	});
    }
    

    If you don't understand why or what the code is doing, go back again to the references I have posted to really learn this and get comfortable with the task paradigm.

     

    -Jeff


    Jeff Sanders (MSFT)

    Friday, April 12, 2013 2:14 PM
    Moderator

All replies

  • Hi A,

    Check out this sample:

    http://code.msdn.microsoft.com/windowsapps/Package-sample-46e239fa/sourcecode?fileId=43882&pathId=1276694036

    Scenario 2 shows you how to get the install location folder.  You would do this and get the folder where your assets are located.

    Happy Coding!

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, April 10, 2013 2:22 PM
    Moderator
  • Hi A,

    Check out this sample:

    http://code.msdn.microsoft.com/windowsapps/Package-sample-46e239fa/sourcecode?fileId=43882&pathId=1276694036

    Scenario 2 shows you how to get the install location folder.  You would do this and get the folder where your assets are located.

    Happy Coding!

    -Jeff


    Jeff Sanders (MSFT)

    Hi Jeff,

    Thanks for the reply. Unfortunately, i'm ok with getting the install location folder. The problem is to get the subfolder.

     

    Based on scenario2 FolderEnumeration sample, I've modified the codes to suit my requirements.

    Package^ package = Package::Current; 
    Windows::Storage::StorageFolder^ installedLocation = package->InstalledLocation;
    String^ comp = "components";
    
    create_task(installedLocation->GetFolderAsync(comp)).then([this](StorageFolder^ compFolder)
    {
        QueryOptions^ queryOptions = ref new QueryOptions(CommonFolderQuery::DefaultQuery);
        StorageFolderQueryResult^ queryResult = compFolder->CreateFolderQueryWithOptions(queryOptions);
    
        create_task(queryResult->GetFolderAsync()).then([this](IVectorView<StorageFolder^>^ folders)    
        {
          This part is using the same code from scenario2 FolderEnumeration sample.   
         });
    });

    MyApp->MyData->MyData1->MyData1_A, MyData1_B

                             ->MyData2->MyData2_A, MyData2_B   

    When I ran this code, it will only return file name from MyData1 and MyData2 folder. It does not go into MyData1 and MyData2 subfolder. But the sample code will go into each folder and retrieve every file name.

    Note: I'm using the DefaultQuery instead of GroupByMonth.

    BTW, the first create_task in the code above, is it the right way to get a specific subfolder in install folder?

    Thanks.



    • Edited by azmas Thursday, April 11, 2013 1:53 AM
    Thursday, April 11, 2013 1:51 AM
  • Maybe I've make a mistake. It seems that scenario2 of FolderEnumeration sample must make use of any one of the member from CommonFolderQuery class. I think I've no need to use that class.

    So what's the best way to enumerate a folder (which contains multiple folder and a large number of files in those folder) and get every file name?

    Thanks.

    Thursday, April 11, 2013 2:13 AM
  • Hi A,

    You really need to understand tasks and recursion.  Please read up more on them or you may find yourself in further trouble.  The best resources are here:

    Asynchronous programming (Windows Store apps)

    Asynchronous programming in C++ (Windows Store apps)

    Task Parallelism (Concurrency Runtime)

     

    Please study this code as it well documented:

    task<void> Scenario1::GetFolders(StorageFolder ^theFolder, String ^previousName)
    {
    	//Build the folder name from the previous and current folder name and pass to the next task
    	previousName += "\\" + theFolder->Name;
    
    	//Create a task that gets all the files and pass the previousName and this in
    	return create_task(theFolder->GetFilesAsync())
    		.then([this, previousName](IVectorView<StorageFile^>^ files)
    		{
    			//We now have a list of files in the folder...
    			String^ outputText = ref new String();
    			//Output the name of the folder..
    			outputText = "Folder: " + previousName + "\n";
    			//Iterate over the files (this is a task as well) and output the name
    			std::for_each(begin(files), end(files), [this, &outputText](StorageFile^ file)
    			{
    				outputText += "    File: " + file->Name + "\n";
    			});
    
    			//Output the results (this is the UI thread since we are using tasks)
    			OutputTextBlock->Text += outputText;
    		})
    		.then( [this,theFolder,previousName] (task<void> t )  //We are done with the files now see if this folder has any more folders...
    		{
    			//get this folder's folders
    			create_task(theFolder->GetFoldersAsync())
    			.then([this,previousName](IVectorView<StorageFolder^>^ folders)
    			{
    				//If we have any folders, recursively call ourself in a new task!
    				std::for_each(begin(folders), end(folders), [this,previousName](StorageFolder^ folder)
    				{
    					//Recurse for each folder we found and start again!
    					create_task(GetFolders(folder, previousName));
    				});
    			});
    	});
    }
    

    If you don't understand why or what the code is doing, go back again to the references I have posted to really learn this and get comfortable with the task paradigm.

     

    -Jeff


    Jeff Sanders (MSFT)

    Friday, April 12, 2013 2:14 PM
    Moderator