List contents of a directory/folder in Powerbuilder.

Posted on 2004-10-14
Last Modified: 2013-12-26
Hi there.
I want a way to retrieve a list of all the files in a chosen directory or folder.

Pretty much any format will do, although something like an array of filenames and metadata such as dates would probably be the best.

The overall goal will be to generate a list of .sql files in \\server\folder\ and read the contents of each file so they can be executed against a database.

Using a Windows API call or whatever is fine.  I am using Windows 2000.

Thank you for your time.

Question by:akadruid
LVL 33

Accepted Solution

sajuks earned 500 total points
ID: 12306832
You can use the dirlist() function to get the directory into a listbox

Author Comment

ID: 12307269
That does what I want.

Not perfect, since it requires a visual object and bit of playing around, but good enough.

In case anyone else cares, this quick and dirty code (below) will read the first file name.  You can adjust it to loop through lb_1.TotalItems() to give you all the filenames.

uint li_filetype
string ls_file_pattern

ls_file_pattern = "\\server\share\"
lb_1.Reset ()
if lb_1.DirList ( ls_file_pattern, li_filetype ) then
      lb_1.SelectItem (1)
      //Messagebox ("First item is:", lb_1.SelectedItem ())
      Messagebox ("Can't read from:", ls_file_pattern)
end if

In all the years I've been working with pb, I have never seen that function!  Probably because I don't use listboxes normally.

Expert Comment

ID: 12335421
Dear Friend,
  Try out this, Ive written and used  for updating the application at the client system, by a single click of button, which has this following code.

Declare this API-Function in Application's Global Function Declaration Area

Function boolean CopyFile (string lpExistingFileName, ref string lpNewFileName, boolean b) Library "KERNEL32.DLL" Alias for "CopyFileA"

String  ls_source, ls_dll, ls_pbd, ls_app, ls_all, ls_destination
String ls_path
String  ls_complete_path

ls_pbd = '\\souce_system\spts_test\*.pbd'
ls_path = '\\source_system\spts_test\'
ls_destination = "C:\Spts\"

ddlb_dir_details.DirList(ls_pbd, 0)

If ddlb_dir_details.TotalItems() > 0 Then
      For ii_row = 1 to ddlb_dir_details.TotalItems()
       is_source_file_name = ddlb_dir_details.Text(ii_row)
       ls_destination = ls_destination + is_source_file_name
       is_source_file_name = ls_path + is_source_file_name
      MessageBox("Error","Copying Default Pbds Failure")
End IF

with the slight modification to this code, you can get the list of files to the list box.

   after getting the list of files, you can take the selected file and continue as
String ls_search
ls_search = "c:\abc.txt"
if FileExists(ls_search) then
    ls_file = ls_file_2
    string     ls_shortpath, ls_file_contents
    long     ll_max_length
    ll_max_length = 255
    ls_shortpath = space(ll_max_length)
    long     ll_length
//windows API to get the dos-name(short name) and path if you know the window name
    ll_length = GetShortPathName (ls_search, ls_shortpath, ll_max_length)
    Integer li_FileNum
//Check out to find the file exists before proceeding, you can use ls_search(window name) or
//ls_shortpath(Dos Name) as you need
    li_FileNum = FileOpen(ls_search,StreamMode!,Read!,LockWrite!)
//if exists aplly the wite lock to it
       IF li_FileNum > 0  THEN
           FileRead ( li_FileNum , ls_file_contents )
           FileClose (li_FileNum)
       End If
End IF

All the best,

Konka Kiran Kumar

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now