Have dir() only accumulate files between certain dates

2 次查看(过去 30 天)
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