Find file and get full path; expand special folder name

Syntax

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

 

Parameters

s - str variable.

file - full path or filename (with extension) of file or folder. Also can be drive (should end with \). Default: s.

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

flags:

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:\windows\system32\file.exe", s will be "$system$\file.exe".

 

Remarks

searchpath searches for file in path. If path is omitted and file is not full path, searches in these places. If file is full path, only tests file existence. If file exists, s will receive full path, else 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 and COM functions.

 

See also: FileExists

 

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"