Get an absolute file path from a relative file path

From CodeCodex

Implementations[edit]

Common Lisp[edit]

> (directory "filename.txt")

(#P"/home/Username/filename.txt")

Erlang[edit]

% current working directory : /temp
filename:absname("test.dat").   % "/temp/test.dat"

% or
filename:absname("test.dat", "/usr/local").     % "/usr/local/test.dat"

Java[edit]

    File file = new File("filename.txt");
    file = file.getAbsoluteFile();  // c:\temp\filename.txt
    
    file = new File("dir"+File.separatorChar+"filename.txt");
    file = file.getAbsoluteFile();  // c:\temp\dir\filename.txt
    
    file = new File(".."+File.separatorChar+"filename.txt");
    file = file.getAbsoluteFile();  // c:\temp\..\filename.txt
    
    // Note that filename.txt does not need to exist

Ruby[edit]

Dir.chdir("/work/backup") do
  fname = "tmp.txt"
  p File.expand_path(fname)    #=> "C:/work/backup/tmp.txt"
end