Jump to content
Sign in to follow this  
MattX

Making a list

Recommended Posts

About selecting the computers, what about using a list in file? You check the @computername value over the list.

I want to take this further if I can. If I create a txt file of computer names how can I tell the script to only do a certain part if the computer exsists in the script - a sort of look up table....

I have not done this before so would appreciate some advice.

Share this post


Link to post
Share on other sites

Load the file in a string, convert the string to an array and checks @computername with the list via For loop.

Share this post


Link to post
Share on other sites

Load the file in a string, convert the string to an array and checks @computername with the list via For loop.

<{POST_SNAPBACK}>

Could you provide an example please ?

Share this post


Link to post
Share on other sites

Simple...

RTFM

Create a text file. Type in all of your computernames.

Read from the text file. Line one, and then + 1, so it goes down the list.

$line1 = readtextfile(1) (dont remember exact function name, you get the point)

If @ComputerName = $line Then

Do whatever you want it to do when it gets it, maybe...

Writetotextfile("lol.txt", @Computername & "Working")

Hope that helped.

-Para :lmao:


Share this post


Link to post
Share on other sites

Does this make any sence to anyone ?

; computernames in c:\cnames.txt
$cname = FileOpen("C:\cnames.txt", 0)
If $cname = -1 Then
   MsgBox(0, "Error", "Unable to open file.")
   Exit
While 1
    $line = FileReadLine($cname)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Line read:", $line)
Wend
If $line = @Computername then DO SOMETHING
ELSE DO SOMETHINGELSE
FileClose($cname)

Would this check the file cnames.txt and if a name was in there that I wanted would execute the If $line=@computername ?

Simple...

RTFM

Create a text file. Type in all of your computernames.

Read from the text file. Line one, and then + 1, so it goes down the list.

$line1 = readtextfile(1) (dont remember exact function name, you get the point)

If @ComputerName = $line Then

Do whatever you want it to do when it gets it, maybe...

Writetotextfile("lol.txt", @Computername & "Working")

Hope that helped.

-Para :lmao:

<{POST_SNAPBACK}>

Share this post


Link to post
Share on other sites

This load the whole file in an array, one line per array position. Total lines in position 0.

Local $filename = 'test.txt'


Local $computerlist, $c
If FileExists($filename) Then
   $computerlist = FileRead($filename, FileGetSize($filename))
   Select
      Case StringInStr($computerlist, @CRLF)
         StringReplace($computerlist, @CRLF, @LF)
      Case StringInStr($computerlist, @CR)
         StringReplace($computerlist, @CR, @LF)
   EndSelect
   
   $computerlist = StringSplit($computerlist, @LF)
   
   For $c = 1 To $computerlist[0]
      MsgBox(0, '', $computerlist[$c])
   Next
   
EndIf

This one, shown a line per line reading it direcly from the file.

Local $filename = 'test.txt'

Local $filehandle
$filehandle = FileOpen($filename, 0)
If $filehandle <> - 1 Then
   While 1
      $line = FileReadLine($filehandle)
      If @error Then
         FileClose($filehandle)
         ExitLoop
      EndIf
      
      MsgBox(0, '', $line)
      
   WEnd
EndIf

Edit: I arrived late.

Just fileclose just after the "If @error", not at the end of the script. Leaving handles pending more than needed if a bad idea.

Edit2: Download Tidy.exe of Jdbe and try to tidy your script when you do not understand what is going on. Most probably you meant...

; computernames in c:\cnames.txt
$cname = FileOpen("C:\cnames.txt", 0)
If $cname = -1 Then
   MsgBox(0, "Error", "Unable to open file.")
Else
   While 1
      $line = FileReadLine($cname)
      If @error Then ExitLoop

      MsgBox(0, "Line read:", $line)

      If $line = @ComputerName Then
        ; DO SOMETHING
         
         ExitLoop;so you dont check the rest of the list.
      Else
        ; DO SOMETHINGELSE, probably nothing. You have to check the rest of the list.
      EndIf
      
   WEnd
   FileClose($cname)
EndIf

Edit3: improved.

Edit4: changed the position of FileClose() so it close the handle also when the computername is found.

Edited by ezzetabi

Share this post


Link to post
Share on other sites

Thanks for the reply. When you edited my code you put in:

'ExitLoop;so you dont check the rest of the list.'

Does that mean not to check the rest of the file if the name is found ?

Another question - [ sorry ] - if the computer name is not found in the list will it just execute the rest of the code ?

Edit2: Download Tidy.exe of Jdbe and try to tidy your script when you do not understand what is going on. Most probably you meant...

; computernames in c:\cnames.txt
$cname = FileOpen("C:\cnames.txt", 0)
If $cname = -1 Then
   MsgBox(0, "Error", "Unable to open file.")
Else
   While 1
      $line = FileReadLine($cname)
      If @error = -1 Then
         FileClose($cname)
         ExitLoop
      EndIf
      MsgBox(0, "Line read:", $line)
      
      
      If $line = @ComputerName Then
       ; DO SOMETHING
         
         ExitLoop;so you dont check the rest of the list.
      Else
       ; DO SOMETHINGELSE, probably nothing. You have to check the rest of the list.
      EndIf
      
   WEnd
EndIf

Edit3: improved.

<{POST_SNAPBACK}>

Share this post


Link to post
Share on other sites

Thanks for the reply. When you edited my code you put in:

'ExitLoop;so you dont check the rest of the list.'

Does that mean not to check the rest of the file if the name is found ?

If it is not bleeding obvious, I am afraid you have to script a little more... :lmao:

I'll try to explain with an example:

List:

A

B

C

D

E

@Computername = 'C'

The script passes the lines of the list reading in order: 'A', 'B', 'C' now it detects that the computer where the script is executing is in the list and do its job. Done the job there is no need of check also the list for reading 'D' and 'E'. It finished already! You need to pass the whole file only if it does not find the @computername in the list.

Ops... I noticied that this exitloop will skip the FileClose(), so I changed its position.

Another question - [ sorry ] - if the computer name is not found in the list will it just execute the rest of the code ?

<{POST_SNAPBACK}>

The code that is executed if the @computername is in the list is the one inside the If Then/EndIf with the comment '; DO SOMETHING'. If the whole list passes without finding a match this code is never executed so the script does nothing.

Share this post


Link to post
Share on other sites

Thanks - I agree with having to script more. Problem its something I only turn to now and again when I need to find a work around or an answer to a problem. As I am all self taught [ like many others ] I have got loads of bad habits - I really miss my GOTO commands... :lmao:

If it is not bleeding obvious, I am afraid you have to script a little more... o:)

I'll try to explain with an example:

List:

A

B

C

D

E

@Computername = 'C'

The script passes the lines of the list reading in order: 'A', 'B', 'C' now it detects that the computer where the script is executing is in the list and do its job. Done the job there is no need of check also the list for reading 'D' and 'E'. It finished already! You need to pass the whole file only if it does not find the @computername in the list.

Ops... I noticied that this exitloop will skip the FileClose(), so I changed its position.

The code that is executed if the @computername is in the list is the one inside the If Then/EndIf with the comment '; DO SOMETHING'. If the whole list passes without finding a match this code is never executed so the script does nothing.

<{POST_SNAPBACK}>

Share this post


Link to post
Share on other sites

Oh dear. If you miss goto you REALLY NEED TO SCRIPT MORE.

If you want avoid that everyone laught at you, don't say you miss GoTo.

But you said so...

Scripters -> :)o:):whistle::):huh2::):P:lmao: <- You

Edited by ezzetabi

Share this post


Link to post
Share on other sites

Good lord, I was joking - I've said that before - didn't you spot the o:) after the comment ?

BTW - I really miss GOSUB, INKEY$ & DRAW too, but lets not go there...

Oh dear. If you miss goto you REALLY NEED TO SCRIPT MORE.

If you want avoid that everyone laught at you, don't say you miss GoTo.

But you said so...

Scripters -> :whistle:  :)  :huh2:  :)  :P  :)  :P  :lmao: <- You

<{POST_SNAPBACK}>

Share this post


Link to post
Share on other sites

Oh your such a tease !! :lmao:o:)

God knows what I would do without Autoit though - most of the apps being used at school where I work are launched using it - its a lifesaver.

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...