[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

vbscript to format USB drive and set label

Posted on 2013-05-20
13
Medium Priority
?
2,089 Views
Last Modified: 2013-05-21
Hi,

I need a simple vbs to format and set the drive label of a usb. I found this but it does not seem to do anything on Win8. Any suggestions please?
Tom

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)
objVolume.Label = "MyLabel"
Next

Open in new window

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
  • 6
  • 5
  • 2
13 Comments
 
LVL 18

Expert Comment

by:x-men
ID: 39180606
the script assumes drive E: for the usb. is this correct?
0
 

Author Comment

by:tomdenton
ID: 39180621
Yes but that can be changed to anything as needed
0
 
LVL 18

Expert Comment

by:x-men
ID: 39180630
what's returne on the errResult variable?
0
Independent Software Vendors: 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!

 

Author Comment

by:tomdenton
ID: 39180648
nothing seems to happen at all format wise and the labels is not changed either
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39180762
Try removing the double "\"...with this adjustment:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colVolumes = objWMIService.ExecQuery _
("Select * from Win32_Volume Where DriveType=3)

For Each objVolume in colVolumes
  If objVolume.Name = "E:\" Then  'Filters for ONLY drive E:
    errResult = objVolume.Format("NTFS", true)
    objVolume.Label = "MyLabel"
  End If
Next

Open in new window

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39180780
Also - are you running this in an elevated environment? (as Administrator)
0
 

Author Comment

by:tomdenton
ID: 39181425
yes i am full admin - (out now will test later)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39181466
But from an elevated environment?
For instance, launching your app using
cscript FormatUSB.vbs

Open in new window

in a command prompt...does the command window state that it's an Administrative environment?

See: http://www.eightforums.com/tutorials/2754-elevated-command-prompt-open-windows-8-a.html
0
 

Author Comment

by:tomdenton
ID: 39182623
Since the suggested change i get an 'unterminated string constant' error on line 6

and the same if run as cscript... from a cmd window

the command window does not specifically stat admin environment. I can however run lots of other scripts without a problem if that helps?!
0
 

Author Comment

by:tomdenton
ID: 39182651
i went back to my original script and ran that from an admin cmd and it ran and formatted the drive ok but did not rename its label

Will it be possible to make this work from a non admin point of view though?
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 total points
ID: 39183882
Don't know of a way to do it without elevation.  You could create a shortcut and mark that to only run as admin, but you'll get that pop up asking you to confirm it each time.

Here, try this adjustment with the elevated cmd:
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,,"My Label")
Next

Open in new window

0
 

Author Closing Comment

by:tomdenton
ID: 39183892
This works fine if 'elevated'. The other option is i can compile it using exescript then right click and run as admin.

Thanks for your help
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39184028
Happy to help - thanx for the grade! :^)
0

Featured Post

Technology Partners: 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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

656 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