Listing all drives and making user select one

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Listing all drives and making user select one

#1 Post by Rileyh » 12 Oct 2011 01:37

Hi,
I want the person to choose which drive to make a file in. It lists the drives (my previous question!), they choose one, and then the batch sets their answer as a variable. Then, it changes to the directory they inputted and makes the file there.

Could you tell me how to do so?

Thanks for the help,
Rileyh

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Listing all drives and making user select one

#2 Post by aGerman » 13 Oct 2011 10:12

You could use the WMIC command.

Code: Select all

@echo off

setlocal enabledelayedexpansion
set /a n=0
echo Number   Drive Name
for /f "tokens=2 delims==" %%i in ('wmic logicaldisk get name /value') do (
  set /a n+=1
  echo !n!        %%i
  set "d_!n!=%%i"
)
:loop
echo(
set "num="
set /p "num=Choose a number: "
if defined d_%num% (
  set "drive=!d_%num%!"
) else (
  echo This drive doesn't exist^^!
  goto :loop
)
endlocal &set "drive=%drive%"

echo(
echo You have chosen %drive%.
pause


Regards
aGerman

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: Listing all drives and making user select one

#3 Post by Rileyh » 13 Oct 2011 22:39

aGerman,
Thank you for the code. Now I am going to spend a few hours understanding what you have posted! :D
Could you tell me how to, at the end of the code you posted, change directory to the variable "drive"?
Thanks again for the code,
Rileyh

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Listing all drives and making user select one

#4 Post by Ed Dyreen » 14 Oct 2011 11:50

'
permanent:

Code: Select all

cd %Drive%
using the stack:

Code: Select all

pushd "%Drive%\"
popd

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: Listing all drives and making user select one

#5 Post by Rileyh » 14 Oct 2011 22:38

@Ed Dyreen,
Thanks for the post. It worked well. What do you by "the stack" (please excuse my poor batch skills!)

Regards,
Rileyh

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Listing all drives and making user select one

#6 Post by Ed Dyreen » 14 Oct 2011 22:54

'
Image
In computer science, a stack is a last in, first out (LIFO) abstract data type and data structure. A stack can have any abstract data type as an element, but is characterized by only three fundamental operations: push, pop and stack top. The push operation adds a new item to the top of the stack, or initializes the stack if it is empty. If the stack is full and does not contain enough space to accept the given item, the stack is then considered to be in an overflow state. The pop operation removes an item from the top of the stack. A pop either reveals previously concealed items, or results in an empty stack, but if the stack is empty then it goes into underflow state (It means no items are present in stack to be removed). The stack top operation gets the data from the top-most position and returns it to the user without deleting it. The same underflow state can also occur in stack top operation if stack is empty.

http://en.wikipedia.org/wiki/Stack_%28data_structure%29

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: Listing all drives and making user select one

#7 Post by Rileyh » 14 Oct 2011 23:16

@Ed Dyreen,
Thanks for the example.

Rileyh

Post Reply