About NotDirectoryError (Caught "std::exception" Exception message is:)

12 views (last 30 days)
Hello, I am using SPM8 with matlab. I have an error as below so that 'for' statement does not work.
Caught "std::exception" Exception message is: fl:filesystem:NotDirectoryError
Whenever I run the script, I also have "matlab syntax error cannot run as it appears" too.
I made simple scripts to remove unnecesarry files in each directory. There are subfolders named 's01', 's02' in the root directory. I wanted to remove unnecessary files whose name starts with 'main', 'p', 'ap', in each subfolders. I realized that this script always works for the "first" subfolders such as 's01', but it does not work for the "second (s02)" or ... subfolders. Could anyone advise me, please?
SN = {}; SN{end+1} = {'s01', 1:6}; SN{end+1} = {'s02', 1:6};
root_dir = '/home/data/PRES';
%% Deleting unnecessary files
for mm=1:size(SN, 2); aa=dir(fullfile(root_dir, SN{mm}{1}));
for nn=1:size(aa,1)
delete(fullfile(root_dir, SN{mm}{1}, aa(nn).name, 'main*'));
delete(fullfile(root_dir, SN{mm}{1}, aa(nn).name, 'p*'));
delete(fullfile(root_dir, SN{mm}{1}, aa(nn).name, 'ap*'));
end
end

Answers (1)

Cedric
Cedric on 28 Jan 2015
Edited: Cedric on 28 Jan 2015
In your calls to delete:
SN{mm}{1} is the folder name.
aa(nn).name is the file name.
so why are appending e.g. 'main*' ? You will get path/names like
/home/data/PRES/s01/main_abc.txt/main*
which is not what you want. You should either use the pattern 'main*' when you build the call to DIR, e.g. (untested)
SN = {{'s01', 1:6}, {'s02', 1:6}} ;
patterns = {'main*', 'p*', 'ap*'} ;
root_dir = '/home/data/PRES' ;
for dirId = 1 : numel( SN )
dirPath = fullfile( root_dir, SN{dirId}{1} ) ;
if ~exist( dirPath ), continue ; end
for patId = 1 : numel( patterns )
content = dir( fullfile( dirPath, patterns{patId} )) ;
for fileId = 3 : numel( content ) % 1 is '.', 2 is '..'
delete( content(fileId).name ) ;
end
end
end
or loop over all files and match relevant files according to patterns.
  2 Comments
Cedric
Cedric on 28 Jan 2015
You're welcome! Please note that I probably updated my answer while you were writing your comment, so take the latest version.

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!