How can one recode

subst P: /D
subst P: D:\mydir

such that a second run does not delete the drive needlessly i.e. if the drive is already subst-ed to this path?

The momentary removal of the drive interferes with an Explorer view that I have watching that drive.

  • Is it possible P: could be connected to a different path or is it just enough to check if P is connected (to any random path) and if not connect it to D:\mydir ?? The latter would be easy to do with an IF NOT EXIST P:\NUL subst P: D:\mydir – Rik Aug 10 '14 at 10:45
  • @Rik, thanks for the thought but I do need the full check. Leaving P: on a different path would be disastrous. – ChrisJJ Aug 10 '14 at 13:46

Ok, so you definitely need the full check that P: get redirected to D:\mydir

You could do it like this (save as a batch-file):

@echo off
subst | findstr /C:"P:\\: => D:\\MYDIR" 1> nul
if errorlevel 1 (
  echo. P-drive not mapped to D:\mydir
  echo. remapping P:\
  subst P: /D 1> nul
  subst P: D:\mydir
) else (
  echo. P-drive already mapped to D:\mydir

It checks (with command subst and findstr) if the P: is already mapped to D:\mydir.
(please note the double \ in the check with findstr)
(also please note that subst always returns the path in all caps)

  • if P:\: => D:\MYDIR is not present in subst then we need to remap.
  • The subst P: /D 1> nul deletes the current P:
    (and suppresses any error message in case P: was not mapped)
  • Then with subst P: D:\mydir we map the drive
    (no error message should appear as we just deleted any P-mapping)
  • You could delete the echo-lines if you don't want them

Please check your output of subst to see if it corresponds with the format i used here. So:
P:\: => D:\MYDIR
(if not, please adjust the batch-file accordingly)

Here is a revised (and parameterized) version. You can call it like remap P: D:\mydir. The new version is case-insensitive. It adds the double \ to the findstr parameters. It checks beforehand if the directory exists (it may not contain a trailing slash).

@echo off
if "%2"=="" (
  echo. Call with: %0 drive: destination-direcory
if not exist "%2\." (
  echo. The destination directory does not exist
SET drive=%1\\
SET dest=%2
SET dest=%dest:\=\\%
subst | findstr /I /R /C:"^%drive%: => %dest%$" 1> nul
if errorlevel 1 (
  echo. %1-drive not mapped to %2
  echo. remapping %1
  subst %1 /D 1> nul
  subst %1 %2
) ELSE (
  echo. %1-drive already mapped to %2
  • Thanks. Yes, the output is the same here. I will need this as a subroutine callable with parameters P: and D:\mydir. How may I translate D:\mydir to D:\\mydir? – ChrisJJ Aug 10 '14 at 21:23
  • @ChrisJJ I edited my answer with a parameterized batch-file. It will add the double \ for you. You can use it like remap p: d:\mydir. (it's case insensitive now.) See the Edit in my answer. – Rik Aug 10 '14 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.