PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → Better way to count files in a directory?
Better way to count files in a directory?
Iniciado por guest, 15,jun. 2016 14:42 - 2 respuestas
Publicado el 15,junio 2016 - 14:42
Is there a better way to count the number of files in a directory with a given extension?

PROCEDURE CountFiles(LOCAL p_directory is string, LOCAL p_extension is string)

l_FileCount is int
l_FileString is ANSI string

l_FileString = fListFile(CompleteDir(p_directory) + "*." + p_extension,frNotRecursive + frNoHiddenFile + frNoHiddenDirectory)

IF NoSpace(l_FileCount,sscAll) = "" THEN
l_FileCount = 0
ELSE
l_FileCount = StringCount(l_FileString,CR) + 1
END

RESULT l_FileCount
Publicado el 16,junio 2016 - 10:05
result whitin 1 second

NbROFiles is int // Number of files matching the criteria
NbTXTFiles is int // Number of "*.TXT" files
NbTXTFiles = 0
SearchCriterion is string

// Select the directory in which the files will be listed
SearchCriterion = fSelectDir("", "Select a directory", ...
"Finding the .TXT files in the selected directory")

// Add the "\*.TXT" criteria at the end of name of the selected directory
SearchCriterion = SearchCriterion + "\*.TXT"

// List the "*.TXT" files
NbTXTFiles = fListFile(SearchCriterion, "FileAttribute", &NbROFiles)

// Display the number of files matching the criteria
Info("There are " + NbROFiles + " read-only files out of " + NbTXTFiles + " listed files")

Marc :hot:
Publicado el 16,junio 2016 - 10:12
AND A LOCAL PROCEDURE

PROCEDURE FileAttribute(Directory, Name, Change, NbTXTFiles)
// Declare the variable
NbROFiles is int
// Read-only files?
IF Position(fAttribute(Directory + Name), "R") <> 0 THEN
Transfer(&NbROFiles, NbTXTFiles, 4)
NbROFiles++
Transfer(NbTXTFiles, &NbROFiles, 4)
// Display the name of the file in "LIST_FileList"
ListAdd(LIST_FileList, Directory + Name)
END
RESULT True