Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Querying default drive and current working directory

0 views
Skip to first unread message

Rich Pasco

unread,
Sep 15, 2009, 7:23:55 PM9/15/09
to
What are the best ways for a shell script (CMD or BAT file) to query:
(a) the current default drive?
(b) the current working directory on a specified drive?

The best ways I can think of:

To query the default drive, issue the "CD" command with no
parameters, and capture the first two characters of its output
(drive letter and colon), e.g.

To query the current working directory on a specified drive,
issue the CD command with that drive as its parameter (e.g.
CD D:) and then capture its output.

Can anyone suggest a cleaner way?

- Rich

foxidrive

unread,
Sep 15, 2009, 7:41:50 PM9/15/09
to
On Tue, 15 Sep 2009 16:23:55 -0700, Rich Pasco <rich...@hotmail.com>
wrote:

Your techniques seem reasonable.

@echo off
for /f "delims=" %%a in ("%cd%") do set "cntdrv=%%~da"
for /f "delims=" %%a in ('cd d:') do set "Ddir=%%~pnxa"
echo "%cntdrv%" "%ddir%"
pause


Pegasus [MVP]

unread,
Sep 16, 2009, 1:57:11 AM9/16/09
to

"Rich Pasco" <rich...@hotmail.com> wrote in message
news:%232lPcul...@TK2MSFTNGP06.phx.gbl...

Here is another way:
@echo off
echo The current drive is %cd:~0,2%
echo The current directory is %cd:~2%


Rich Pasco

unread,
Sep 16, 2009, 2:24:11 AM9/16/09
to Pegasus [MVP]
Pegasus [MVP] wrote:

Your cleaner way to find the current drive seems to be the one
I proposed (albeit a concise expression of it). Your statement
of the current directory shows only the current directory of the
default drive, whereas I asked for a way to find the current
directory of any specified drive. I was not able to make your
syntax work on a non-default drive, like this:

echo The current directory of drive e: is %cd e:%

- Rich

Pegasus [MVP]

unread,
Sep 16, 2009, 3:56:46 AM9/16/09
to

"Rich Pasco" <rich...@hotmail.com> wrote in message
news:4AB0848...@hotmail.com...

If you want the current directory of the specified drive then foxidrive's
method is the simplest way to go. A more involved method goes like this:
@echo off
pushd "%1"
echo Drive=%cd:~0,2%, Folder=%cd:~2%
popd


Rich Pasco

unread,
Sep 16, 2009, 11:37:43 AM9/16/09
to

Wonderful. Thanks to both of you for all your help.

- Rich

0 new messages