Jump to content

Refresh array after one loop


Recommended Posts

Hi

I need some help.

I try ping computers from targets.txt (I have computer name in lines) if they ping I want do something and after that remove pc name from targets.txt. It`s working but problem is when I put this inside loop, because I need run script until all computers will not be removed from the list (targets.txt). I don`t know how to "refresh" array :(

Please help :)

#include <File.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>

Local $PC_list = "\targets.txt"
Local $bTargets = FileReadToArray(@ScriptDir& $PC_list)

Do
   _check_ping()
   ;count_lines
   Local $file_count_lines = _FileCountLines(@ScriptDir& $PC_list)
   $i = $file_count_lines
   ConsoleWrite("na liście "&$i & @CRLF)
Until $i = 0
; IF &i = 0 then show MsgBox
MsgBox(1, "xXx", "Finish")

;PING
Func _check_ping()
   For $sPC In $bTargets
   $var = Ping($sPC, 250)
      If $var Then
         ConsoleWrite($sPC &"online"& @CRLF)
         ;Do something

         ;Delete $sPC from targets.txt
         Local $_Array
         _FileReadToArray ( @ScriptDir& "\targets.txt", $_Array )
         $_Array = _DeleteArrayElementWithStringInstr ( $_Array, $sPC )
         _FileWriteFromArray ( @ScriptDir& "\targets.txt", $_Array, 1 )
         Sleep(200)
      Else
         ConsoleWrite($sPC &"offline"& @CRLF)
      EndIf
   Next
EndFunc

;DELETE LINE FROM TXT
Func _DeleteArrayElementWithStringInstr ( $_Array, $_String )
    Local $_Item
    For $_Element In $_Array
        If StringInStr ( $_Element, $_String ) <> 0 Then
            _ArrayDelete ( $_Array, $_Item )
        Else
            $_Item+=1
        EndIf
    Next
    Return ( $_Array )
EndFunc ;==> _DeleteArrayElementWithStringInstr ( )

 

Edited by harvester2001
Link to comment
Share on other sites

If you don't really need to destroy your "targets.txt" file, then you could use an array to track what's going on, keeping your file safe instead.

Something like this should work (not tested)

Local $PC_list = "\targets.txt"
Local $aTargets = FileReadToArray(@ScriptDir & $PC_list)
If @error Then
    MsgBox(0, "Error", "Error on reading file.")
    Exit
EndIf

Local $iToBePinged = UBound($aTargets) ; total nr of pc to ping
Local $aIndex[$iToBePinged + 1] = [$iToBePinged] ; counter in element [0]

; initialize index
For $i = 1 To $aIndex[0]
    $aIndex[$i] = $i - 1
Next

Do
    For $i = $aIndex[0] To 1 Step -1 ; scans all remaining pc
        If Ping($aTargets[$aIndex[$i]], 250) Then
            ConsoleWrite($aTargets[$aIndex[$i]] & " online" & @CRLF)
            ; Do something

            ; adjust indexes
            $aIndex[$i] = $aIndex[$aIndex[0]]
            $aIndex[0] -= 1
        Else
            ; ConsoleWrite($aTargets[$aIndex[$i]] & " offline" & @CRLF)

        EndIf
    Next
Until Not $aIndex[0]

MsgBox(0, "xXx", "Finish")

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

But this work wery unstable. The loop works, after a few runs exit code. No specific error.

Any ideas how to improve?

 

This is all code, maybe somebody need this.

Script read information about configuration from computers in network.

 

P.S.

I somebody know how to get display resolution (from remote computer) plz give me know :)

#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <FileConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <Date.au3>

Local $PC_list = "\targets.txt"
Local $log_success = "\success.txt"
Local $log_error = "\error.txt"


Local $aTargets = FileReadToArray(@ScriptDir & $PC_list)
If @error Then
    MsgBox(0, "Error", "Error on reading file targets.txt")
    Exit
EndIf

Local $iToBePinged = UBound($aTargets) ; total nr of pc to ping
Local $aIndex[$iToBePinged + 1] = [$iToBePinged] ; counter in element [0]

; initialize index
For $i = 1 To $aIndex[0]
    $aIndex[$i] = $i - 1
Next

Do
    For $i = $aIndex[0] To 1 Step -1 ; scans all remaining pc
        If Ping($aTargets[$aIndex[$i]], 250) Then
            ConsoleWrite($aTargets[$aIndex[$i]] & " online" & @CRLF)
            ; Do something
               czytam_zdalny_pc()
               Sleep(200)
            ; adjust indexes
            $aIndex[$i] = $aIndex[$aIndex[0]]
            $aIndex[0] -= 1
            ConsoleWrite($i & " remained " & @CRLF)
        Else
            ConsoleWrite($aTargets[$aIndex[$i]] & " offline" & @CRLF)
            Sleep(100)
        EndIf
    Next
Until Not $aIndex[0]

MsgBox(0, "INWENTARYZATOR", "Finished gathering information ")


Func czytam_zdalny_pc()

   $objWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\" & $aTargets[$aIndex[$i]] & "\root\cimv2")
   If IsObj($objWMIService) Then
         ;Get System
         $colItem = $objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem")
         For $objItem in $colItem
             ;ConsoleWrite( $objItem.CSName & " " & $objItem.Caption & " " & $objItem.Version & @CRLF)
             Local $systemOS = $objItem.CSName & " " & $objItem.Caption & " " & $objItem.Version
         Next

         ;ConsoleWrite(@CRLF)

         ;Get Ram
         $colItem = $objWMIService.ExecQuery ("SELECT * FROM Win32_ComputerSystem")
         For $objItem in $colItem
             ;ConsoleWrite( "Total RAM " & $objItem.TotalPhysicalMemory & @CRLF)
             Local $RAM = Int($objItem.TotalPhysicalMemory /1048576)
         Next

         ;Get PC Model
         $colItem = $objWMIService.ExecQuery ("SELECT * FROM Win32_ComputerSystemProduct")
         For $sOS in $colItem
             ;ConsoleWrite(" Vendor: " & $sOS.Vendor & " SN: " & $sOS.IdentifyingNumber & " Name: " & $sOS.Name & @CRLF)
         Next

         ;ConsoleWrite(@CRLF)

         ; Wpisuje do pliku
         FileWriteLine(@ScriptDir & $log_success, $aTargets[$aIndex[$i]] & _
         " Vendor: " & $sOS.Vendor & " SN: " & $sOS.IdentifyingNumber & " Name: " & $sOS.Name & _
         " System: " & $systemOS & " RAM: " & $RAM &" mb "& _
         @CRLF)


         ;console log
         ConsoleWrite($aTargets[$aIndex[$i]] & " I read data" & @CRLF)
       Else
           ;Msgbox($MB_OK,"WMI Output","No WMI Objects Found for class: " & "Win32_ComputerSystemProduct" )
           ConsoleWrite($aTargets[$aIndex[$i]] & " No WMI Objects Found" & @CRLF)
       Endif
EndFunc

 

Edited by harvester2001
Link to comment
Share on other sites

  • 3 weeks later...

Help me plz.

I do something like that, its stable but one problem, its not delete last row and loop never end.

I need loop working until last localization answer for ping.

#include <File.au3>
#include <Array.au3>


For $i = 0 to _FileCountLines(@ScriptDir& "\targets.txt")
   Dim $array = FileReadToArray(@ScriptDir& "\targets.txt")
   For $i = UBound($array) -1 To 0 Step -1
      Sleep(200)
      If Ping($array[$i], 250) Then
         ;ONLINE
         ConsoleWrite($array[$i] & "online" & @CRLF)
         Sleep(200)
         $iIndex = _ArraySearch($array, $array[$i], 1, 0, 0, 1)
         Sleep(200)
         ; And if found - delete
         If Not @error Then
             _ArrayDelete($array, $iIndex)
          EndIf
          Sleep(200)
          _FileWriteFromArray(@ScriptDir& "\targets.txt", $array, 1)
      Else
         ;OFFLINE
         ConsoleWrite($array[$i] & "offline" & @CRLF)
         Sleep(200)
      EndIf
   Next
Next

 

Link to comment
Share on other sites

Maybe this?

#include <File.au3>
#include <Array.au3>
Dim $array = FileReadToArray(@ScriptDir & "\targets.txt")
For $i = UBound($array) - 1 To 0 Step -1
    Sleep(200)
    If Ping($array[$i], 250) Then
        ;ONLINE
        ConsoleWrite($array[$i] & "online" & @CRLF)
        Sleep(200)
        ; And if found - delete
        If Not @error Then
            _ArrayDelete($array, $i)
        EndIf
        Sleep(200)
    Else
        ;OFFLINE
        ConsoleWrite($array[$i] & "offline" & @CRLF)
        Sleep(200)
    EndIf
Next
_FileWriteFromArray(@ScriptDir & "\targets.txt", $array)

In your script you were reusing the $i variable in the second loop, so the value never gets updated for the first loop.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thx, but  loop end when check all locations from list.

I need loop to working untill  locations respond successfully to ping.

If  location is offline loop should check another locations and after finish, check again locations which were offline.

In brief: I want send something for this locations, but if they offline this aplications should wait until they will be online.

Sorry for my english.

Link to comment
Share on other sites

Try this.

#include <File.au3>
#include <Array.au3>
Dim $array = FileReadToArray(@ScriptDir & "\targets.txt")
Global $ReadError = @error
While $ReadError = 0
    For $i = UBound($array) - 1 To 0 Step -1
        Sleep(200)
        If Ping($array[$i], 250) Then
            ;ONLINE
            ConsoleWrite($array[$i] & "online" & @CRLF)
            Sleep(200)
            ; And if found - delete
            If Not @error Then
                _ArrayDelete($array, $i)
            EndIf
            Sleep(200)
        Else
            ;OFFLINE
            ConsoleWrite($array[$i] & "offline" & @CRLF)
            Sleep(200)
        EndIf
    Next
    If UBound($array) > 0 Then
        _FileWriteFromArray(@ScriptDir & "\targets.txt", $array)
    Else
        ExitLoop
    EndIf
    $array = FileReadToArray(@ScriptDir & "\targets.txt")
    $ReadError = @error
WEnd

If FileReadToArray reads an empty file it will set the @error macro and the While loop will end. If the $array array is empty, it exits the loop as well, which would only happen if all the Pings return as valid.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

harvester2001,

Another way to look at the problem.  The loop is controlled by a switch and the source file is not altered.

#include <array.au3>
#include <date.au3>

Local $aPingList = StringSplit(FileRead(@ScriptDir & '\targets.txt'), @CRLF, 3), $again = True

While $again

    $again = False

    For $1 = 0 To UBound($aPingList) - 1

        If $aPingList[$1] = '' Then ContinueLoop
        Ping($aPingList[$1], 250)
        If @error Then
            ConsoleWrite('!  ' & StringFormat('%10s %30s was not reachable', _Now(), $aPingList[$1]) & @CRLF)
            $again = True ; loop again
        Else
            ConsoleWrite('>  ' & StringFormat('%10s %30s was     reachable', _Now(), $aPingList[$1]) & @CRLF)
            ;
            ; go do your other stuff
            ;
            $aPingList[$1] = '' ;   clear entry
        EndIf

    Next
WEnd

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...