saving drive names into variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

saving drive names into variable

#1 Post by tcpman » 30 Jun 2014 12:39

i am looking for a way to save drive names into var

we can get the drive names using this
for %a in ( B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO @if exist %i: @echo %a:
now if the output is for exmaple
c
d
e
n
i want to do something like this
set drive1=C
set drive2=d
...
i came with one way
i can save the output in a text file and then using findstr serach for names and if find one save it in a var
but i want to know is there any better way?

also if we use a command like dir
can we serach for string without saveing the output in a text file?
i think i must use for /f perhaps?

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: saving drive names into variable

#2 Post by Compo » 30 Jun 2014 15:19

This should set the drives accordingly.

Code: Select all

@Echo Off & SetLocal EnableDelayedExpansion
Set _DRVNUM=0
For /f "Delims=: " %%A In ('Mountvol^|Find ":\"') Do (Set/a _DRVNUM +=1
   Set Drive!_DRVNUM!=%%A)
Set Drive
Ping -n 6 127.0.0.1 1>Nul

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: saving drive names into variable

#3 Post by Yury » 02 Jul 2014 11:03

Code: Select all

@echo off
for /f "tokens=2 delims==:" %%i in ('
 wmic logicaldisk get Name /value
') do (
 set %%i=%%i
 call set %%%%i%%| findstr /x %%i=%%i
 )
pause>nul
exit /b


???

Post Reply