在操作路径和文件名时,我总是陷入困境,因为我没有遵循路径组件的命名标准。
考虑下面的玩具问题(以Windows为例,但希望答案与平台无关)。你已经得到了一个文件夹的路径:
C:\Users\OddThinking\Documents\My Source\
您希望遍历下面的文件夹并将所有.src文件编译为.obj文件。
在某种程度上,你会看到以下路径:
C:\Users\OddThinking\Documents\My Source\Widget\foo.src
你如何命名下面的路径组件?
A. foo
B. foo.src
C. src
D. .src
E. C:\Users\OddThinking\Documents\My Source\ (absolute path of the root)
F. Widget\foo.src (relative path of the file to absolute path of the root)
G. Widget\
H. C:\Users\OddThinking\Documents\My Source\Widget\
I. C:\Users\OddThinking\Documents\My Source\Widget\foo.src
以下是我的尝试:
A.基地名称?Basename吗?
B.文件名?文件名吗?在选择标识符名称时,差异很重要,这里我从来没有保持一致。
c扩展吗?
d .扩展吗?等等,这就是我所说的c,我应该避免存储点,只在需要的时候放进去吗?如果某个文件上没有点怎么办?
E. ?
F. ?
g .文件夹吗?但这不是windows特有的术语吗?
H.路径名称?路径名吗?路径?
一、文件名?等等,这就是我说的c路径名?等等,这就是我说的H。
我认为您对“标准”命名约定的搜索将是徒劳的。以下是我根据现有的知名项目提出的建议:
A) C:\users\OddThinking\Documents\My Source\Widget\foo.src
---
Vim称其为文件根(:help filename-modifiers)
B) C:\users\OddThinking\Documents\My Source\Widget\foo.src
-------
文件名或基本名称
C) C:\users\OddThinking\Documents\My Source\Widget\foo.src
___ (without dot)
-文件name分机
D) C:\users\OddThinking\Documents\My Source\Widget\foo.src
____ (with dot)
还有文件扩展名。简单地存储没有点,如果文件上没有点,它就没有扩展名
E) C:\users\OddThinking\Documents\My Source\Widget\foo.src
-----------------------------------------
树的顶端
没有约定,git称它为base directory
F) C:\users\OddThinking\Documents\My Source\Widget\foo.src
--------------
从树顶到树叶的路径
相对路径
G) C:\users\OddThinking\Documents\My Source\Widget\foo.src
------
树的一个节点
没有约定,可能是一个简单的目录
H) C:\users\OddThinking\Documents\My Source\Widget\foo.src
------------------------------------------------
你的名字
I) C:\users\OddThinking\Documents\My Source\Widget\foo.src
-------------------------------------------------------
全/绝对路径
foo Filename Without Extension
foo.src Filename
src Extension
.src Maybe Extension With Dot, but this should not be used. As written this could be a directory name or a filename.
C:\users\OddThinking\Documents\My Source\ [Absolute] Directory Path
Widget\foo.src Relative File Path
Widget Directory Name
C:\users\OddThinking\Documents\My Source\Widget\ This is still an Absolute Directory Path. If one is root and the other isn't, its up to your variable names to keep track of that, there isn't really a semantic difference there.
C:\users\OddThinking\Documents\My Source\Widget\foo.src [Absolute] File Path
“Filename”是一个单词,所以通常我们应该使用“Filename”而不是“文件名”(以及Filename而不是Filename)。
“目录”可以替换为“文件夹”。(也许我们应该在较低的层次上使用“目录”,但“文件夹”更短,这是我更喜欢的。)
实际上可以创建所有这些类型的语义框架,并使用语义上有效的函数来组合它们。例如,FolderName和Filename可以组合成RelativeFilePath。一个FolderPath(绝对路径)和一个RelativeFilePath可以组合成一个FilePath(绝对路径)。
还有一些是相关的;例如,FilenameWithoutExtension是一种文件名,所以应该是可转换的。FolderName是相对文件夹路径,所以应该是可转换的。等。
不,你没疯。
在Windows系统中,有时包含文件的目录的路径被称为path,从一开始就是这样。举个例子,
x:\dir1\dir2\myfile.txt
Windows:
--------
PATH: x:\dir1\dir2
FILE: myfile.txt
Unix/Linux:
-----------
PATH: /dir1/dir2/myfile.txt
FILE: myfile.txt
Unix/Linux方法更符合逻辑,这就是上面每个人都提到的:路径包括文件名本身。然而,如果你在Windows命令行中输入“call /?”,你会得到这样的结果:
%~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
这就是,“仅路径”和“仅文件名”。同时,它们将整个字符串称为“完全限定路径名”,即驱动器号加路径加文件名。所以没有真正的真相。这是徒劳的。你被背叛了。
不管怎么说,
回答你的问题
以下是我给你举的例子:
A: -
B: basename
C: extension
D: -
E: -
F: -
G: -
H: pathname (or dirname or containing path)
I: full name
A-D-E-F没有简单的昵称。由于php可能是最广为人知的跨平台语言,每个人都理解“basename”和“dirname”,所以我坚持使用这个命名。全名也很明显;全路径可能有点模糊,但大多数时候它的意思是完全相同的。
我认为您对“标准”命名约定的搜索将是徒劳的。以下是我根据现有的知名项目提出的建议:
A) C:\users\OddThinking\Documents\My Source\Widget\foo.src
---
Vim称其为文件根(:help filename-modifiers)
B) C:\users\OddThinking\Documents\My Source\Widget\foo.src
-------
文件名或基本名称
C) C:\users\OddThinking\Documents\My Source\Widget\foo.src
___ (without dot)
-文件name分机
D) C:\users\OddThinking\Documents\My Source\Widget\foo.src
____ (with dot)
还有文件扩展名。简单地存储没有点,如果文件上没有点,它就没有扩展名
E) C:\users\OddThinking\Documents\My Source\Widget\foo.src
-----------------------------------------
树的顶端
没有约定,git称它为base directory
F) C:\users\OddThinking\Documents\My Source\Widget\foo.src
--------------
从树顶到树叶的路径
相对路径
G) C:\users\OddThinking\Documents\My Source\Widget\foo.src
------
树的一个节点
没有约定,可能是一个简单的目录
H) C:\users\OddThinking\Documents\My Source\Widget\foo.src
------------------------------------------------
你的名字
I) C:\users\OddThinking\Documents\My Source\Widget\foo.src
-------------------------------------------------------
全/绝对路径