What is the naming standard for path components?

后端 未结 8 1555
广开言路
广开言路 2020-12-22 15:22

I keep getting myself in knots when I am manipulating paths and file names because I don’t follow a naming standard for path components.

Consider the following toy pr

8条回答
  •  爱一瞬间的悲伤
    2020-12-22 16:08

    No you're not crazy.

    In Windows systems, sometimes the path of the directory containing the file is called path, which is how it was from the beginning. So, for example,

        x:\dir1\dir2\myfile.txt
    
        Windows:
        --------
            PATH:  x:\dir1\dir2
            FILE:  myfile.txt
    
        Unix/Linux:
        -----------
            PATH:  /dir1/dir2/myfile.txt
            FILE:  myfile.txt
    

    The Unix/Linux approach is a lot more logical, and that's what everyone mentioned above: path including the file name itself. However, if you type "call /?" in the Windows command line, you get this:

        %~1         - expands %1 removing any surrounding quotes (")
        %~f1        - expands %1 to a fully qualified path name
        %~d1        - expands %1 to a drive letter only
        %~p1        - expands %1 to a path only
        %~n1        - expands %1 to a file name only
        %~x1        - expands %1 to a file extension only
    

    So there it is, "path only" and "file name only". At the same time, they refer to the whole string as "fully qualified path name" which is understood as drive letter plus path plus file name. So there's no real truth. It's futile. You've been betrayed.

    Anyway,

    To answer your question

    This is how I'd name your examples:

    A: -
    B: basename
    C: extension
    D: -
    E: -
    F: -
    G: -
    H: pathname (or dirname or containing path)
    I: full name
    

    A-D-E-F have no simple nicknames. And since php is probably the most widely known cross-platform language, everyone understands "basename" and "dirname" so I'd stick with that naming. Full name is also obvious; full path would be a bit ambiguous but most of the time it means the very same thing.

提交回复
热议问题