Querying default drive and current working directory

R

Rich Pasco

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
 
F

foxidrive

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

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
 
P

Pegasus [MVP]

Rich Pasco said:
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

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

Rich Pasco

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

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
 
P

Pegasus [MVP]

Rich Pasco said:
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

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
 
R

Rich Pasco

Pegasus said:
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

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

- Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top