subst without parameters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
relro
Posts: 3
Joined: 21 Feb 2014 08:24

subst without parameters

#1 Post by relro » 21 Feb 2014 09:00

Hey people
I'm new here and I hope someone can/want help me with a problem using 'subst'

I use subst to mount a folders path to a drive-letter to find them more easliy...
Now I want to create a tool wich unmountes the virtual drives by entering the drive-letter in a console, but I don't have any idea how to get the letters allready mounted...

If I enter subst without parameters my wishes comes true, but how can I analyse them and use them in further functions?

Sometimes I mount a path and within this path I mount an other one.
Example of the result of Subst

Code: Select all

C:\users\me>subst

A:\: => C:\hello\there
B:\: => A:\there


I need this information because, if A:\ will be unmounted also B:\ should be.

I hope someone can help

Greets
relro

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: subst without parameters

#2 Post by foxidrive » 21 Feb 2014 09:11

I don't quite follow what you need to do. Can you explain a little more?

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: subst without parameters

#3 Post by carlos » 21 Feb 2014 09:23

edit: code edited almost 3 times.

Code: Select all

for /f "tokens=1,3,* delims=: " %a in ('subst') do (
echo %%a: %%c
rem uncomment for remove it
rem subst %a: /d
)


with it %%a have the drive letter and %%c the path.

relro
Posts: 3
Joined: 21 Feb 2014 08:24

Re: subst without parameters

#4 Post by relro » 21 Feb 2014 12:06

Hey foxidrive, hey carlos, nice to meet u and thanks for the reply...

@carlos
this is exactly what I'm looking for... :) great

here the .bat I wrote:

Code: Select all

@echo off
SETLOCAL

echo Virtual Drives:
echo ------------------------------------------
for /f "tokens=1,3,* delims=: " %%a in ('subst') do (
    echo %%a:\ ......... %%c
)
echo ------------------------------------------

set /p toRelease=release

call :release %toRelease:~0,1%
goto :eof


:release
SETLOCAL

set toRelease=%~1

subst %toRelease%: /D

for /f "tokens=1,3,* delims=: " %%a in ('subst') do (
    set FolderPath=%%c
    set DriveLetter=%%a
   
    if %toRelease% == %FolderPath:~0,1% (
        call :release %DriveLetter%
    )
)


it is working but it isn't so nice because the recursion will run the for loop more times that needed.
the 'subst %toRelease%: /D' will raise some errors because the virtual drive does not exists anymore.

Testing if the path exists does not work...

Code: Select all

if exist %toRelease%: (subst %toRelease%: /D)


maybe someone has an idea to solve this task in a cleaner way...

Greets
relro

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: subst without parameters

#5 Post by carlos » 21 Feb 2014 22:31

you can avoid the recursion problem not using recursion, that means replace call by goto and use a variable instead a parameter. Also, you have a mistake using normal expansion of variable inside the foor loop, because that expansion is done before the block is executed, then try to get the actual value inside the block is imposible using normal expansion, because you are not expanding that variable value in that point (cmd expand it before execute the block), for solve it use delayed expansion.

check this if works for you:

Code: Select all

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

:INI
echo Virtual Drives:
echo ------------------------------------------
for /f "tokens=1,3,* delims=: " %%a in ('subst') do (
    echo %%a:\ ......... %%c
)
echo ------------------------------------------

:INPUT
set "toRelease="
set /p "toRelease=release"
if not defined toRelease GOTO :EOF

set "toRelease=!toRelease:~0,1!"

:RELEASE

subst !toRelease!: /D

for /f "tokens=1,3,* delims=: " %%a in ('subst') do (
    set "FolderPath=%%c"
    set "DriveLetter=%%a"
   
    if "!toRelease!" == "!FolderPath:~0,1!" (
        set "toRelease=!DriveLetter!"
        GOTO :RELEASE
    )
)


using it maybe you not get the subst message that the volumen is not found, because you are really get the value of DriveLetter variable.

Anyways if you need check if the volumen is mounted (for example the user input is a letter of volumen not mounted), I remember that the way is using vol command like this:

using z as example:

Code: Select all

vol z: >nul 2>&1
if errorlevel 1 (set "exist=0") else (set "exist=1")

relro
Posts: 3
Joined: 21 Feb 2014 08:24

Re: subst without parameters

#6 Post by relro » 24 Feb 2014 03:58

Hey carlos, thanks a lot for ur help...
the way to avoid the recursion is not working at all, so I decided to keep the recursion.

I wrote/copied some stuff together to solve my task...


I wanted to attach all of it but I can't find any button for attachment... Where is it? ;) Wanna share the received help...
Greets
relro

Post Reply