How I can search a .txt file in any directory(i.e. c:\,d:\ etc.) using file functions in PowerBuilder?
-
Are you looking to search a directory *for* *.txt files, or scan the *.txt files in a directory for specific content? One directory or subtrees? And what PB version and target type? – Terry Apr 23 '10 at 18:49
2 Answers
So, if all you're doing is looking for files, you can do this with a listbox.DirList(), or if you want to do this without being tied to a window or a control, you can call WinAPI functions to do this:
Function long FindFirstFileW (ref string filename, ref os_finddata findfiledata) library "KERNEL32.DLL" alias for "FindFirstFileW"
Function boolean FindNextFileW (long handle, ref os_finddata findfiledata) library "KERNEL32.DLL" alias for "FindNextFileW"
where os_finddata is defined as
unsignedlong ul_fileattributes
os_filedatetime str_creationtime
os_filedatetime str_lastaccesstime
os_filedatetime str_lastwritetime
unsignedlong ul_filesizehigh
unsignedlong ul_filesizelow
unsignedlong ul_reserved0
unsignedlong ul_reserved1
character ch_filename[260]
character ch_alternatefilename[14]
and os_filedatetime is defined as
unsignedlong ul_lowdatetime
unsignedlong ul_highdatetime
If you want examples of how to use these, look in PFC (PowerBuilder Foundation Classes, available at CodeXchange) at the object (pfcapsrv.pbl)pfc_n_cst_filesrvunicode.of_DirList (). (That's where these prototypes and the structures are copied from, BTW.)
Good luck,
Terry

- 6,160
- 17
- 16
You can use a ListBox
control to get a list of files/directories based on a given string pattern (*.txt, myfile.txt, .etc). Look at the DirList
function in the help. And here is an example from here showing how to use a ListBox control without putting it visually on a window.
string ls_files[]
window lw_1
listbox llb_1
int li_items, li_i
Open( lw_1 )
lw_1.openUserObject( llb_1 )
llb_1.DirList( sFileSpec, uFileType )
li_items = llb_1.TotalItems()
For li_i = 1 to li_items
ls_files[ li_i ] = llb_1.Text( li_i )
Next
lw_1.closeUserObject( llb_1 )
Close( lw_1 )

- 7,721
- 4
- 40
- 55
-
It is very annoying that you have to have a control on a window for the DirList to work. It adds unnecessary steps, especially when I'm working from an NVO. – Adam Hawkes Apr 27 '10 at 16:30
-
I believe it does not have to be visible. You just add the listbox in code and get rid of it. – Ham Dong Kyun Jul 23 '17 at 04:52