forum-msg-id-133519-edit

Original date:2019-01-24 14:11:54 Edited by: irv Subject: Re: First letter of dir() result

SDPringle said...
irv said...

If you wish to build a filter which can do this, plus is easily expandable to do other things:

include std/filesys.e 
include std/console.e 
include std/sequence.e 
 
object files = dir("/home/irv") 
 
files = vslice(filter(files,routine_id("filefilter")),D_NAME) 
 -- vslice gives us just the names 
 
display(files) 
 
function filefilter(object x, object y)  
if equal(x[D_NAME][1],'.')  -- remove hidden files; 
or equal(x[D_NAME],".")     -- remove dot files; 
or equal(x[D_NAME],"..") 
or equal(x[D_ATTRIBUTES],"d") then return 0 -- remove folders; 
end if 
return 1 
end function 

Be careful if you want to test for 'd' in attributes. The equal test above will leave your routine processing hidden or read only directories. What you really should do for this attribute is use find :

find('d',x[D_ATTRIBUTES]) 

This is where it gets tricky. Hidden files and directories on Linux always begin with a dot. So those will be filtered out. Windows, maybe not. And the only attribute for Linux is 'd', or nothing, so either will work. The following seems to work, and is simpler, but Linux only:

------------------------------------------ 
function filefilter(object x, object junk)  
------------------------------------------ 
return file_type(x[D_NAME]) = 1 and x[D_NAME][1] != '.' 
end function 
Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu