Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ◇C++, ◇Clojure, ◇Scala


[ edit | logout ]
[ sandbox | passwd ]

The Unix `which` command

You need something like the which command, i.e. you want to figure out where a binary is in the PATH (in which directory).

import std/os
import std/strutils


proc which(fname: string): string =
  let
    sep = if defined(windows): ";" else: ":"
    dirs = getEnv("PATH").split(sep)

  for dir in dirs:
    let path = joinPath(dir, fname)
    if fileExists(path):
      return path
  #
  return ""    # not found

proc main() =
  echo which("date")            # /usr/bin/date

  echo which("nim")             # /home/jabba/.nimble/bin/nim

  echo which("doesnt_exist")    # "" (empty string)

##########

when isMainModule:
  main()

If the binary is found, the function returns its full path.

If not found, then an empty string is returned.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 April 23, 21:04