17

I've accidentally found that if I do:

dir C:\

I get a list of files that are direct children of the C: drive, in the root.

However, if I do:

dir C:

I get a huge list of files that I'm not even sure where they are (DLLs, EXEs, CPLs, etc.).

Do you know why does this happen and where does this list come from?

4
  • 10
    dir c: returns the list of files in your current directory
    – clhy
    Commented Dec 20, 2015 at 5:53
  • 2
    @The_IT_Guy_You_Don't_Like Unless you're on a different drive, in which case imagine each drive has a current directory, so if you're on Q:\ it shows for whatever directory "you're in" on C.
    – barlop
    Commented Dec 20, 2015 at 6:16
  • 3
    @BrianDrummond Nope. 100% wrong. Every drive in a Windows session has its own independent working directory per process. It absolutely does not have a single working directory name for all drives. Try it.
    – nobody
    Commented Dec 21, 2015 at 0:10
  • @AndrewMedico I'd have to dig out something with Windows to confirm - maybe I'm remembering DOS, LOL! so I'll defer to you. Comment deleted. Commented Dec 21, 2015 at 10:19

5 Answers 5

19

Specifying DIR location will show you the contents of the location.

Specifying C: on many commands, including the DIR command, refers to the C drive, and refers to the current directory. To see your current directory, type this:

c: 
cd

The cd command is typically used to change which directory is current. However, in MS-DOS (and similar operating systems, including modern Microsoft Windows, but not including Unix), running cd by itself will show you the current directory.

In all probability, if you haven't been using the cd command, then your current directory is probably the directory that your operating system was installed to. (At least, that's a common behavior for Microsoft Windows systems.)

You can do this:

cd "C:\Program Files"
cd
dir c:

That will show you the contents of C:\Program Files

Similarly, you can do something like: copy C:*.* and all contents from the current directory will be copied.

When you specify C:\, then the backslash indicates the "root" directory, also known as the "top level" directory. That might, or might not, be the same thing as your current directory.

If you just specify DIR \, then the current drive will be assumed. (You can type something like C: or D:, as an entire command (on a line by itself), to change which drive is considered to be the "current drive".) If you just specify DIR, then the current drive and the current directory will be assumed.

1
  • Oh yeah! This makes all the sense. Thank you very much. I'm a lot more familiar with unix shell, which doesn't have this kind of behavior. And yes, I didn't use "cd" in that session, so the contents being shown were from "C:\WINDOWS\system32". Very kind regards.
    – Nuno
    Commented Dec 20, 2015 at 13:46
21

TL;DR - dir C: will list the current directory on C: whatever that happens to be, as opposed to dir C:\ which will always list the \ root of C:.

Long story... At the command line prompt, cmd remembers the current directory for each drive it accessed during the session.

The current directory on a drive X: other than the current one can be set with cd X:\etc (without the /d switch which would also change the current drive to X:), and is returned by cd X: (without a \) - both noted in the cd /? help:

Type CD drive: to display the current directory in the specified drive.

Use the /D switch to change current drive in addition to changing current directory for a drive.

See Why does each drive have its own current directory? for background, including this quote.

Remembering the current directory for each drive has been preserved ever since [n.b. DOS], at least for batch files, although there isn’t actually such a concept as a per-drive current directory in Win32. In Win32, all you have is a current directory. The appearance that each drive has its own current directory is a fake-out by cmd.exe, which uses strange environment variables to create the illusion to batch files that each drive has its own current directory.

The strange environment variables referred to in the linked post What are these strange =C: environment variables? can be seen with set "" at the cmd prompt, for example:

C:\temp>cd X:\etc

C:\temp>cd C:
C:\temp

C:\temp>cd X:
X:\etc

C:\temp>set ""
=C:=C:\temp
=X:=X:\etc
ALLUSERSPROFILE=C:\ProgramData
[...]
1
  • 1
    dir C: will list the current directory on C: - for extra detail, if you have not set the current directory on C: at all then assuming it is your system partition (the one holding Windows' files) and depending on how you are logged & as which user & which version of windows, the default current directory will either be the home directory of the current user context (c:\Users\<you>), c:\windows\system32 or c:\windows\temp. Never assume any of this though: always state or switch to the directory you want to look at instead of relying on any particular default being consistently applied. Commented Dec 21, 2015 at 14:48
5

The reason you are getting two different directories is simple. The \ is the difference.

dir C:\ is asking for the directory listing of the C drive at its \. \ is the root folder.

dir C: is asking for the directory listing of where you are (or was last, if you changed drives)

If you're on the C drive and just type dir and hit enter, you will get the same contents of dir C:. If you're on a different drive, and type C: to change back to the C drive, then you'll end up in the directory that you saw when you typed dir C:.

0
0

As this is not StackOverflow, this should only be a comment, at best, but it doesn't fit:.

The .NET Framework (perhaps via Win32) does reflect the current directory for the current drive:

> (curdir)
C:\Utils\DotLisp\My Stuff
> (.FullName (DirectoryInfo. "C:"))
C:\Utils\DotLisp\My Stuff
> (.FullName (DirectoryInfo. "G:"))
G:\
> (set (curdir) "G:/Temp/Emails")
G:/Temp/Emails
> (curdir)
G:\Temp\Emails
> (.FullName (DirectoryInfo. "G:"))
G:\Temp\Emails
> (.FullName (DirectoryInfo. "C:"))
C:\
0

You are running two commands together. c: is itself a command for changing to the PREVIOUS LOCATION on the C: drive. So it would hold the location before you changed drives using d: or e:. dir is a command used to list the contents of a directory.

So basically you are listing content of your previous folder on the C: drive even though you are on another drive.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .