Solved

VBS Format multiple USB drives

Posted on 2013-06-24
2
539 Views
Last Modified: 2013-06-24
Hi, I found this code on EE the lists the USB drives on a computer:
bolDeviceFound = False 
strComputer = "."

'query the WMI service for the computer name
 
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
 
Set colItems = objWMIService.ExecQuery ("SELECT * from Win32_LogicalDisk WHERE Description = 'Removable Disk'")
 
For Each objItem in colItems
	WScript.Echo "The computer has USB drive letter: " & objItem.Name
	bolDeviceFound = True
Next
 
If bolDeviceFound = False Then
	WScript.Echo "NO USB devices found"
End If

Open in new window

And i have this that will format and name a USB  (E: in this case):
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
   
Set colVolumes = objWMIService.ExecQuery _
("Select * from Win32_Volume Where Name = 'E:\\'")

For Each objVolume in colVolumes
  errResult = objVolume.Format("NTFS", true,,"ATT")
Next

Open in new window

I get errors when i try to combine them - what i would like to do is combine them so the script loops through the USB drives and formats and names them all.

I may be coming at this the wrong way so any suggestions please?

PS I compile the script and 'run it as admin'.
0
Comment
Question by:tomdenton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39270974
try this:
bolDeviceFound = False 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
   
Set colItems = objWMIService.ExecQuery ("SELECT * from Win32_LogicalDisk WHERE Description = 'Removable Disk'")
 
For Each objItem in colItems
	bolDeviceFound = True
	
	dim driveLetter = objItem.Name
	WScript.Echo "The computer has USB drive letter: " & driveLetter
	
	Set colVolumes = objWMIService.ExecQuery ("Select * from Win32_Volume Where Name = '" & driveLetter & ":\\'")

	For Each objVolume in colVolumes
	  errResult = objVolume.Format("NTFS", true,,"ATT")
	Next
Next
 
If bolDeviceFound = False Then
	WScript.Echo "NO USB devices found"
End If

Open in new window

0
 

Author Closing Comment

by:tomdenton
ID: 39271078
Great job, thanks :)

I had to add 'dim driveLetter' separately and remove dim from line 11 at the start and also we had an extra ':' on line 14 but when I deleted that it works great

thanks again
Tom

Final version in case it helps others:

dim driveLetter
bolDeviceFound = False
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("SELECT * from Win32_LogicalDisk WHERE Description = 'Removable Disk'")
For Each objItem in colItems
      bolDeviceFound = True
      driveLetter = objItem.Name
      WScript.Echo "The computer has USB drive letter: " & driveLetter
      Set colVolumes = objWMIService.ExecQuery ("Select * from Win32_Volume Where Name = '" & driveLetter & "\\'")
      For Each objVolume in colVolumes
        errResult = objVolume.Format("NTFS", true,,"ATT")
      Next
Next
If bolDeviceFound = False Then
      WScript.Echo "NO USB devices found"
End If
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question