VBScript: How do I run a command that contains a object or variable?

I am trying to write a script that will help me to copy a large number of USB Disks.  I have most of the code written but having trouble w/ a small section.  I need to run a command that uses one of output from a WMI query.  I have option explicit turned on and have "Dim'd" all necessary variables.

      formatUSB = "cmd /c format " & objItem.DeviceID, "/FS:NTFS /Q"
      WScript.Echo "Formatting USB Drive..."
      Set objShell = CreateObject ("WScript.Shell")
      Set objExec2 = objShell.Exec (formatUSB)

When I run this script, I get Expected ')" or Expected end of statement.  Is this possible to do?
LVL 1
jleleuxAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
sirbountyConnect With a Mentor Commented:
Try this - unless there's more to it, there was some code that was either redundant or unnecessary...
Option Explicit
'On Error Resume Next
 
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const DriveType = 2
Dim objExec, objDrive, strCommand, strDrive, choice
 
Dim objShell : Set objShell = CreateObject ("WScript.Shell")
Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Dim colItems : Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk where DriveType =" & DriveType, _
                                         "WQL",wbemFlagReturnImmediately + wbemFlagForwardOnly)
 
DevConCopy 'no need to repeat per loop?
 
Dim objItem      
Do 
  For Each objItem In colItems
    WScript.Echo "DeviceID: " & objItem.DeviceID
    WScript.Echo "VolumeName: " & objItem.VolumeName
    FormatDrive (objItem.DeviceID)
  Next
 
  ' Asks the user if they want to copy another drive or exit the script.
  WScript.StdOut.Write "Do you want to copy another drive? (y/n)"
  choice = WScript.StdIn.ReadLine
Loop While choice = "y" Or choice = "Y"
 
Sub FormatDrive (strDrive)  ' Format Drive
  strCommand = "cmd /c format " & strDrive & " /FS:NTFS /Q"
  WScript.Echo "Formatting USB Drive..."
  objShell.Run strCommand
End Sub
 
Sub DevConCopy()  ' Copy Microsoft DevCon utility
  strCommand = "cmd /c xcopy P:\Software\Miscellaneous\devcon.exe c:\windows\system32 /y"
  WScript.Echo "Copying needed utility..."
  objShell.Run strCommand
End Sub

Open in new window

0
 
sirbountyCommented:
remove the comma (,) after DeviceID
0
 
sirbountyCommented:

formatUSB = "cmd /c format " & objItem.DeviceID " /FS:NTFS /Q"
      WScript.Echo "Formatting USB Drive..."
      Set objShell = CreateObject ("WScript.Shell")
      Set objExec2 = objShell.Exec (formatUSB)

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Shift-3Commented:
You'll need another ampersand in there too.
 

formatUSB = "cmd /c format " & objItem.DeviceID & " /FS:NTFS /Q"

Open in new window

0
 
jleleuxAuthor Commented:
Now I get the following error:

Microsoft VBScript runtime error: Object required: ''
0
 
sirbountyCommented:
try this - if the error persists, please indicate which line throws the error.
formatUSB = "cmd /c format " & objItem.DeviceID & " /FS:NTFS /Q"
WScript.Echo "Formatting USB Drive..."
Set objShell = CreateObject ("WScript.Shell")
Set objExec2 = objShell.Exec (formatUSB)

Open in new window

0
 
jleleuxAuthor Commented:
Here is the whole program, this might be more helpful.  The error is happening on line 38, col 2.

Option Explicit
'On Error Resume Next
Dim strComputer
Dim objWMIService
Dim colItems
Dim objItem
Dim copyDev
Dim formatUSB
Dim choice
Dim objShell
Dim objExec1
Dim objExec2

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const DriveType = 2

strComputer = "."
   WScript.Echo
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk where DriveType =" & DriveType, _
                                         "WQL",wbemFlagReturnImmediately + wbemFlagForwardOnly)
     
Do
      For Each objItem In colItems
          WScript.Echo "DeviceID: " & objItem.DeviceID
          WScript.Echo "VolumeName: " & objItem.VolumeName
      'If objItem.VolumeName Or objItem.DeviceID    
      Next
      
      ' Copy Microsoft DevCon utility
      copyDev = "cmd /c xcopy P:\Software\Miscellaneous\devcon.exe c:\windows\system32 /y"
      WScript.Echo "Copying needed utility..."
      Set objShell = CreateObject ("WScript.Shell")
      Set objExec1 = objShell.Exec (copyDev)
      
      ' Format Drive
      formatUSB = "cmd /c format " & objItem.DeviceID & " /FS:NTFS /Q"
      WScript.Echo "Formatting USB Drive..."
      Set objShell = CreateObject ("WScript.Shell")
      Set objExec2 = objShell.Exec (formatUSB)
      
      
      ' Asks the user if they want to copy another drive or exit the script.
      WScript.StdOut.Write "Do you want to copy another drive? (y/n)"
      choice = WScript.StdIn.ReadLine
Loop While choice = "y" Or choice = "Y"
0
 
sirbountyCommented:
yep, it's your objitem - I'll clean up the script for you...
0
 
jleleuxAuthor Commented:
Thanks all for helping a noob scripter!!!!  I really appreciate!!!!
0
 
sirbountyCommented:
Glad I could lend a hand - thanx for the grade! :^)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.