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
1 Comment »
Leave a Reply
-
Archives
- August 2009 (2)
- May 2009 (1)
- March 2009 (1)
- January 2009 (2)
- December 2008 (1)
- November 2008 (2)
- September 2008 (1)
- August 2008 (1)
- July 2008 (24)
- June 2008 (22)
- May 2008 (69)
- April 2008 (25)
-
Categories
-
RSS
Entries RSS
Comments RSS
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.