Difference between revisions of "Determine if two file paths refer to the same file"

From CodeCodex

(Ruby)
(Erlang)
 
Line 1: Line 1:
 
==Implementations==
 
==Implementations==
 +
===Erlang===
 +
<pre>
 +
same_path(Path1, Path2) ->
 +
    Abs1 = absname(Path1),
 +
    Abs2 = absname(Path2),
 +
    Abs1 =:= Abs2.
 +
 +
absname(Path) ->
 +
    Path2 = filename:split(Path),
 +
    case string:chr(hd(Path2), $/) of
 +
        0 -> {ok, Cwd} = file:get_cwd(),
 +
            Abs = lists:reverse(filename:split(Cwd)),
 +
            absname(Path2, Abs);
 +
        _ -> absname(Path2, [])
 +
    end.
 +
 +
absname([], Absname) ->
 +
    filename:join(lists:reverse(Absname));
 +
absname([H|T], Absname) ->
 +
    case H of
 +
        "."  -> absname(T, Absname);
 +
        ".." -> absname(T, tl(Absname));
 +
        _    -> absname(T, [H|Absname])
 +
    end.
 +
</pre>
 +
 
===Java===
 
===Java===
 
A filename path may include redundant names such as `.' or `..' or symbolic links (on UNIX platforms). File.getCanonicalFile() converts a filename path to a unique canonical form suitable for comparisons.
 
A filename path may include redundant names such as `.' or `..' or symbolic links (on UNIX platforms). File.getCanonicalFile() converts a filename path to a unique canonical form suitable for comparisons.
<pre>
+
<pre class="java">
 
     File file1 = new File("./filename");
 
     File file1 = new File("./filename");
 
     File file2 = new File("filename");
 
     File file2 = new File("filename");
Line 23: Line 49:
 
===Ruby===
 
===Ruby===
 
Converts a pathname to an absolute pathname and compares them.
 
Converts a pathname to an absolute pathname and compares them.
<pre>
+
<pre class="ruby">
 
def same_path?(path1, path2)
 
def same_path?(path1, path2)
 
   path1 = File.expand_path(path1)
 
   path1 = File.expand_path(path1)
Line 33: Line 59:
 
</pre>
 
</pre>
  
 +
[[Category:Erlang]]
 
[[Category:Java]]
 
[[Category:Java]]
 
[[Category:Ruby]]
 
[[Category:Ruby]]
 
[[Category:I/O]]
 
[[Category:I/O]]

Latest revision as of 17:55, 19 January 2011

Implementations[edit]

Erlang[edit]

same_path(Path1, Path2) ->
    Abs1 = absname(Path1),
    Abs2 = absname(Path2),
    Abs1 =:= Abs2.

absname(Path) ->
    Path2 = filename:split(Path),
    case string:chr(hd(Path2), $/) of
        0 -> {ok, Cwd} = file:get_cwd(),
             Abs = lists:reverse(filename:split(Cwd)),
             absname(Path2, Abs);
        _ -> absname(Path2, [])
    end.

absname([], Absname) ->
    filename:join(lists:reverse(Absname));
absname([H|T], Absname) ->
    case H of
        "."  -> absname(T, Absname);
        ".." -> absname(T, tl(Absname));
        _    -> absname(T, [H|Absname])
    end.

Java[edit]

A filename path may include redundant names such as `.' or `..' or symbolic links (on UNIX platforms). File.getCanonicalFile() converts a filename path to a unique canonical form suitable for comparisons.

    File file1 = new File("./filename");
    File file2 = new File("filename");
    
    // Filename paths are not equal
    boolean b = file1.equals(file2);      // false
    
    // Normalize the paths
    try {
        file1 = file1.getCanonicalFile(); // c:\almanac1.4\filename
        file2 = file2.getCanonicalFile(); // c:\almanac1.4\filename
    } catch (IOException e) {
    }
    
    // Filename paths are now equal
    b = file1.equals(file2);              // true

Ruby[edit]

Converts a pathname to an absolute pathname and compares them.

def same_path?(path1, path2)
  path1 = File.expand_path(path1)
  path2 = File.expand_path(path2)
  path1 == path2
end

p same_path?("./filename", "filename")  #=> true