Read Windows hidden file attribute

Trying to find how to read the hidden attribute on a Windows file. Not having much luck atm.

Any guidance very much appreciated.

Getting an unresolved import error for:

use std::os::windows::prelude::*;

Not finding how to resolve it.

Are you compiling for Windows? It won't be available on Linux platforms. You can use conditional compilation to only use it when building for Windows targets. E.g.

#[cfg(windows)]
fn platform_specific_function() {
    // This will be compiled for windows only
}
#[cfg(unix)]
fn platform_specific_function() {
    // This will be compiled for *nix platforms.
}

Yeah, I'm working on Windows and compiling on Windows with the latest prod release of Rust. Working in VS Code if that matters with the Rust addons.

Weird. Could be something wonky about the addons? Personally I only use rust-analyzer so I don't know about others.

The following simple program compiles without error, right?

#[cfg(windows)]
use std::os::windows::prelude::*;

fn main() {}
1 Like

It works, looks to be a problem just in VS Code for me. Thanks for the help.

So here's the working solution for anyone who needs it.

use std::os::windows::prelude::*;

pub fn is_hidden(
                file_path: &std::path::PathBuf
            ) -> std::io::Result<bool>  
{
    let metadata = fs::metadata(file_path)?;
    let attributes = metadata.file_attributes();
    
    if (attributes & 0x2) > 0 {
        Ok(true)
    } else {
        Ok(false)
    }
}