In our scripts, we often ask a user to give diretory or path. That path may contain files that need to be processed and results may need to be written to that path. So a check is needed to verify that the path exists and that the user has write access to that path. In other cases, we might need to check only read access, but the idea is the same.
So I have used a while loop to see if the given path exists. If it doesn't, the user is asked a question again. I have another while loop to check for write access. Here is where I run into problem. I tried using a nested while loop to evaluate both conditions, but if the user ends a valid path that they do not have write access to, the program runs in and endless loop. I tried separating both checks, but I get the same problem. The only solution that I have come up with is to ask the user to check the permissions and run the script if the write access check fails (hence the return command near the end of the code). Ideally, I'd like the original question to be asked again (give a valid path with write access) rather than halting the program all together. Is there anyway to do that?
I have pasted sample code below as proof of concept. I realize that it is probably ideal to have input checks in a function, but I'd like to see if my problem can be fixed first.
dir=input('Please enter a directory or path:','s');
if exist (dir, 'dir')
disp('Congrats your directory exists');
disp('That directory or path does not exist. Please try again');
disp('Congrats. You have write access to the directory');
disp('Now that we have verified that the diretory exits and you have write access, press any key to contine');
disp('You do not have write access. Please pick a directory where you have write access and run this script again.');
disp('Good Job. That directory exits and you can write files to there.');