# Solved: diskpart utility question



## scrfix (May 3, 2009)

I am checking for utilized drive letters on a computer. I know this can be done because HP does it with their printers however i keep running into an error.

*VISTA*
The command is:
*diskpart*
Wait for about 2-3 seconds
then *list volume*

This is great when I am doing this directly from the command line howeve when I attempt to batch it, the program hangs every time.

What am I doing wrong? Do I have to utilize Delayed Expansion or something?


```
@echo off
diskpart
delay 10
list volume
pause
```


----------



## Squashman (Apr 4, 2003)

I believe you would need to do it as a diskpart script.
Inside your batch file you would do this.

diskpart /s myscript.txt

Put all your diskpart commands in the myscript.txt file and end it with the exit command.


----------



## TheOutcaste (Aug 8, 2007)

Squashman said:


> I believe you would need to do it as a diskpart script.
> Inside your batch file you would do this.
> 
> diskpart /s myscript.txt
> ...


Correct, diskpart starts it's own interface, so your *delay 10* command won't even be run until diskpart exits. And unlike netsh or wmic, it won't accept commands on the command line; you have to use a script.
Dispart will not show mapped drives, so you would need to run the net use command to check for those, or you can use wmic to get them all:
*wmic logicaldisk get DeviceID,DriveType*
This will list all drive letters and the type of drive:


Undetermined
Removable
Hard Drive
Network Drive
CD-ROM
RAM Disk
Keep in mind that Mapped Drives are user specific, so in Vista, if you run wmic from an Admin Prompt, you will only see drives mapped using the Administrator account, not drives mapped for the current user.
Net Use on Vista will show drives from other user accounts, on XP it won't.

Jerry


----------



## scrfix (May 3, 2009)

Jerry,

That is brilliant. I had no clue that WMIC could do that. I attempted it on my XP box and it stated it had to install it. After it installed, it worked absolutely wonderfully.

Is there a way to check to see if it is already installed and if it is not installed, then install it before I run the command.


----------



## TheOutcaste (Aug 8, 2007)

Should be able to just run the command, it will install automatically. It does mess with the output a bit, but if you skip the first line it shouldn't matter.

I ran the following line in a batch file on a XP system that doesn't have WMIC installed, the output is below:

```
For /F "tokens=*" %%I In ('wmic logicaldisk get DeviceID^,DriveType') Do @Echo %%I
```
Note that the comma has to be escaped.

```
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>test.cmd

C:\>For /F "tokens=*" %I In ('wmic logicaldisk get DeviceID,DriveType') Do @Echo %I
Please wait while WMIC is being installed.DeviceID  DriveType
A:        2
C:        3
D:        5

C:\>
```
It just sat for a few seconds while it was installing, then completed just fine. Skipping the first line should work whether it's installed or not. If you need the column headers, you can use substring replacement to remove the install message, however, I have seen it list a different message:
Please wait while WMIC compiles updated MOF files.DeviceID DriveType
This happened when I ended the batch before WMIC finished installing. There could be other messages as well, and it's also possible that it will fail to install; I remember seeing that happen on a system that had a lot of services that refused to start.

You could also just run a *WMIC /?>Nul* command which would install it if not already installed.


----------



## scrfix (May 3, 2009)

Jerry,

Off the subject. Does the %I mean something in a FOR statement or can I use %F.

Why is there only 1 % vs 2 %'s?


----------



## TheOutcaste (Aug 8, 2007)

scrfix said:


> Jerry,
> 
> Off the subject. Does the %I mean something in a FOR statement or can I use %F.
> 
> Why is there only 1 % vs 2 %'s?


You can use any single character for the loop variable. Help says A-Z and a-z, but you can use punctuation and numbers. That can get tricky if you need more than 1, as you need to know the proper order. Numbers could be confused for batch parameters instead of a loop variable. Special characters like ", &, <, and > need to be escaped. ! may work if delayed expansion is disabled, but might not work if it's enabled, or may need to be escaped. I haven't tried using % yet, but I imagine it would be a pain.

So for example, instead of A and B, you can use Z and [.
For loops in BASIC (similar to For /L) were often used to access an array element, so the the letter I (for Index) was often used, so I tend to use I in loops a lot.

You only use two %'s in a batch file. At the command prompt, you only need one, and if Echo is ON, you'll see that it only displays one % when executing the line in the batch file. The above is the output from the batch file, so there is only one %. You'll notice the ^ wasn't displayed either.


----------



## scrfix (May 3, 2009)

Thanks,

So in your batch you really had %%I and you had ^,

Is that correct?


----------



## TheOutcaste (Aug 8, 2007)

Yes, the first code block is the line in the file:


TheOutcaste said:


> ```
> For /F "tokens=*" [COLOR=Red]%%I[/COLOR] In ('wmic logicaldisk get DeviceID[COLOR=Red]^,[/COLOR]DriveType') Do @Echo [COLOR=Red]%%I[/COLOR]
> ```


----------

