Announcement

Collapse
No announcement yet.

Reading from text file to array

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reading from text file to array

    I've got the following script. It should take a text file with user DNs in it, and read those into a collection, then change the manager attribute of each DN to a given value.

    If I echo during the Do loop, it shows each line. If I echo during my For Each loop, it tells me I don't have a collection. Why not and how do I get one?

    Code:
    Const ForReading = 1
     
    strFile = "PathToTextFile"
    strManagerDN = "CN=SomeUser,OU=SomeOU,DC=SomeCompany,DC=com"
     
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTxt = objFSO.OpenTextFile(strFile, ForReading)
    
    Do Until objTxt.AtEndOfStream
    	colRepDN = objTxt.ReadLine
            Wscript.Echo colRepDN
    Loop
    
    For each strRepDN in colRepDN
    	Wscript.Echo strRepDN
    	Set objRep = GetObject("LDAP://" & strRepDN)
    	objRep.Put "manager", strManagerDN
    	objRep.SetInfo	
    Next
    Thanks

  • #2
    Re: Reading from text file to array

    Originally posted by mhashemi View Post
    it tells me I don't have a collection. Why not and how do I get one?

    <...>
    Code:
    <snip>
    
    Do Until objTxt.AtEndOfStream
       colRepDN = objTxt.ReadLine
       Wscript.Echo colRepDN
    Loop
    The script does not create an Array. The string value of 'colRepDN' just get overwriten each new line.

    To be able to increases the size of the last dimension of a dynamic array without losing any existing data in the array you have to specify the Preserve modifier, so the elements from the existing array are copied to the new array.

    Code:
    Const ForReading = 1
    
    strFile = "PathToTextFile"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTxt = objFSO.OpenTextFile(strFile, ForReading)
    
    Dim arrRepDN(): i = 0
    Do Until objTxt.AtEndOfStream
       strRepDN = Trim(objTxt.ReadLine)
       If len(strRepDN) > 0 Then
         Redim Preserve arrRepDN(i)
         arrRepDN(i)= strRepDN
         i = i + 1
       End If
    Loop
    
    objTxt.close
    
    For each strRepDN in arrRepDN
       Wscript.Echo strRepDN
    Next
    OR,
    Code:
    Const ForReading = 1
    
    strFile = "PathToTextFile"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTxt = objFSO.OpenTextFile(strFile, ForReading)
    
    arrRepDN = Split(objTxt.ReadALL, vbCrLf)
    
    objTxt.close
    
    For each strRepDN in arrRepDN
       If Len(Trim(strRepDN)) > 0 Then
         Wscript.Echo Trim(strRepDN)
       End If
    Next

    \Rems
    Last edited by Rems; 16th February 2010, 20:40.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Reading from text file to array

      Or you could just include everything in the loop without creating an arrary:

      Code:
      Const ForReading = 1
       
      strFile = "PathToTextFile"
      strManagerDN = "CN=SomeUser,OU=SomeOU,DC=SomeCompany,DC=com"
       
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objTxt = objFSO.OpenTextFile(strFile, ForReading)
      
      Do Until objTxt.AtEndOfStream
         strRepDN = Trim(objTxt.ReadLine)
      
         On Error Resume Next
         Set objRep = GetObject("LDAP://" & strRepDN)
         If (Err.Number <> 0) Then
            On Error GoTo 0
            WScript.Echo "Unable to bind to Rep: " & strRepDN
         End If
         On Error GoTo 0
         
         objRep.Put "manager", strManagerDN
      
         On Error Resume Next
         objRep.SetInfo
         If (Err.Number <> 0) Then
            On Error GoTO 0
            WScript.Echo "Unable to set Rep info for: " & strRepDN
         End If
         On Error GoTo 0
      Loop
      Last edited by ekrengel; 16th February 2010, 22:26.

      Comment


      • #4
        Re: Reading from text file to array

        Thanks for the clarification.

        Comment

        Working...
        X