[Solved] list Files and Directory with DIR()

General FreeBASIC programming questions.
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: [Solved] list Files and Directory with DIR()

Post by exagonx »

fxm wrote:
coderJeff wrote:There is already a good example on the wiki page. Maybe another shorter one?

Code: Select all

#include once "dir.bi"

'' allow everything
var mask = fbDirectory or fbHidden or fbSystem or fbArchive or fbReadOnly

var attrib = 0
var f = dir( "*.*", mask, attrib )
while( f > "" )
	'' show any files that have directory attribute and don't
	'' care if it is system, hidden, read-only, or archive 
	if( attrib and fbDirectory ) then
		print f
	end if
	f = dir( attrib )
wend
Done.

See the DIR documentation page for all updates.
Thank you this is usefull to make something better than my hold code.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: [Solved] list Files and Directory with DIR()

Post by D.J.Peters »

Does fbSystem works on Linux also ?

/boot
/etc
/sbin
...

Joshy
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: [Solved] list Files and Directory with DIR()

Post by TeeEmCee »

On Unix (src/rtlib/unix/file_dir.c):

Code: Select all

	if( S_ISCHR( info->st_mode ) || S_ISBLK( info->st_mode ) || S_ISFIFO( info->st_mode ) || S_ISSOCK( info->st_mode ) )
		attrib |= 0x4;	/* system */

	if( S_ISDIR( info->st_mode ) )
		attrib |= 0x10;	/* directory */
	else
		attrib |= 0x20;	/* archive */
So only character and block devices (e.g. probably everything under /dev), fifos and sockets are 'system'. Directories like /boot never are 'system'.

And non-directories always have the archive bit set (even if not regular files).

The way DIR works is pretty annoying IMO but everyone has always had to work with it the way it works. A while back I switched to using POSIX readdir() on Unix to get more control over things like symlinks and error checking. I'm going to have to stop using DIR on Windows too, to get unicode filenames.
Post Reply