Date: 2022apr18
OS: Windows
Product: MSVC
Language: C/C++
Keywords: check, directory, dir, folder
Q. Windows: S_ISREG() and S_ISDIR()
Q. If you're writing new code its best to use GetFileAttributes()
http://www.davekb.com/search.php?target=GetFileAttributes
But if you are compiling Unix/Linux code on Windows:
#include <sys/stat.h>
#ifndef _S_ISTYPE
#define _S_ISTYPE(mode, mask) (((mode) & _S_IFMT) == (mask))
#define S_ISREG(mode) _S_ISTYPE((mode), _S_IFREG)
#define S_ISDIR(mode) _S_ISTYPE((mode), _S_IFDIR)
#endif
ExampleUse() {
struct sb sb;
char *pathname = "c:/windows";
if (stat(pathname, &sb) == -1) {
// It does not exist (or other less common problem)
return;
}
if (S_ISREG(sb.st_mode)) {
// Its a regular file
}
if (S_ISDIR(sb.st_mode)) {
// Its a directory
}
}