count files ...

Prev Next

Function Names

count files, count files recursive

Description

Searches the disk for a particular files and/or directories and counts them

  • couunt files recursive searches recursively. Recursive strategy: If a subdirectory is encountered, the files inside the subdirectory will be searched immediately before continuing with the next entry.
  • count files searches in the specified directory.

Call as: function

Restrictions

Indirect parameter passing is disabled

Parameter count

0-4

Parameters

No.TypeDescription
Opt. 1
input
string drive or path name

Specify a disk drive (e.g. C:\) or a path name (/usr/local, or \\server_name\path_name\...). If no additional parameters are provided, then the path name may also contain a file name (to list 1 file) or matching pattern to list specific files.
Note: Wildcard symbols are supported here for the file name, but not for the path name (OK: C:\Subdir\*.txt, Not OK: C:\Sub*\*.txt)

Default value: Current working directory
Opt. 2
input
string pattern name

Specify a disk drive (e.g. C:\) or a path name (/usr/local, or \\server_name\path_name\...). If no additional parameters are provided, then the path name may also contain a file name (to list 1 file) or matching pattern to list specific files.
Note: Wildcard symbols are supported here for the file name, but not for the path name (OK: C:\Subdir\*.txt, Not OK: C:\Sub*\*.txt)

Default value: * (all files)
Opt. 3
input
numeral Match count limitation

<0 Search for all matches and return set containing all names. A set is also used if only one item has been found.
≥0 Count at least 1 (even if 0 is passed into this parameter) or specified number of files / directories.

Suggestion: Recommended to use -1 to count all files. Limiting the count (e.g. you need to know if more or less than specified files or directories are avaiable) will avoid wasting time searching and counting the remaining files.

Default value: <0 (no limit)
Opt. 4
input
string search option

One of the following:

files Files only
directories Directories only
files and directories Foth files and directoreies (default)
all Also includes hidden and system files

Default value: files and directories

Return value

TypeDescription
numeral Number of files and/or directories found

Examples

               echo("All files in a directory   : ", count files( "C:\Program Files\B4P\" ), new line );

               echo("All files                  : ", count files( ".\", "*.b4p"      ) );
               echo("1st file only              : ", count files( ".\", "*.b4p", 0   ) );
               echo("1st file only (same)       : ", count files( ".\", "*.b4p", 1   ) );
               echo("3 files only               : ", count files( ".\", "*.b4p", 3   ), new line );
               echo("All subdirectories         : ", count files( "C:\Program Files\B4P", "*", -1, directories   ) );
               echo("All .b4p/.txt recursively  : ",
                       count files recursive( "C:\Program Files\B4P", "*.txt,*.b4p", -1, files  ) );

Output

All files in a directory   : 9

All files                  : 6
1st file only              : 1
1st file only (same)       : 1
3 files only               : 3

All subdirectories         : 2
All .b4p/.txt recursively  : 20
Try it yourself: Open LIB_Function_count_files.b4p in B4P_Examples.zip. Decompress before use.

See also

search files
directory listing