d-examples/source/examples/common/isexists.d

30 lines
809 B
D

module examples.common.isexists;
import std.process : environment;
import std.file : exists, isFile;
import std.path : pathSeparator, buildPath;
import std.array : split;
import std.stdio : writefln;
// Проверка существования утилиты в PATH
private bool isExecutableExists(string appName) {
auto path = environment.get("PATH");
if (path is null) return false;
foreach (dir; path.split(pathSeparator)) {
auto fullPath = buildPath(dir, appName);
if (exists(fullPath) && isFile(fullPath)) {
return true;
}
}
return false;
}
void isExists() {
foreach (app; ["ls", "mc", "htop", "bash", "nano"]) {
if (isExecutableExists(app))
writefln("Приложение %s найдено", app);
else
writefln("Приложение %s не найдено", app);
}
}