Querying default drive and current working directory

  • Thread starter Thread starter Rich Pasco
  • Start date Start date
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
 
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
 
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%
 
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
 
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
 
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
 
Back
Top