Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use for...to.. next in stead of for each..next

Status
Not open for further replies.

sk2107c

Programmer
May 2, 2017
22
JO
hello everybody,
I would like to know how to use for...to.. next in stead of for each..next
for example :
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objDrive In objFSO.Drives
  WScript.Echo objDrive.DriveLetter
Next

I want the same result but with using For..To..Next
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
nn = objFSO.Drives.Count
For i=1 to nn
  WScript.Echo .............  ' I want to get same result obove
Next

thank you all
 
You can't natively, but you could always use the "For Each ... In ..." loop to create an array if you really needed it in an array. What are you trying to achieve?
 
thank you for your quick response.. that's exactly the point that I want to use it as an array to use it for other purposes in other parts of my application.

 
this is what I used to fill the array, but is there any faster or better way to do this?

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")

nn = objFSO.Drives.Count
Dim arr()
ReDim arr(nn)
x=0
For Each objDrive In objFSO.Drives
	x=x+1
        arr(x)= objDrive.DriveLetter
 Next
 
Both of which reiterate that you need to load an array ...

The problem here is that the item Index used for the FileSystemObject's Files collection is the filename, not a number, so we cannot use for ... next with it ...

But here's a question: do you have a good reason why you want to use For Next, or is it just a personal preference?
 
Actually I am collecting data from different locations with different types of data and at the end I do some calculations and statistics then I flush the data according to user choice either to text file,excel file or SQL database, in fact I can output them to a temp text file and reuse it after choice but I am trying to avoid writing the data anywhere before selection for security reasons and for more speed in execution as writing to memory is faster than writing to storage.

thank you

 
thank you guitarzan for the links


regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top