Difference between revisions of "Get the parent of a file path"

From CodeCodex

 
(Erlang)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
==Implementations==
 
==Implementations==
===Java===
+
 
 +
===Erlang===
 
<pre>
 
<pre>
 +
% Get the parent of a relative filename path
 +
filename:dirname("test/codex.erl").        % "test"
 +
filename:dirname("codex.erl").              % "."
 +
 +
% Get the parents of an absolute filename path
 +
filename:dirname("/usr/src/codex.erl").    % "/usr/src"
 +
</pre>
 +
 +
===Java===
 +
<pre class="java">
 
     // Get the parent of a relative filename path
 
     // Get the parent of a relative filename path
 
     File file = new File("Ex1.java");
 
     File file = new File("Ex1.java");
Line 19: Line 30:
 
</pre>
 
</pre>
 
* Original Source: [http://javaalmanac.com/egs/java.io/pkg.html?l=rel#Files The Java Developers Almanac 1.4]
 
* Original Source: [http://javaalmanac.com/egs/java.io/pkg.html?l=rel#Files The Java Developers Almanac 1.4]
 +
 +
===Ruby===
 +
<pre class="ruby">
 +
Dir.chdir("/work") do
 +
  # Get the parent of a relative filename path
 +
  fname = "backup/tmp.txt"
 +
  p parent = File.dirname(fname)        #=> "backup"
 +
  p parent = File.dirname(parent)      #=> "."
 +
  p parent = File.dirname(parent)      #=> "."
 +
 
 +
  # Get the parents of an absolute filename path
 +
  p expath = File.expand_path(fname)    #=> "C:/work/backup/tmp.txt"
 +
  p parent = File.dirname(expath)      #=> "C:/work/backup"
 +
  p parent = File.dirname(parent)      #=> "C:/work"
 +
  p parent = File.dirname(parent)      #=> "C:/"
 +
  p parent = File.dirname(parent)      #=> "C:/"
 +
end
 +
</pre>
 +
 +
[[Category:Erlang]]
 
[[Category:Java]]
 
[[Category:Java]]
 +
[[Category:Ruby]]
 
[[Category:I/O]]
 
[[Category:I/O]]

Latest revision as of 18:25, 13 January 2011

Implementations[edit]

Erlang[edit]

% Get the parent of a relative filename path
filename:dirname("test/codex.erl").         % "test"
filename:dirname("codex.erl").              % "."

% Get the parents of an absolute filename path
filename:dirname("/usr/src/codex.erl").     % "/usr/src"

Java[edit]

    // Get the parent of a relative filename path
    File file = new File("Ex1.java");
    String parentPath = file.getParent();      // null
    File parentDir = file.getParentFile();     // null
    
    // Get the parents of an absolute filename path
    file = new File("D:\\almanac\\Ex1.java");
    parentPath = file.getParent();             // D:\almanac
    parentDir = file.getParentFile();          // D:\almanac
    
    parentPath = parentDir.getParent();        // D:\
    parentDir = parentDir.getParentFile();     // D:\
    
    parentPath = parentDir.getParent();        // null
    parentDir = parentDir.getParentFile();     // null

Ruby[edit]

Dir.chdir("/work") do
  # Get the parent of a relative filename path
  fname = "backup/tmp.txt"
  p parent = File.dirname(fname)        #=> "backup"
  p parent = File.dirname(parent)       #=> "."
  p parent = File.dirname(parent)       #=> "."
  
  # Get the parents of an absolute filename path
  p expath = File.expand_path(fname)    #=> "C:/work/backup/tmp.txt"
  p parent = File.dirname(expath)       #=> "C:/work/backup"
  p parent = File.dirname(parent)       #=> "C:/work"
  p parent = File.dirname(parent)       #=> "C:/"
  p parent = File.dirname(parent)       #=> "C:/"
end