4

Is there a way to get the current working directory of a drive other than the current drive?

Windows stores the current directory of every drive and %cd% gets the current working directory of the actual drive.

If I change the drive in a batch script I can get the current working directory of the actual drive:

cd w:\Downloads
cd c:\Windows
c:
(echo %cd%)
w:
(echo %cd%)

Is there a way to get the current working directory without changing the actual drive?

3
  • Your assumptions are wrong. In Windows there is only one current directory. For compatibility with MSDOS CMD.Exe pretends there are per drive current directories.
    – CatCat
    Commented Oct 31, 2018 at 9:39
  • 1
    @CatCat maybe is "current directory" not the best choice to name it. But AFAIK "the system remembers the last current path for each volume (drive letter)" (learn.microsoft.com/de-de/windows/desktop/FileIO/…) and the system stores those "last current paths" in those "strange variables" (blogs.msdn.microsoft.com/oldnewthing/20100506-00/?p=14133). See my answer below
    – wolfrevo
    Commented Nov 1, 2018 at 15:09
  • @CatCat: You're right. See update of my answer.
    – wolfrevo
    Commented Nov 21, 2018 at 16:54

1 Answer 1

6

After searching for an answer I found some hints on strange variables under https://blogs.msdn.microsoft.com/oldnewthing/20100506-00/?p=14133

use %=c:%) for the current directory of drive c or %=w:%) for w

Or in a batch script:

cd c:\Windows
cd w:\Downloads
(echo %=c:%)
(echo %=w:%)

UPDATE

As @RossRidge pointed out it is not Windows storing those strange variables but the DOS command processor. Those strange variables are only known in the current 'session'. The same holds for cd (see comment of @Stephan)

Opening two DOS command processors:

In the first DOS command processor:

cd c:\Windows
cd w:\Downloads
(echo %=c:%)
    -> c:\Windows
(echo %=w:%)
    -> w:\Downloads
cd w:
    -> w:\Downloads

In the second DOS command processor:

(echo %=w:%)
    -> %=w:%       <- this variable is not set, not known
cd w:
    -> w:\

Notice the inconsistent way to hold the 'current directory': cd returns the root directory after entering a DOS command processor. But %=w:% is not set until the directory changes!

4
  • 6
    You'll also notice that Windows doesn't actually store the current directory of every drive: "Win32 does not have the concept of a separate current directory for each drive, but the command processor wanted to preserve the old MS-DOS behavior because people were accustomed to it (and batch files relied upon it). The solution was to store this "per-drive current directory" in the environment, using a weird-o environment variable name so it wouldn't conflict with normal environment variables."
    – Ross Ridge
    Commented Oct 31, 2018 at 8:04
  • @RossRidge: Thanks for your comment. See the update of my answer.
    – wolfrevo
    Commented Nov 21, 2018 at 10:48
  • if you just want to know it, also cd w: tells you. (But in a batch script, those varables are surely more helpful.)
    – Stephan
    Commented Nov 21, 2018 at 12:37
  • @Stephan: Thanks for the hint. See update of my answer.
    – wolfrevo
    Commented Nov 21, 2018 at 16:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.