Comments on: Find newest file in a directory https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/?s_tid=feedtopost Stuart uses video to share his experiences solving problems with MATLAB day-to-day, interesting new features, plus tips and tricks he has picked up along the way. Mon, 11 Nov 2013 14:31:09 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Doug https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-29428 Mon, 11 Nov 2013 14:31:09 +0000 https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-29428 There is a date field that comes out of the command also. You would parse that. You might need to store the last run data in a file on disk somewhere.

]]>
By: Leslie https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-29055 Thu, 07 Nov 2013 13:46:29 +0000 https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-29055 How would this code change if I wanted to find only the newest files since the last run of this code?

]]>
By: Dominik https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-3082 Thu, 24 May 2012 23:14:37 +0000 https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-3082 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!

]]>
By: Dominik https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-3081 Thu, 24 May 2012 22:49:51 +0000 https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-3081 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];
[~,newestI]=max(dn);
[~,nom,~]=fileparts(files(i).name);
old=fd(newestI+3).name;
unix(‘mv old nom’)
end
end

]]>
By: Dominik https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-3080 Thu, 24 May 2012 22:12:53 +0000 https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-3080 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

]]>
By: dhull https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-2689 Wed, 29 Jun 2011 18:28:19 +0000 https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-2689 @Vishu,

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

Doug

]]>
By: Vishu https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-2682 Tue, 28 Jun 2011 11:28:10 +0000 https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-2682 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.

]]>
By: dhull https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-2089 Mon, 07 Jun 2010 17:36:49 +0000 https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-2089 @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

]]>
By: Kevin Sheppard https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-2083 Thu, 03 Jun 2010 20:59:30 +0000 https://blogs.mathworks.com/videos/2010/06/03/find-newest-file-in-a-directory/#comment-2083 !dir /od *.m

at least on Windows

]]>