Discussions related to Visual Prolog
User avatar
abdelrahman
Posts: 13
Joined: 13 Sep 2009 6:23

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

Unread post by abdelrahman »

i want to obtain all files in a director collected in string list
["file1", "file2","file3","etc"]
Harrison Pratt
VIP Member
Posts: 356
Joined: 5 Nov 2000 0:01

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

Unread post 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.
User avatar
abdelrahman
Posts: 13
Joined: 13 Sep 2009 6:23

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

Unread post 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
User avatar
abdelrahman
Posts: 13
Joined: 13 Sep 2009 6:23

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

Unread post by abdelrahman »

also getfile Returns a string value not List Value
Harrison Pratt
VIP Member
Posts: 356
Joined: 5 Nov 2000 0:01

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

Unread post 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,         !.
User avatar
abdelrahman
Posts: 13
Joined: 13 Sep 2009 6:23

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

Unread post by abdelrahman »

Thank you very much ,

List Apprehension worked so well ,
so grateful,
Harrison Pratt
VIP Member
Posts: 356
Joined: 5 Nov 2000 0:01

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

Unread post by Harrison Pratt »

I'm glad you apprehended comprehension. :-)
Post Reply