Skip to content
Prev 261486 / 398502 Next

how to tell if two file paths refer to the same file

Does R have a standard function that takes two file paths
(e.g., "./myDirectory/file" and "myDirectory/file")
and returns TRUE if those paths refer to the same file?

The paths make take different routes ("absolute" or
relative paths or via different symbolic or hard links)
to the same file or may use different naming conventions
(Windows or DOS 8.3 on Windows).  If the file paths do not
refer to actual files the answer is not generally well-defined
and I'm sure what the best approach would be.

S+ has a match.path() function that is like match() but
the equality test is that the strings refer to the same
file.  On Unix it checks that the inode and device numbers
are the same; on Windows that _fullpath() returns the same
thing for both paths.  E.g.,
   > match.path(c("c:/PROGRA~1",
                  "c:\\temp",
                  "C:/Program Files/../Program Files"),
                c("C:\\Program Files",
                  "C:\\Temp"))
   [1] 1 2 1

I know about R's normalizePath() but it doesn't map all ways
of refering to a file to the same string so
   normalizePath(f1) == normalizePath(f1)
is not a reliable test.  E.g., with R 2.13.0 on Windows it
doesn't use a standard capitalization:
   > cat(normalizePath(c("c:/Program Files",
                         "C:/program files",
                         "c:\\PROGRA~1")), sep="\n")
   c:\Program Files
   C:\program files
   c:\Program Files

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com