Find file and get full path; expand special folder name

s.searchpath([file] [path])
s.expandpath([file] [flags])

 

Parts

s - str variable.

file - full path or filename (with extension) of file or folder. Also can be drive (should end with \). Full path can begin with special folder name enclosed in $, or environment variable enclosed in %. Default: s.

path - folder where to search. If file contains full path, this part is ignored. Default: "".

flags - one of the following values:

1 Expand all (and only) environment variables. Without this flag, environment variables are expanded only if file begins with %.
2 QM 2.2.0. Unexpand path. For example, if file is "c:\program files\some prog", s will become "$program files$\some prog".

 

Remarks

searchpath searches for file in path. If path is omitted and file does not contain full path, searches in these places. If file is full path, only tests file existence. If file exists, s will receive full path. Otherwise, s will be empty.

 

expandpath only expands special folder name or environment variable, but does not search. All QM file functions support special folders, so you don't have to use this function before calling them. You can use it before calling dll functions.

 

See also: iff dir

 

Examples

 If file exists, get full path:
str s.searchpath("c:\folder\file.txt")
if(s.len) out s ;; "c:\folder\file.txt"
else out "not found"
 
 Search for "notepad.exe" and get full path:
str s.searchpath("notepad.exe")
if(s.len) out s ;;e.g. "c:\windows\system32\notepad.exe"
else out "not found"

 Get full path when the given path begins with special folder name:
str s.expandpath("$Desktop$\new file.txt")
out s ;;e.g. "c:\windows\desktop\new file.txt"