Get latest updated folder in a dir

Basically I have a folder /Versions with multiple folders inside. I need to get the latest updated folder by the OS and go to that one in my path. How would I do that?

First, you'd use std::fs::read_dir() to read the contents of the directory. The function returns an iterator of DirEntry objects. On each entry, you can call DirEntry::metadata() to get the metadata. Then, you can use Metadata::is_dir() to confirm that the entry is a directory (and skip it otherwise), and Metadata::modified() to get the modified time. Finally, once you have the (entry, modified) pairs, you can use Iterator::max_by_key() to find the entry with the latest modified time.

1 Like

The std::fs::metadata() function will give you metadata about a particular item on the file system. From there, you can use Metadata::modified() to get the last modified time.

1 Like

Would you be able give some code example I am still not understanding the logic for it. I'm normally working with JavaScript projects so this is a whole new experience to me :sweat_smile:

If you click on the links @LegionMammal978 has given, you'd find examples for each one of them.


The algorithm is pretty much language-independent. You'd have to ask for a list of files/subdirectories and check their last modified date in JavaScript, too. The task isn't harder or more complicated (or at all different, pretty much) in Rust, either.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.