Home > OS >  Update File Modification Time
Update File Modification Time

Time:03-21

I'm trying to update the file modification metadata of a file using Rust ( a language to which I am very new )

I can access the Metadata as shown here;

https://doc.rust-lang.org/stable/std/fs/struct.Metadata.html#method.modified

 use std::fs;

fn main() -> std::io::Result<()> {
    let metadata = fs::metadata("foo.txt")?;

    if let Ok(time) = metadata.modified() {
        println!("{:?}", time);
    } else {
        println!("Not supported on this platform");
    }
    Ok(())
}

I don't know how to alter that value though. My instinct was to open existing files in append mode and write an empty string--didn't work.

Wondering what a general approach for this would look like?

CodePudding user response:

You will need a external crate: filetime

CodePudding user response:

The set_file_mtime function from the filetime crate can update the file modification time metadata:

use filetime::{set_file_mtime, FileTime};

fn main() {
    set_file_mtime("foo.txt", FileTime::now()).unwrap();
}
  • Related