findExePath function

String? findExePath(
  1. String exe, {
  2. String cwd = '',
})

Windows only: find exe from PAT}H variable (current working directory first, if specified).

Implementation

String? findExePath(String exe, {String cwd = ''}) {
  exe = pathExpand(exe);
  if (exe.contains(':')) // exe is rooted
  {
    if (!fileExists(exe)) return null;
    return pathFullName(exe);
  }
  var path = getenv('PATH') ?? '';
  path = '$cwd;$path';
  var pathSplit = path.split(';');
  for (int i = 0; i < pathSplit.length; i++) {
    String elem = pathSplit[i];
    elem = elem.trim();
    elem = pathExpand(elem);
    if (elem.isNotEmpty && fileExists(path_path.join(elem, exe))) {
      return pathFullName(path_path.join(elem, exe));
    }
    String baseName = pathBaseName(exe);
    if (elem.isNotEmpty &&
        fileExists(path_path.join(elem, '$baseName.bin', exe))) {
      return pathFullName(path_path.join(elem, '$baseName.bin', exe));
    }
  }
  return null;
}