Using dir to access files on WebApp Server

6 views (last 30 days)
I am creating a WebApp that needs to reference a set of data files located on the WebApp Server itself. The dir() function is supposed to list out the contents of the target directory and it is working on my local PC, however, when compiled as a WebApp dir() is returning an empty structure. I have checked that it is pointing at the correct folder, and other commands like load() and save() are working using the same directory on the WebApp Server. The behaviour is consistent with giving dir() a path that does not exist, as even pointing dir() at an empty folder typically returns two entries called "." and "..".
% Tested on my local PC
folder_contents = struct2table(dir('C:\Empty Folder that Exists')); % Returns a 2x6 table
folder_contents = struct2table(dir('C:\Folder that does not Exist')); % Returns a 0x6 empty table
% While on the WebApp server
folder_contents = struct2table(dir('C:\Folder that Exists and has contents'));
isempty(folder_contents) % Returns true
To be clear, the folder and files within it I am trying to access are on the same Virtual Machine as the WebApp Server and the compiled .ctf file.
The files within this folder will have unknown names so I cannot hard-code them into the WebApp ahead of time.
I've looked at the Web App Limitations and Unsupported Functionality page and the only thing mentioned there is uigetdir(), which I am not using.
Is there a trick to using dir() within a WebApp?
  3 Comments
David
David on 25 Jul 2025
I've added the fix that did work as an answer below, but just to provide more context for anyone that finds this later:
I am using fullfile() to construct the path, as my other files are located in the same parent folder as the data-file folder. Something like this...
% App Parameters
base_folder = "C:/WebApp Config";
config_filename = "config.yaml";
data_foldername = "Data";
% Within button callback
config = load(fullfile(app.base_folder, app.config_filename);
folder_contents = struct2table(dir(fullfile(app.base_folder, app.data_foldername)));
I had also tried using \ and /, 'char' and "string", as well as with and without escape characters for every instance of \. The fix seemed to be with Windows permissions.
Fangjun Jiang
Fangjun Jiang on 25 Jul 2025
Yeah. The cause seems to be the permission of the file system.

Sign in to comment.

Accepted Answer

David
David on 25 Jul 2025
Although I am still not sure of the exact root cause, nor why dir() failed when other 'file-accessing' functions like load() and save() worked fine, I was able to find a working fix. From some of the WebApp Server documentation:
"By default, the server creates local accounts like MwWebAppServerR20XXx and MwWebAppWorkerR20XXx (where R20XXx represents the MATLAB release) for the server and worker services, respectively."
By logging into the Virtual Machine hosting the WebApp Server and explicitly adding these two local accounts to the permissions list for the folder in question and giving them the same access level as my personal user account, the WebApp is now able to access the contents of the folder with dir().

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!