Dir Tab
Sean‘s pick this week is dirtab(in) by Julian.
Do you ever need to scrape the files in a directory and figure out how to parse them? If you have, you’ve probably come across the dir command. While dir works, it’s frustrating because you get back an array of structs, that, in releases since R2013b, would be better served by a table. I’ve found that I now pretty much just always call struct2table on the output. See Loren’s Blog Post on this topic.
This is what Julian’s file does, but he goes one step further and converts the date to a datetime rather than date number and removes those pesky “.” and “..”s from every listed directory. Another bit of extra work saved.
dirtab(pwd)
ans = 4×7 table folder name ext date bytes isdir datenum __________________________________ ________________ ______ ____________________ _____ _____ __________ 'C:\Documents\MATLAB\potw\dirtab\' 'dirtab' '.m' 23-Apr-2018 15:35:22 3776 false 7.3717e+05 'C:\Documents\MATLAB\potw\dirtab\' 'license' '.txt' 23-Apr-2018 15:57:12 1306 false 7.3717e+05 'C:\Documents\MATLAB\potw\dirtab\' 'mainDirTab' '.m' 23-Apr-2018 17:32:53 2154 false 7.3717e+05 'C:\Documents\MATLAB\potw\dirtab\' 'wordpress_html' '' 23-Apr-2018 17:33:13 0 true 7.3717e+05
However, since 18a came out, fullfile now works with strings (my favorite 18a feature!); I’ve typically gone a step further and converted the file parts to strings and added the full file path for easy opening.
D = dirtab(pwd); SD = varfun(@convertCharsToStrings, D); SD.Properties.VariableNames = D.Properties.VariableNames; SD.fullfilepath = fullfile(SD.folder, SD.name + SD.ext); SD.datenum = []
SD = 4×7 table folder name ext date bytes isdir fullfilepath __________________________________ ________________ ______ ____________________ _____ _____ ________________________________________________ "C:\Documents\MATLAB\potw\dirtab\" "dirtab" ".m" 23-Apr-2018 15:35:22 3776 false "C:\Documents\MATLAB\potw\dirtab\dirtab.m" "C:\Documents\MATLAB\potw\dirtab\" "license" ".txt" 23-Apr-2018 15:57:12 1306 false "C:\Documents\MATLAB\potw\dirtab\license.txt" "C:\Documents\MATLAB\potw\dirtab\" "mainDirTab" ".m" 23-Apr-2018 17:32:53 2154 false "C:\Documents\MATLAB\potw\dirtab\mainDirTab.m" "C:\Documents\MATLAB\potw\dirtab\" "wordpress_html" "" 23-Apr-2018 17:33:13 0 true "C:\Documents\MATLAB\potw\dirtab\wordpress_html"
Comments
Do you have other requirements you’d like to see in dir?
Give it a try and let us know what you think here or leave a comment for Julian.
Published with MATLAB® R2018a
- 범주:
- Picks
댓글
댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.