Le forum (ô combien francophone) des utilisateurs de Powerbuilder.
Pages: 1
Pour récupérer la liste des fichiers d'un dossier, plusieurs alternatives sont possibles selon l'environnement de l'application :
1) Utilisation d'un ListBox
PowerScript propose la fonction DirList qui remplit un ListBox :
listboxname.DirList ( filespec, filetype {, statictext } )
Populates a ListBox with a list of files. You can specify a path, a mask,
and a file type to restrict the set of files displayed. If the window has
an associated StaticText control, DirList can display the current drive
and directory as well.
...
Example : This statement populates the ListBox lb_emp with a list of
read/write files with the file extension TXT in the search
path C:\EMPLOYEE\*.TXT:
lb_emp.DirList("C:\EMPLOYEE\*.TXT", 0)
...
Ensuite une boucle sur les éléments du ListBox permet d'exploiter les résultats.
li_item_count = listboxname.TotalItems( ) For li_index = 1 To li_item_count listboxname.SelectItem( li_index ) ls_file = listboxname.SelectedItem( ) // traitements ... Next
Lorsque l'on souhaite récupérer d'autres informations sur les fichiers (taille, date, attributs, etc) ou éviter l'utilisation de contrôles graphiques (ex: déploiement de composants PB dans un serveur d'applications), on préférera l'une des deux méthodes suivantes :
2) Utilisation des PFC
Les PFC implémentent de nombreuses fonctionnalités utiles pour la manipulation de fichiers à travers l'objet pfc_n_cst_filesrv et ses descendants spécifiques à l'OS (pfc_n_cst_filesrvwin32, pfc_n_cst_filesrvaix...)
On y trouve notamment la méthode of_DirList() :
public function long of_dirlist (string as_filespec, long al_filetype, ref n_cst_dirattrib anv_dirlist[]); ////////////////////////////////////////////////////////////////////////////// // Public Function: of_DirList // Arguments: as_FileSpec The file spec. to list (including wildcards); an // absolute path may be specified or it will // be relative to the current working directory // al_FileType A number representing one or more types of files // to include in the list, see PowerBuilder Help on // the DirList listbox function for an explanation. // anv_DirList[] An array of n_cst_dirattrib structure whichl will contain // the results, passed by reference. // Returns: Long // The number of elements in anv_DirList if successful, -1 if an error occurrs. // Description: List the contents of a directory (Name, Date, Time, and Size). //////////////////////////////////////////////////////////////////////////////
Sources : http://pfc.codexchange.sybase.com/files … 15/pfc.zip
Utilisation et exemples : cf doc Sybase.
3) Utilisation de l'API windows
Sous Windows et lorque les PFC ne sont pas utilisées, on peut s'inspirer de la fonction of_dirlist des PFC, en utilisant les fonctions FindFirstFileA, FindNextFileA et FindClose de KERNEL32.DLL.
Exemple d'un objet n_file avec une méthode of_dirlist qui remplit un tableau de nom de fichiers en fonction d'un chemin avec masque :
forward global type n_file from nonvisualobject end type end forward global type n_file from nonvisualobject autoinstantiate end type type prototypes Function Long FindFirstFileA( Ref String filename, Ref os_finddata findfiledata ) Library "KERNEL32.DLL" Function Boolean FindNextFileA( Long handle, Ref os_finddata findfiledata ) Library "KERNEL32.DLL" Function Boolean FindClose( Long handle ) Library "KERNEL32.DLL" end prototypes type variables // déclarations des structures en instance (vs compilation) os_finddata istr_finddata os_filedatetime istr_filedatetime end variables forward prototypes public function integer of_dirlist (string as_pathfile, ref string as_dirlist[]) end prototypes public function integer of_dirlist (string as_pathfile, ref string ref_as_dirlist[]); // récupération de la liste des fichiers // arg : as_pathfile : chemin + filtre (ex: 'c\temp\*.txt') // ref : ref_as_dirlist[] : tableau des noms de fichiers // ret : nb fichiers Long ll_handle, ll_file_nr = 0 String ls_tab_empty[] Boolean lb_more_files // ràz tableau ref_as_dirlist = ls_tab_empty // cherche le 1er fichier correspondant au chemin + filtre ll_handle = FindFirstFileA( as_pathfile, This.istr_finddata ) // si aucun fichier trouvé : retourne 0, sinon : boucle sur les fichiers If ll_handle <> -1 Then Do ll_file_nr ++ // ajout du nom de fichier au tableau ref_as_dirlist[ ll_file_nr ] = This.istr_finddata.ch_filename // Récupère le fichier suivant lb_more_files = FindNextFileA( ll_handle, This.istr_finddata ) Loop While lb_more_files End If // fermeture FindClose( ll_handle ) Return ll_file_nr end function on n_file.create call super::create TriggerEvent( this, "constructor" ) end on on n_file.destroy TriggerEvent( this, "destructor" ) call super::destroy end on
Structures à déclarer :
global type os_filedatetime from structure unsignedlong ul_lowdatetime unsignedlong ul_highdatetime end type global type os_finddata from structure 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] end type
Appel :
n_file lnv_file String ls_tab_file[] lnv_file.of_dirlist( "C:\EMPLOYEE\*.TXT", ls_tab_file )
Dans cet exemple seule la propriété ch_filename est utilisée. D'autres informations utiles de la structure os_finddata peuvent être exploitées : attribut, dates, taille. (cf PFC)
A vos claviers !
HTH
Hors ligne
bonne idée, ça peut être utile. Je parle en connaissance de cause
Hors ligne
Hors ligne
Pages: 1