Version 21 of file normalize

Updated 2013-04-17 23:18:00 by pooryorick

Summary

normalize a file name

Synopsis

file normalize name

Description

Returns a unique normalised path representation for the file-system object (file, directory, link, etc), whose string value can be used as a unique identifier for it. A normalized path is an absolute path which has all '../', './' removed. Also it is one which is in the "standard" format for the native platform. On MacOS, Unix, this means the segments leading up to the path must be free of symbolic links/aliases (but the very last path component may be a symbolic link), and on Windows it also means we want the long form with that form's case-dependence (which gives us a unique, case-dependent path).

The one exception concerning the last link in the path is necessary, because Tcl or the user may wish to operate on the actual symbolic link itself (for example file delete, file rename, file copy are defined to operate on symbolic links, not on the things that they point to).

See Also

file
file nativename
file readlink

Bugs

1194458
fixed in 8.4.11

Forward Compatibility

file forward compatibility
[file normalize] emulation code taken from critcl.tcl, and added to the wiki in the hope that more people will use normalize, and move faster to 8.4 and later.

Volume-Relative Paths

Windows has the concept of a current directory per drive, a concept it inherited from DOS. From a Command Prompt you can do:

c:
cd \winnt
d:
type c:setup.log

and see setup.log from the current directory \winnt on drive c:. It's an obscure featrue, and it doesn't work in Windows Explorer.

Therefore, if the current working directory is c:\winnt,

file normalize c:setup.log

results in

C:/WINDOWS/WindowsUpdate.log

Prior to Tcl version 8.4.5, [file normalize] didn't handle volume relative paths correctly:

file normalize c:a/b

resulted in c:a/b

File normalize and Windows folder junctions

2013-01-08: All TCL version before 8.5.13, 8.6.0 have bugs with folder junctions, when the access rights are restricted tcl-Bugs-3092089 , tcl-Bugs-3587096 . They are fixed in Tcl fossil Check-in 8a291bcb44 .

HaO: file normalize will resolve junctions of the Windows NTFS file system and return the path without the junctions, if there is a component after the junction. This is specially helpful, if the junction has less access rights than the direct way (which is the case for localized "program files" folders of Windows Vista). I took the habbit, to pass any files in system folders by file normalize before accessing them.

Example

Create a folder 'C:\test2' and a junction 'C:\test2_junction' to it.

In a dos box with administrator rights:

C:\Windows\system32> cd c:\
C:\> mkdir test2
C:\> mklink /j test2_junction test2

Now test file normalize in a wish console:

% file normalize c:/test2_junction
c:/test2_junction
% file normalize c:/test2_junction/test.txt
c:/test2/test.txt