Home > Language Reference > File Functions > DirTree


The Dirtree function returns a list of the files and folders found matching the specified filespec.


In an automation task, you often have to repeat the same actions for all the files within a folder, or for all the files with a specific extension. DirTree function returns the needed list of files to process. See the second example below.


count=DirTree(<file_spec>, tab_long_filename$(), tab_short_filename$(), tab_path$(), tab_attribute_filename$(), <option_string>)


<file_spec>, string. For example, to return the list of files in the c:\wttest folder specify "c:\wttest\*.*"   . If the path is omitted, the search is performed in the current working folder. If option "R" is specified, the files are searched in the current folder and all its subfolders.

tab_long_filename$(), array of strings. It contains the long file names of the files found (including hidden or system files), without their paths.

tab_short_filename$(), array of strings. It contains the short file names (DOS 8.3 format) of the files found with their paths.

tab_path$(), array of strings. It contains the path (without the \ at the end) of the files found.

tab_attribute_filename$(), array of strings. It contains the attributes of the files found. The possible values are:

Return value   Attribute description
 R  Read only
 H  Hidden
 A  Archive
 S  System

<option_string>, string which specifies the search and sort order of the result arrays. It may contain one or more of the following letters:
 <option_string>  Description 
 N   Sort by name
 S  Sort by size
 E  Sort by extension
 D   Sort by date
 R   Include subfolders
 A  Sort by last access date
For example,  if <option_string> is "AN", the arrays will be sorted by last access date. For identical access dates, files will be sorted by name. 

Return value

count, optional integer. If the function succeeds, it returns the number of files found. If the function fails, the return value is -1 (see File Functions Error Codes).


The arrays used as arguments for the DirTree function must be declared at the beginning of the script using Dim function.

See also



Dim tab1$(100)
Dim tab2$(100)
Dim tab3$(100)
Dim tab4$(100)

'Returns all the *.LST filenames within the current folder and its subfolders.
if num=0 then stop endif
until i>=num


'Use of DirTree in a loop for repeating the same tasks on all the files within a folder
'The script below lists all the files with a .txt extension in a folder, loads each of them in notepad, makes some changes in each of them,
'and saves under the same filename concatenated with the day of the month.

'Declaration of the arrays needed by DirTree function for retrieving all the files
'with extension .txt in the specified directory
dim longname$(500)
dim shortname$(500)
dim pathname$(500)
dim att$(500)
'path where the files to process are
'In this example, we use notepad to process each individual file with extension .txt
'It can be any application, for instance Photoshop.
'nb gives the number of *.doc files present in DIREC$
if nb=0 then msgbox("no file to process !") end endif
'now the loop which makes the repetitive actions for each file
'In this example, the tasks to process on each file are
'Open the file, then format it, then save under the same name concatenated with the date of the day
UseWindow("NOTEPAD.EXE|Notepad|Untitled - Notepad",1)
 ChooseMenu(Normal,"&File|&Open... Ctrl+O")

Pause 10 ticks   ' wait a very short time to let the cursor be in Filename field
SendKeys(direc$+longname$(i),Noactivate)   'In Filename field, type the file to process. Noactivate keyword is used to type where the cursor is
'The file longname$(i) is now opened, format it
'Truncate the window name for the Notepad window to cover any filename window title
 ChooseItem(Combo, "3", "16", single, left )
'Save the modified file under the same name but with the day number of the current month added
'We extract the filename without the extension in leftpart$
'excluding the . character
'Select File/save as menu
 ChooseMenu(Normal,"&File|Save &As...")

UseWindow("NOTEPAD.EXE|FloatNotifySink|Save As|1",1)
Pause 10 ticks
'Type the new filename
UseWindow("NOTEPAD.EXE|#32770|Save As",1)
pause 1
until i=nb
'at the end, close the application