Jezreel Martin
Jezreel Martin

Reputation: 89

How to change to directory to your homedrive?(C:\, D:\, etc..)

I am writing this batch file which searches for specific a specific file. Now every time the code cd /D "%HOMEDRIVE%" is executed, it of course starts to search in that directory. But what I get is file not found.

I tried doing cd /D "%HOMEDRIVE% in the command line but it only replies where the cmd is run (e.g C:\Users\onlYUs)

How do I fix this? There is an environment variable named HOMEDRIVE whose value is C:. But it does not change to that directory. And by the way the reason why I needed that because if an instance that your homedrive is set to D: or E: it can still search for the file. Any help would be greatly appreciated!

Upvotes: 2

Views: 1546

Answers (2)

Ken White
Ken White

Reputation: 125718

You can't change to a directory without providing a path to a directory, and %HOMEDRIVE% only contains a drive letter. Without the backslash, it's the equivalent of typing C: at the command prompt, which only changes the drive.

You need to add the trailing path separator (backslash) to make it a directory path instead, because you're wanting to change to the root directory of that drive.

This does not work:

cd /D %HOMEDRIVE%

This does work (note the trailing backslash):

cd /D %HOMEDRIVE%\

Upvotes: 3

dxiv
dxiv

Reputation: 17658

An alternative way is pushd %HOMEDRIVE%\ which allows the batch to later popd back to the drive and directory that were initially current.

Upvotes: 1

Related Questions