Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to tell if folder or file

Status
Not open for further replies.

nbgoku

Programmer
May 25, 2004
108
US
is there anyway to tell if something is a folder or a file? b/c i noticed files can have no extensions and still be considered files, and folders can have periods in them and still be considered folders (even though they may look like files names)
is there a certain property or function that can detect if a file or rolder?
 
Code:
#include <windows.h>
static const DWORD bad_attr = 0xFFFFFFFF;

bool isFolder(const char* path)
{
  DWORD attr;
  return path 
  && (attr=GetFileAttributes(path)) != bad_attr
  &&  (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
}

bool isFile(const char* path)
{
  DWORD attr;
  return path
  && (attr=GetFileAttributes(path)) != bad_attr
  &&  (attr & FILE_ATTRIBUTE_DIRECTORY) == 0;
}
 
Use the stat() command to be portable on Windows & UNIX, then check the st_mode attribute that is returned for S_IFDIR.
 
ok cpjust i tried this but doesnt work, any ideas?

Code:
	struct _stat buf;
	int r = _stat("C:\\",&buf);
	if(buf.st_mode==_S_IFDIR)
	{
		MessageBox("c:\ exist");
	}
	else
	{
		MessageBox("c:\ not exist");
	}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top