Page 1 of 1

how to collect File Names in a specific Directory to a string List

Posted: 26 Nov 2021 20:31
by abdelrahman
i want to obtain all files in a director collected in string list
["file1", "file2","file3","etc"]

Re: how to collect File Names in a specific Directory to a string List

Posted: 26 Nov 2021 20:38
by Harrison Pratt
The directory class has a number of other useful predicates in addition to this one:

Code: Select all

directory::getFilesInDirectory_nd/3->   getFilesInDirectory_nd : (     string Directory,     string WildCard,     boolean IncludeSystem)     -> string FullFilename     nondeterm.

Re: how to collect File Names in a specific Directory to a string List

Posted: 26 Nov 2021 20:51
by abdelrahman
i don't want getfile Dialog to appear and select from it
i want to obain list automatically without displaying any dialog
get_dir_FileList (Directory,FilesList ):-

i don't want any dialog to appear to user

Re: how to collect File Names in a specific Directory to a string List

Posted: 26 Nov 2021 20:54
by abdelrahman
also getfile Returns a string value not List Value

Re: how to collect File Names in a specific Directory to a string List

Posted: 26 Nov 2021 22:50
by Harrison Pratt
Try the code below in a console application.

The predicate getFilesInDirectory_nd/3 returns the files in the directory nondeterministically, so you need to collect them for use somehow. For your purpose I assume you want to collect them all in a list, so you can use list comprehension to collect them like this:

Code: Select all

    run() :-         mainExe::getFilename(AppDir, _AppNameExt),         Files = [ F || F = directory::getFilesInDirectory_nd(AppDir, "*.*", false) ],         nothing(Files),         !.
If you want to do something with the files stepwise and don't want to collect them in a list you can do something like this:

Code: Select all

    run() :-         mainExe::getFilename(AppDir, _AppNameExt),         foreach F = directory::getFilesInDirectory_nd(AppDir, "*.*", false) do             stdio::write("\n", F)         end foreach,         !.

Re: how to collect File Names in a specific Directory to a string List

Posted: 27 Nov 2021 12:00
by abdelrahman
Thank you very much ,

List Apprehension worked so well ,
so grateful,

Re: how to collect File Names in a specific Directory to a string List

Posted: 27 Nov 2021 12:48
by Harrison Pratt
I'm glad you apprehended comprehension. :-)