Schultz’s PowerBuilder Notes

Using the PFC’s n_cst_filesrv


The PFC’s contains handy services to work with files saved on your PC or network.  I recently had a need to collect all the files contained in particular folder and any of its subfolders.  The result is this recursive function wf_GetFileNames().

The Path and file name for each file is inserted into an datawindow with an external data source. 

//////////////////////////////////////////////////////////////////////////////
//
//   Function: wf_GetFileNames
//
//   Access: Protected
//
//   Description: For the passed folder, get all of its files.  If the folder
//              contains a subfolder, call this function recursively with the
//              new folder name
//
//   Arguments: string as_path
//
//   Returns:   integer
//           
//////////////////////////////////////////////////////////////////////////////
//   Revision History
//
// Date      By   Task    Description/Comments
// --------  ---  ------  -------------------------------------------
// 05/12/09  fjs          Initial Version
//
//////////////////////////////////////////////////////////////////////////////

int                li_usr, li_rc, li_pos
n_cst_filesrvwin32 lnv_file
Integer            li_indx, li_file_cnt, li_insert
String             ls_file_name, ls_path
n_cst_dirattrib    lnv_filelist[], lnv_folderlist[]

//Overhead
if NOT IsValid(lnv_file) then
   lnv_file = CREATE n_cst_filesrvwin32
end if

// Get all the files in this subfolder and write to dw
ls_path     = as_path + "\*.*"
li_file_cnt = lnv_file.of_DirList(ls_path, 0, lnv_filelist)
li_indx = 0
do while li_indx < li_file_cnt
   li_indx ++
   ls_file_name = lnv_filelist[li_indx].is_filename
   li_insert = dw_1.InsertRow(0)
   dw_1.SetItem(li_insert, "path", as_path)
   dw_1.SetItem(li_insert, "file", ls_file_name)
loop

// Recursive call if this contains any subidirectories
li_file_cnt = lnv_file.of_DirList(ls_path, 16, lnv_folderlist)
li_indx = 0
do while li_indx < li_file_cnt
   li_indx++
   ls_file_name = lnv_folderlist[li_indx].is_filename
   if wf_IsSubFolder(ls_file_name) then
      ls_path = as_path + "\" + ls_file_name
      wf_GetFileNames(ls_path)
   end if
loop

// Clean up
if IsValid(lnv_file) then
   DESTROY lnv_file
end if

Return 1

wf_GetFileNames calls the following function to determine if the filename is a subfolder.  A subfolder is surrounded by “[]” brackets and is not all periods:

//////////////////////////////////////////////////////////////////////////////
//
//   Function: wf_IsSubFolder
//
//   Access: Protected
//
//   Description: is the passed file name a subfolder?
//
//   Arguments: string as_file_nm
//
//   Returns:   Boolean
//           
//////////////////////////////////////////////////////////////////////////////
//   Revision History
//
// Date      By   Task    Description/Comments
// --------  ---  ------  -------------------------------------------
// 05/12/09  fjs          Initial Version
//
//////////////////////////////////////////////////////////////////////////////
 

boolean    lb_folder = TRUE
integer    li_pos
n_cst_string lnv_string
string       ls_file

as_file_nm = Trim(as_file_nm)

if lb_folder then
   li_pos = Pos(as_file_nm, "[")
   if li_pos = 1 then
      as_file_nm = Mid(as_file_nm, 2)
   else
      lb_folder = FALSE
   end if
end if

if lb_folder then
   li_pos = Pos(as_file_nm, "]")
      if li_pos > 1 then
         as_file_nm = Mid(as_file_nm, 1, Len(as_file_nm) - 1)
      else
         lb_folder = FALSE
   end if
end if

if lb_folder then
   ls_file = lnv_string.of_GlobalReplace(as_file_nm, ".", "")
   if Len(ls_file) = 0 then
      lb_folder = FALSE
   end if
end if

Return lb_folder
About these ads

May 13, 2009 - Posted by | 3. PFC, Utility Services | , , , , ,

1 Comment »

  1. Thanks for the code.

    One minor point…
    Use Longs instead of integers for insert row numbers and file counts as the PB max for integer (32767) can easily be exceeded.

    Comment by Matt | July 28, 2009 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: