Have dir() only accumulate files between certain dates

조회 수: 2 (최근 30일)
John Gilmore
John Gilmore 2023년 11월 13일
답변: Walter Roberson 2023년 11월 13일
Is there a way to make the dir() function only accumulate files from between certain dates?
I have the following code,
S = dir(fullfile(SomePath,'**/*.xls'));
Which accumulates all the xls files in the "SomePath" directory. The SomePath directory contains upwards of 10,000 folders each with potentially 5 or 6 xls files in them. Needless to say, the command takes a significant amount of time to run, about 15 mintues.
I have this line in a function that looks for a specifc one of the xls files in the SomePath directory. Based on various inputs to this function I should know within a range of about 10 days of when the file should have been added to the SomePath directory.
Is there a way to make dir only look at that range of 10 days instead of all the folders and xls files in the SomePath directory?

채택된 답변

Walter Roberson
Walter Roberson 2023년 11월 13일
Is there a way to make the dir() function only accumulate files from between certain dates?
No, there is not.
I notice you used forward slash as your directory separator -- which is valid on all supported operating systems. But people who use Windows tend to use \ instead of / in their paths.
If you happen to be using MacOS or Linux, I suggest you consider system() of a find() https://ss64.com/osx/find.html call
start_days_ago = 23; %for example
days_window = 10; %within a range of about 10 days
start_days_ago = start_days_ago;
end_days_ago = (start_days_ago - days_window);
cmd = "find " + '"' + SomePath + '"' + " -depth -name \*.xls -ctime -" + start_days_ago + " -a -ctime +" + end_days_ago + " -print";
[status, msg] = system(cmd);
filenames = regexp(msg, '\r?\n', 'split');
filenames(cellfun(@isempty, filenames)) = []; %can happen especially at the end of list
Now filenames should be a cell array of character vectors of filenames qualified relative to whatever was in SomePath

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by