Home > Language Reference > File Functions > DirTree

DirTree

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

Usage

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.

Syntax

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

Parameters

<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).

Remarks

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

See also

Dir
FileAttr$

Examples

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

spec$="*.lst"
'Returns all the *.LST filenames within the current folder and its subfolders.
num=dirtree(spec$,tab1$(),tab2$(),tab3$(),tab4$(),"R")
if num=0 then stop endif
i=0
repeat
msgbox(tab1$(i))
i=i+1
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
DIREC$="C:\wttest\"
 
'In this example, we use notepad to process each individual file with extension .txt
'It can be any application, for instance Photoshop.
shell("notepad")
 
'nb gives the number of *.doc files present in DIREC$
nb=dirtree(direc$+"*.txt",longname$(),shortname$(),pathname$(),att$(),"")
 
if nb=0 then msgbox("no file to process !") end endif
 
'now the loop which makes the repetitive actions for each file
i=0
repeat
'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")

UseWindow("NOTEPAD.EXE|DirectUIHWND|Open|2",1)
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
UseWindow("NOTEPAD.EXE|#32770|Open",1)
 Click(Button,"&Open")
'The file longname$(i) is now opened, format it
'Truncate the window name for the Notepad window to cover any filename window title
UseWindow("NOTEPAD.EXE|Notepad|",1)
 ChooseMenu(Normal,"F&ormat|&Font...")
UseWindow("NOTEPAD.EXE|#32770|Font",1)
 ChooseItem(Combo, "3", "16", single, left )
 Click(Button,"OK")
'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
pos=instr(longname$(i),".")
leftpart$=left$(longname$(i),pos-1)
'Select File/save as menu
UseWindow("NOTEPAD.EXE|Notepad|",1)
 ChooseMenu(Normal,"&File|Save &As...")

UseWindow("NOTEPAD.EXE|FloatNotifySink|Save As|1",1)
Pause 10 ticks
'Type the new filename
 SendKeys(direc$+leftpart$+day$(),NoActivate)
UseWindow("NOTEPAD.EXE|#32770|Save As",1)
 Click(Button,"&Save")
 
pause 1
i=i+1
until i=nb
 
'at the end, close the application
KillApp("notepad.exe",1)