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
Solved

VBS Format multiple USB drives

Posted on 2013-06-24
2
537 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
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add and format columns in vb6 7 63
Compile Error 7 61
excel 2016 program to loop through scripts to apply filename 2 25
batch file or script 4 25
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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