1. First letter of dir() result
- Posted by alrobnett Jan 23, 2019
- 1077 views
The following code uses dir() to list the languages in a directory. Unfortunately, the list includes '.' and '..' .
atom language_name1 sequence language_files, language_name language_files = dir("/home/allen/Language/") language_name = language_files[language_nmbr][D_NAME] language_name1 = language_name[1] if language_name1 >= 'A' then
The 'if' statement is used to eliminate '.' and '..' from the displayed list. I first tried simply:
if language_files[language_nmbr][D_NAME] < 'A'
A series of error messages led me to the above code, which works but seems clutsy. Is there a better way?
2. Re: First letter of dir() result
- Posted by ghaberek (admin) Jan 23, 2019
- 1047 views
I'd say this is the best-practice approach:
object files = dir( path ) if atom( files ) then -- report error, exit end if for i = 1 to length( files ) do sequence file_name = files[i][D_NAME] if find( file_name, {".",".."} ) then -- skip current/parent directory continue end if end for
And if you want to skip hidden files, add this:
sequence file_attr = files[i][D_ATTRIBUTES] if file_name[1] = '.' or find( 'h', file_attr ) then -- skip hidden files continue end if
-Greg
3. Re: First letter of dir() result
- Posted by irv Jan 23, 2019
- 1101 views
To be honest, I'm lazy, and usually just sort the listing and discard the first 2:
include std/filesys.e include std/sort.e object files = sort(dir("demos")) files = files[3..$]
4. Re: First letter of dir() result
- Posted by irv Jan 23, 2019
- 1042 views
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
5. Re: First letter of dir() result
- Posted by petelomax Jan 23, 2019
- 1032 views
if language_files[language_nmbr][D_NAME] < 'A'
atoms are always less that sequences, and 'A' is an atom.
Logically, at least, if you had said <"A" (or >="A") instead, things might have worked out better.
However, another problem on OE (not on phix) is that you cannot use < (or >=) on non-equal length sequences, and when they are equal-length, you get a (nested) sequence of true/false which cannot be used in a if-test. Instead you must code:
if compare(language_files[language_nmbr][D_NAME],"A")<0 then
Pete
6. Re: First letter of dir() result
- Posted by SDPringle Jan 23, 2019
- 1020 views
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])
7. Re: First letter of dir() result
- Posted by irv Jan 24, 2019
- 1004 views
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 on Linux and Windows, and is simpler:
------------------------------------------ function filefilter(object x, object junk) ------------------------------------------ return file_type(x[D_NAME]) = 1 and x[D_NAME][1] != '.' and find('h',x[D_ATTRIBUTES]) = 0 end function