Alternative to FOR EACH

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: J. van Heerde (Han_at_b-v-h.demon.nl)
Date: 04/16/04


Date: Fri, 16 Apr 2004 17:19:57 GMT

Hi Scripting Guys,

I've been reading a lot of information on VBScript, WMI, WSH, but
can't find a solution to my problem.. I'm not only using all examples
for VBScript, but also for another programming language. In the
Windows 2000 Scripting Guide I came across the following example:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
For Each objDrive in colDrives
 Wscript.Echo "Drive letter: " & objDrive.DriveLetter
Next

This works fine in VBScript.

I have one big problem. The programming language I use doesn't support
the FOR EACH .... IN .... command. With other examples I've always
managed to use the .Count property to set up a FOR ... TO .... command
in combination with the .Item() method. I've been digging the
Internet, but can't find any method for solving my problem.

How do I reference one specific object in the Drives collection in the
FileSystemObject by index number?

The following command does work, but is of no use, because it presumes
I already know the drive letter.

Wscript.Echo "Drive letter: " & objDrive.Item( "a:" ).DriveLetter

The following code simply doesn't work.....

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
For counter = 1 TO colDrives.Count
 Wscript.Echo "Drive letter: " & colDrives.Item( counter ).DriveLetter
Next

I also tried using the dictionary object, but didn't know how to
transfer this collection to a dictionary collection.......(again by
NOT using the FOR EACH command).

Can anybody help?

Kind regards,
Han



Relevant Pages

  • Alternative to FOR EACH
    ... I'm not only using all examples for VBScript, ... Set colDrives = objFSO.Drives ... The following command does work, but is of no use, because it presumes I ... I also tried using the dictionary object, but didn't know how to transfer ...
    (microsoft.public.scripting.vbscript)
  • Re: Need WMI script
    ... Error handling in VBScript is obviously not as good as in .NET. ... normal error handling with "On Error GoTo 0". ... Dim adoCommand, adoConnection, strBase, strFilter, strAttributes, xmlDoc, ... you can use a command similar to below at a command ...
    (microsoft.public.windows.server.scripting)
  • Re: Need WMI script
    ... How to handle the errors in vbscript. ... Dim strDomainName, strNodeText, strXmlBuilder ... However, if you use the cscript host, ... you can use a command similar to below at a command prompt: ...
    (microsoft.public.windows.server.scripting)
  • Re: wsharguments length limit?
    ... pasted it into a batch file, changed the path to the VBScript to one on my ... When I pasted your command I noted there were spaces between each argument, ... Could SAS be ... Set objXL = WScript.CreateObject ...
    (microsoft.public.scripting.vbscript)
  • Re: system.Diagnostics.Process Problem
    ... Dim consoleApp As New Process ... Loop Until consoleApp.MainWindowTitle.ToString.EndsWith ... command sent to the cmd.exe) but I am not clear on your response. ... then attempt to read the output from the VBScript using ...
    (microsoft.public.dotnet.general)