# Doug's MATLAB Video Tutorials

## Find newest file in a directory

### 7 Responses to “Find newest file in a directory”

1. Kevin Sheppard replied on :
!dir /od *.m


at least on Windows

2. dhull replied on :

@Kevin,

Thank you, as with many of my videos the intent is more to take a simple example to show general techniques. This command does solve this specific problem very nicely though.

Thanks,
Doug

3. Vishu replied on :

Excellent. This was of a great help to me because i have to read files with stamp. However, one small correction. In line 5, replace the ‘~’ with some variable eg. a. What kevin wrote was right though i cudn get how to obtain the latest file name.

4. dhull replied on :

@Vishu,

In newer versions of MATLAB, the ~ means “ignore this outout/input”.

Doug

5. Dominik replied on :

Hi, Neat trick getting the max of datenum. I would like to find the newest folder in my current directory so that I can rename it.
I have a folder full of .zip files, I want to one at a time, rename the directory that it creates and then move on to the next .zip.
If you do after unzip it includes . and .. as directories that are newer than anything else. Since I don’t have an extension to separate with, how can I differentiate between ., .., and actual folders? Thanks

6. Dominik replied on :

Sorry, that didn’t come out as I wanted above:
Neat trick getting the max of datenum. I would like to find the newest folder in my current directory so that I can rename it.
I have a folder full of .zip files, I want to unzip one at a time, rename the directory that it creates and then move on to the next .zip.
If you do dir after unzip it includes . and .. (and sometimes .DS_Store which I think gets created after unzipping) as files/directories that are newer than anything else. Since I don’t have an extension to separate with, how can I differentiate between ., ..,.DS_Store and actual folders? Some code I have that is a bit of a hack and the last line doesn’t work because you can dynamically reference(?) inside the unix command:

files=dir;
for i=3:length(files)
unzip(files(i).name);
fd=dir;
for j=1:length(fd)
dn=[fd(4:end).datenum];
[~,nom,~]=fileparts(files(i).name);
unix(‘mv old nom’)
end
end

7. Dominik replied on :

I figured out my problem doing:
files=dir(‘*.zip’);
for i=3:length(files)
[~,nom,~]=fileparts(files(i).name);
unzip(files(i).name,nom);
fd=dir(nom);
cd(nom)
cd(fd(end).name)
unix(‘mv * ../’)
cd ../
rmdir(fd(end).name)
cd ../
end

but I would still be curious if there is a way to not capture the . and .. in the dir command. Or more broadly, dir everything except . which could also be applied to everything but .jpg etc. Thanks!

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 6 + 9 ?

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>


If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

These postings are the author's and don't necessarily represent the opinions of MathWorks.