- 1 year ago
How to get extended file-attributes in Windows in Python/Go/C/Batch?
Comment
1 / 1
To get extended file attributes in Windows using different programming languages, here are some approaches:
Python:
You can use the win32file.GetFileAttributes
function from the pywin32
package to get the file attributes. Here's an example:
pythonimport win32file
file_path = 'path_to_file'
# Get the file attributes
file_attributes = win32file.GetFileAttributes(file_path)
# Check if a specific attribute is set
if file_attributes & win32file.FILE_ATTRIBUTE_HIDDEN:
print('File is hidden')
Go:
In Go, you can use the os.Stat
function to get the file information. Here's an example:
gopackage main
import (
"fmt"
"os"
)
func main() {
filePath := "path_to_file"
// Get file information
fileInfo, err := os.Stat(filePath)
if err != nil {
fmt.Println("Error:", err)
return
}
// Check if a specific attribute is set
if fileInfo.Mode()&os.ModeHidden != 0 {
fmt.Println("File is hidden")
}
}
C:
In C, you can use the GetFileAttributes
function from the Windows API to retrieve file attributes. Here's an example:
c#include <windows.h>
#include <stdio.h>
int main() {
const char* filePath = "path_to_file";
// Get file attributes
DWORD fileAttributes = GetFileAttributes(filePath);
if (fileAttributes == INVALID_FILE_ATTRIBUTES) {
printf("Error: %d\n", GetLastError());
return 1;
}
// Check if a specific attribute is set
if (fileAttributes & FILE_ATTRIBUTE_HIDDEN) {
printf("File is hidden\n");
}
return 0;
}
Batch:
In Batch scripting, you can use the attrib
command to retrieve file attributes. Here's an example:
batch@echo off
set "filePath=path_to_file"
for /f "tokens=2 delims= " %%a in ('attrib "%filePath%"') do (
if "%%a" EQU "H" (
echo File is hidden
)
)
These examples demonstrate how to check specific file attributes, such as hidden attribute. You can modify the code to retrieve and check other attributes as needed.