List contents of a directory/folder in Powerbuilder.

Posted on 2004-10-14
Medium Priority
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 1500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

807 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