Anybody else noticed this inconsistency (using Berlin 10.1)?

Anybody else noticed this inconsistency (using Berlin 10.1)?

extractfilepath ('\\192.168.0.1') results in '\\'
extractfilepath ('\\2001:db8::1') results in '\\2001:db8::'

Looks to me like extractfilepath isn't IPv6 aware yet, because it seems to think that the "1" at the end of the IP is a filename, not part of the path. I checked, and

extractfilename ('\\2001:db8::1') indeed results in '1'.

Comments

  1. Jasper Bongertz Actually you're using ExtractFilePath wrong. You should provide a Filename. Definition: "Returns the drive and directory portions of a file name.". When providing a filename the function works perfectly fine. ExtractFilePath should certainly NOT be used to traverse back to the root directory. It isn't made for that and when not providing a filename it has uncertain results.

    ReplyDelete
  2. Rik van Kekem I guess you're right, I abused it for something it is not designed to do. It's an interesting side effect though that it still does work for FQDN and IPv4 based UNCs, but not IPv6 :-)

    ReplyDelete
  3. Jasper Bongertz It's not strange if you look at the source of ExtractFilePath. The first line has LastDelimiter(PathDelim + DriveDelim). And DriveDelim is ':'. So it's logical it messes up with a "host"/Drivename with ":" in the middle.

    If you really want to traverse back to the "root" directory you could also use ExpandFilename and use "..\" in the filename to go back one directory.
    Memo1.Lines.Add(ExtractFilePath(ExpandFileName('\\192.168.1.1\share\subdir\test.txt')));
    Memo1.Lines.Add(ExtractFilePath(ExpandFileName('\\192.168.1.1\share\subdir\..\test.txt')));
    Memo1.Lines.Add(ExtractFilePath(ExpandFileName('\\192.168.1.1\share\subdir\..\..\test.txt')));
    Memo1.Lines.Add(ExtractFilePath(ExpandFileName('\\192.168.1.1\share\subdir\..\..\..\test.txt')));
    Memo1.Lines.Add(ExtractFilePath(ExpandFileName('C:\share\subdir\test.txt')));
    Memo1.Lines.Add(ExtractFilePath(ExpandFileName('C:\share\subdir\..\test.txt')));
    Memo1.Lines.Add(ExtractFilePath(ExpandFileName('C:\share\subdir\..\..\test.txt')));
    Memo1.Lines.Add(ExtractFilePath(ExpandFileName('C:\share\subdir\..\..\..\test.txt')));

    Gives:
    \\192.168.1.1\share\subdir\
    \\192.168.1.1\share\
    \\192.168.1.1\share\
    \\192.168.1.1\share\
    C:\share\subdir\
    C:\share\
    C:\
    C:\

    You'll see that you never get closer to \\ than with the actual share-name included. Which is the actual root-directory. Just \\IP is never a "root"-directory because you can't use it. The first really useful path you can use is \\192.168.1.1\share because you can put files in there. So that's the root.

    And if you are traversing that you can check if the "root" is the same as the previous iteration and then you have the real root. It should also work for IPv6 as long as you include a filename in the ExtractFilePath() calls.

    (But there are other ways to do it too)

    ReplyDelete

Post a Comment