How to get current working directory of another drive in Batch?












0















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?










share|improve this question

























  • 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
    Oct 31 '18 at 9:39











  • @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)" (docs.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
    Nov 1 '18 at 15:09











  • @CatCat: You're right. See update of my answer.

    – wolfrevo
    Nov 21 '18 at 16:54
















0















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?










share|improve this question

























  • 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
    Oct 31 '18 at 9:39











  • @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)" (docs.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
    Nov 1 '18 at 15:09











  • @CatCat: You're right. See update of my answer.

    – wolfrevo
    Nov 21 '18 at 16:54














0












0








0








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?










share|improve this question
















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?







windows batch-file cmd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 31 '18 at 8:00









Ross Ridge

27.8k54682




27.8k54682










asked Oct 31 '18 at 7:44









wolfrevowolfrevo

2,99121224




2,99121224













  • 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
    Oct 31 '18 at 9:39











  • @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)" (docs.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
    Nov 1 '18 at 15:09











  • @CatCat: You're right. See update of my answer.

    – wolfrevo
    Nov 21 '18 at 16:54



















  • 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
    Oct 31 '18 at 9:39











  • @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)" (docs.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
    Nov 1 '18 at 15:09











  • @CatCat: You're right. See update of my answer.

    – wolfrevo
    Nov 21 '18 at 16:54

















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
Oct 31 '18 at 9:39





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
Oct 31 '18 at 9:39













@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)" (docs.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
Nov 1 '18 at 15:09





@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)" (docs.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
Nov 1 '18 at 15:09













@CatCat: You're right. See update of my answer.

– wolfrevo
Nov 21 '18 at 16:54





@CatCat: You're right. See update of my answer.

– wolfrevo
Nov 21 '18 at 16:54












1 Answer
1






active

oldest

votes


















2














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!






share|improve this answer





















  • 2





    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
    Oct 31 '18 at 8:04











  • @RossRidge: Thanks for your comment. See the update of my answer.

    – wolfrevo
    Nov 21 '18 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
    Nov 21 '18 at 12:37











  • @Stephan: Thanks for the hint. See update of my answer.

    – wolfrevo
    Nov 21 '18 at 16:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53078528%2fhow-to-get-current-working-directory-of-another-drive-in-batch%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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!






share|improve this answer





















  • 2





    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
    Oct 31 '18 at 8:04











  • @RossRidge: Thanks for your comment. See the update of my answer.

    – wolfrevo
    Nov 21 '18 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
    Nov 21 '18 at 12:37











  • @Stephan: Thanks for the hint. See update of my answer.

    – wolfrevo
    Nov 21 '18 at 16:53
















2














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!






share|improve this answer





















  • 2





    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
    Oct 31 '18 at 8:04











  • @RossRidge: Thanks for your comment. See the update of my answer.

    – wolfrevo
    Nov 21 '18 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
    Nov 21 '18 at 12:37











  • @Stephan: Thanks for the hint. See update of my answer.

    – wolfrevo
    Nov 21 '18 at 16:53














2












2








2







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!






share|improve this answer















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!







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 16:58

























answered Oct 31 '18 at 7:59









wolfrevowolfrevo

2,99121224




2,99121224








  • 2





    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
    Oct 31 '18 at 8:04











  • @RossRidge: Thanks for your comment. See the update of my answer.

    – wolfrevo
    Nov 21 '18 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
    Nov 21 '18 at 12:37











  • @Stephan: Thanks for the hint. See update of my answer.

    – wolfrevo
    Nov 21 '18 at 16:53














  • 2





    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
    Oct 31 '18 at 8:04











  • @RossRidge: Thanks for your comment. See the update of my answer.

    – wolfrevo
    Nov 21 '18 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
    Nov 21 '18 at 12:37











  • @Stephan: Thanks for the hint. See update of my answer.

    – wolfrevo
    Nov 21 '18 at 16:53








2




2





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
Oct 31 '18 at 8:04





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
Oct 31 '18 at 8:04













@RossRidge: Thanks for your comment. See the update of my answer.

– wolfrevo
Nov 21 '18 at 10:48





@RossRidge: Thanks for your comment. See the update of my answer.

– wolfrevo
Nov 21 '18 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
Nov 21 '18 at 12:37





if you just want to know it, also cd w: tells you. (But in a batch script, those varables are surely more helpful.)

– Stephan
Nov 21 '18 at 12:37













@Stephan: Thanks for the hint. See update of my answer.

– wolfrevo
Nov 21 '18 at 16:53





@Stephan: Thanks for the hint. See update of my answer.

– wolfrevo
Nov 21 '18 at 16:53




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53078528%2fhow-to-get-current-working-directory-of-another-drive-in-batch%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini