开发者

What are "." and ".." in Windows' directory?

开发者 https://www.devze.com 2023-02-11 21:10 出处:网络
I am writing a program to list out all files in a directory on Windows. I simply use those File Management functions provided but I am curious about the result:

I am writing a program to list out all files in a directory on Windows. I simply use those File Management functions provided but I am curious about the result:

Target file is *.*
The first file found is .
The next file found is ..
The next file found is file0
The next file found is file1
...
...
The next file found is fileN

So technic开发者_如何学JAVAally, what are these "." and ".." ? I recalled I use "cd .." in cmd to exit to the upper directory. For "cd .", I don't know what is it for.


"." is the current directory.

".." is the parent directory.

This is the same as Unix systems. From your output it looks like Windows is treating them as files.

In the past I've used the "." to make sure that the command (what ever it might be) finds the file in the current directory. The following two statements should be identical:

run some.exe

run ./some.exe

but if you have some weird search rules that looks on the PATH or in C:\Windows first (for example) then they aren't.

I've found these statements:

  • Use a period as a directory component in a path to represent the current directory, for example ".\temp.txt". For more information, see Paths.
  • Use two consecutive periods (..) as a directory component in a path to represent the parent of the current directory, for example "..\temp.txt". For more information, see Paths.

on the MSDN page on Naming Files, Paths, and Namespaces but there's no explanation of what they actually are.

The Wikipedia page on Path as a little more information, but again doesn't explain how these are actually stored.


The single dot represents the current working directory. The double dot represents the current working directory's parent directory.

Neither of these represent a concrete 'file' in the underlying file system, but represent the directory's location within the file system hierarchy. Although you don't mention it, the '/' symbol, on its own, is similar to these in that it generally represents the root of a particular file system. ie

cd /

will take you to the root of the current file system.

Rather, the '.' and '..' are references to available resources within the file system, and as such are pseudo-files or pseudo-references generated by the request for file information to the underlying file system and are included to aid navigation around the file system. They are generally OS independent ie. most OS systems respect their use.

You say you are 'writing a program'. Many file management routines that retrieve directory listings include a switch or a method which ignores these pseudo-files when generating directory listings.

However, its normally easiest to write your program to iterate over the entries and ignore these directory entries if you don't need them. Alternatively write your program to only list files, or even files of the specific type you are looking for. For example, use DOS type commands in Windows, executing

dir /a:-d

will show only files in the directory but not the '.' and '..' entries. while

dir *.txt

would list only .txt files in the directory.


as mentioned above '.' represents the current directory. While '..' represents the parent directory. Windows represents them as links.

Incidentally, if ur in a root directory then windows (cmd prompt mode) considers '.' and '..' as one and same.

of course in windows explorer the following dont hold good. there if u go to parent directory of say C:\ then u reach the My computer.

0

精彩评论

暂无评论...
验证码 换一张
取 消