Getting a List of Files.... Should Be Easy But
166 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Matt O'Brien
am 30 Aug. 2022
Kommentiert: Matt O'Brien
am 14 Sep. 2022
I have a GUI app 99% complete, but have spent several days trying to resolve what, to me, should be an extremely simple task.
Namely,
I need to get a list of all files and sub files from a given foldername down (incl all subfolders).
The list should exclude;
- Directories.
- System Files
- Hidden Files
- Files which start with a full stop (ie Mac hidden files)
- Files which have ".Spotlight-V100" as part of the folder path.
- Files which have ".Trashes" as part of the folder path.
Questions.
- Is there a MatLab command to do this ….. ????
- Or is there an elegant routine to do this.
- Or is there a Matlab plug-in which will do this.
I have created a matlab script to do this on Windows, but it is approx 90 lines of code. I hate to think what I might have to do to get this working on a Mac as well.
I will be using this regularly, so wish to make this as elegant/efficient as possible.
4 Kommentare
Akzeptierte Antwort
Matt O'Brien
am 14 Sep. 2022
Bearbeitet: Matt O'Brien
am 14 Sep. 2022
4 Kommentare
dpb
am 14 Sep. 2022
Bearbeitet: dpb
am 14 Sep. 2022
Oh,yeah...forgot about stats being an array when putting together...just put the [] you had back that I left out to assimilate back into vectors...
...
full_filenames = fullfile({dinfo.folder}, {dinfo.name}).'; % convert to column for pretty
[~,stats] = cellfun(@fileattrib, full_filenames);
dinfo=dinfo(~([stats.hidden]|[stats.system]|[stats.directory]));
...
Weitere Antworten (9)
dpb
am 30 Aug. 2022
Verschoben: dpb
am 30 Aug. 2022
- Directly as one command, no.
- Probably not extant, no.
- Certainly not specifically, no.
I "know nuthink!" of Mac OS ls equivalent, but doesn't really seem as though it should be particularly difficult -- certainly can't see why it would take some 90 lines of code.
rootpath='YourRootPath';
[~,d]=system(['dir /A:-H-D-S /S /B ' rootpath] );
d=string(split(d,newline));
d=d(strlength(d)>0);
will give you a list of all files that are not hidden/directories/system files for the rootpath folder and all subfolders in a list on Windows using the default CMD command shell. Mac surely has something equivalent.
A few well-chosen filters against the not-wanted list of this list should be pretty slimple to code; a regexp guru there might be of some help; that wouldn't be me, however... :)
I've always wondered why/wished for that TMW would have just supported the basic OS command line switches for the native OS in its incarnation of dir -- having it neutered as it is to "plain vanilla" is a real pain.
ADDENDUM:
Thinking about the exclude list, led me to thinking it's not that hard, either...with the caveat you have had the discipline to not name a file with the excluded path name in a directory not in the excluded list.
excludeList=[".Spotlight-V100"; ".Trashes"]; % filename content to exclude
d=d(~contains(d,excludeList)); % get rid of 'em...
I guess even that part above could be handled if used
d=d(~contains(fileparts(d),excludeList)); % exclude unwanted folders only
I dunno how you would handle @Walter Roberson's comment re: Mac and OS files -- although I'd hope you aren't putting your data where the OS stores its files so it wouldn't be an issue, anyway.
2 Kommentare
Walter Roberson
am 30 Aug. 2022
ls('-ld', tempdir)
You can see that on MacOs and Linux, basic command line switches for MATLAB ls() are supported.
dpb
am 30 Aug. 2022
Yeah, but not for Winwoes -- nor does dir for either which was my specific complaint.
Walter Roberson
am 30 Aug. 2022
query_folder = tempdir; %set as appropriate, tempdir is just used for example purposes
dinfo = dir( fullfile(query_folder, '**', '*') );
dinfo([dinfo.isdir]) = []; %exclude directories
dinfo( startsWith({dinfo.name}, '.') ) = []; %exclude hidden files, which is same as . files on MacOS
dinfo( contains({dinfo.folder}, {'.Spotlight-V100', '.Trashes'}) ) = []; %exclude those particular directories
Unless, that is, when you refer to "hidden files", you refer to things such as ~/Library . If so then you would need to use ls -@ to query looking for the extended attribute com.apple.FinderInfo 32 or system() out to xattr looking for com.apple.FinderInfo
Well, except for the fact that if you add a color tag to a file then the com.apple.FinderInfo attribute with value 32 gets added to the file, and com.apple.metadata:_kMDItemUserTags gets added as well. If you then remove the color from the file, then com.apple.FinderInfo gets removed but a com.apple.metadata:_kMDItemUserTags attribute gets left behind. So to determine whether a file is hidden you need to look for com.apple.FinderInfo is present with value 32 but com.apple.metadata:_kMDItemUserTags is not present...
2 Kommentare
Matt O'Brien
am 30 Aug. 2022
11 Kommentare
dpb
am 31 Aug. 2022
I've not delved into how to control it (although I may have done and just forgotten it) but when "bang" to OS under MATLAB here on Win10, it still uses CMD.EXE. system is builtin so can't see what it actually does, I presume, however, it uses a start command to spawn a new CMD.EXE process, passing it the rest of the command as parameters.
I deduce that because personally on Windows I use the JPSoftware replacement command processor instead of the MS-supplied CMD and even if Windows is configured to use it as default, MATLAB still use CMD.EXE, not the system default. I've also thought that very rude of MATHWORKS to have done and not use the system default so the user could have their toolsets at hand if wish. With the TakeCommand processor from JPSoft, one could add in the various exclusions into its enhanced DIR and do virtually all the culling before returning the list. That, however, doesn't help for Mac not those who don't use it, of course.
But, looks as though you've basically got the problem solved with Walter's esteemed help so I'll retire from the field here unless you have something else specific along this line you care to pursue.
Good luck!!!
Matt O'Brien
am 31 Aug. 2022
2 Kommentare
Walter Roberson
am 31 Aug. 2022
We already know the names cannot be folders, so there is no point testing that.
The below code will work on MacOS and Linux as well -- those will return NaN for the hidden and system attributes, but but specifically testing == 1 then both NaN and 0 are treated as false, so NaN does not need to be special cased.
No loop is needed.
tmpFullNames = fullfile( {MyFileList(iFile).folder}, {MyFileList(iFile).name});
[~,stats] = fileattrib(tmpFullNames);
isBadFile(iFile) = [stats.hidden]==1 | [stats.system]==1;
myFileList(isBadFile) = [];
Matt O'Brien
am 31 Aug. 2022
Bearbeitet: Matt O'Brien
am 5 Sep. 2022
11 Kommentare
dpb
am 1 Sep. 2022
Interesting. I've never been "bit" by the camera bug -- I've bought several with the intent over the years, but they all end up just sitting on the shelf gathering dust, so I've never poked around with any.
I do think this thread is another "shot across the bow" that TMW should strengthen the builtin functionality of dir() to support the underlying OS switches.
I've not tried the dir route via expressly "banging" to CMD.EXE with the command string to avoid powershell -- going that route has the benefit of return the FQNs as a list without having to construct them from the struct returned by dir() as well as the various attribute screening done first instead of later.
Anyways, looks as though you've basically got it sorted -- the other vendor idiosyncracies are likely just going to be additional specific strings to add to the exclusion list. Unfortunately, one can imagine that may continue to grow as new models/features are introduced...
Matt O'Brien
am 12 Sep. 2022
11 Kommentare
Walter Roberson
am 13 Sep. 2022
dinfo = dir('C:\**\*');
full_filenames = fullfile({dinfo.folder}, {dinfo.name});
%filtering
[~,stats] = cellfun(@fileattrib, full_filenames);
is_unwanted = [stats.hidden]==1 | [stats.system]==1;
dinfo(is_unwanted) = [];
full_filenames(is_unwanted) = [];
(On Mac, fileattrib is happy to work on the cell array when I test)
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!