Jump to content

Need some input on a project


Recommended Posts

Hi All,

I am wanting to write a little script to automate loading of some computers.

1.I need the PC's to read a certain text file on the network to obtain the location name

2.Then to read a second file which will give its station number.

3. Combine this to rename the computer (Scripting already doen for this)

4.Append the Station number text document with an x on the end of the station number.

The next client to load will then Go through the same process and if a line of text finishes with an x it will jump to next line...and so on.

Now I being new to auot it, do I need to use an array for this or how should I attack this?

Easiest way possible obviously. I don't want anyone to write it just guide me on the way.

Thanks All in Advance.

Mike

Edited by hispeed_mike
Link to comment
Share on other sites

add an x to the end of the file check out Filereadline and umm stringlen to get the length then getting the last thing you can use

For $i = 1 to _FileCountLines($file)
    $string = Filereadline($file,$i)
If Stringtrimleft($string,stringlen($string) - 1) = 'x' Then
;blah
endif
Next
Edited by thatsgreat2345
Link to comment
Share on other sites

add an x to the end of the file check out Filereadline and umm stringlen to get the length then getting the last thing you can use

For $i = 1 to _FileCountLines($file)
    $string = Filereadline($file,$i)
If Stringtrimleft($string,stringlen($string) - 1) = 'x' Then
;blah
endif
Next
Could you expand on the explaination of how this snippet works? $i, $string, $file? -I don't understand the whole variables thing and where I would put the paths to the document I need to read from....

Thanks

Mike

Link to comment
Share on other sites

Yeah

you said you were reading a txt file so $file i just used as whatever your file would be

$i is being used in a for loop along with Filereadline so it will add 1 to $i each time thus every time it goes through it will read the next line, until it reaches _FileCountLines which is how many lines there are.

For the If statement it will Trim the line read($string) down to the last character by getting its length and subtracting one then compares it to see if x is there since you said x would be the last character, if it is x then it will execute whatever you put between the if then endif

Link to comment
Share on other sites

Yeah

you said you were reading a txt file so $file i just used as whatever your file would be

$i is being used in a for loop along with Filereadline so it will add 1 to $i each time thus every time it goes through it will read the next line, until it reaches _FileCountLines which is how many lines there are.

For the If statement it will Trim the line read($string) down to the last character by getting its length and subtracting one then compares it to see if x is there since you said x would be the last character, if it is x then it will execute whatever you put between the if then endif

Thanks...Will start to script and see how I go....Thanks again for explaination

Mike

Link to comment
Share on other sites

no prob feel free to post your script if you need any help implementing my code

Here is what I have done to my understanding. I can read any given txt file and return to the message box the first line of input. I just cannot for the life of me understand the sequence to impliment the script you gave me...

The next thing I need to do is read that text file and then if it has an x as 3rd character it will then change the $chars variable to equal the next line that does not have an x at the end.

The last part of the script then has to append an x onto the end of the number that had successfully become the $chars value.

All I could do is take a code snippet from the help file and then mess with it. I just need this $chars in memory so I can call on it for the remainder of the script. Please if you don't mind helping me understand the process, so I can give a bit back.

Cheers

$file = FileOpen("textfiletoopen", 0)

$chars = FileReadLine($file)

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

MsgBox(0, "Char read:", $chars)

FileClose($file)

Link to comment
Share on other sites

ehhehe i was bored and had a lil fun so this is the text file i used

EDIT: commented every line with what it does

XXX is made of pornstars that eat
tacos and burritos x
kittens and dogs x
but not people
or monkeys and rhinos
or hydrophylic acid x
cocko doodle doo

and this should get but not people

#include <File.au3> ; includes file UDF(user defined functions)
$file = FileOpenDialog("Select File","","All Filles(*.*)"); searches for a file
$string = FileReadLine($file,1); assigns variable string with first line
If StringTrimLeft(StringLeft($string,3),2) = "x" Then ; grabs 3 characters to the left then trims 2 off and if it equals x then it
For $i = 2 to _FileCountLines($file); will go from line 2 to how ever many are in the file
$string = Filereadline($file,$i); assings string variable with line being read which corresponds to $i
If Stringtrimleft($string,stringlen($string) - 1) = 'x' Then; trims the line read by getting its actual length and subtracting 1
    ;if it does then thats not what you want so it does nothing
Else; has found the line with out x on the end so it does this stuff
    $chars = FileReadLine($file,$i); if the line didnt end in x then it goes here and assigns char variable with the line
    ExitLoop; exit the foor loop so in case there are other lines without x on the end they wont be read and assinged to char
Endif; ends the if end of line = x 
Next ; ends the for loop
If $chars <> "" Then ; if chars is greater or less then nothing meaning if theres some text there then
    MsgBox(0,"Success","Line found it is: " & $chars); says the line
Else; else if there isnt anything there
    MsgBox(0,"Failure","Line not found all have x on end"); gives error
EndIf;ends the check for text in char
Else; goes back to the very begging check if x was the 3rd character
    MsgBox(0,"Error","Text Files 3rd character is not x");gives error
EndIf;ends the check for 3rd character equalling x
Edited by thatsgreat2345
Link to comment
Share on other sites

ehhehe i was bored and had a lil fun so this is the text file i used

EDIT: commented every line with what it does

XXX is made of pornstars that eat
tacos and burritos x
kittens and dogs x
but not people
or monkeys and rhinos
or hydrophylic acid x
cocko doodle doo

and this should get but not people

#include <File.au3> ; includes file UDF(user defined functions)
$file = FileOpenDialog("Select File","","All Filles(*.*)"); searches for a file
$string = FileReadLine($file,1); assigns variable string with first line
If StringTrimLeft(StringLeft($string,3),2) = "x" Then ; grabs 3 characters to the left then trims 2 off and if it equals x then it
For $i = 2 to _FileCountLines($file); will go from line 2 to how ever many are in the file
$string = Filereadline($file,$i); assings string variable with line being read which corresponds to $i
If Stringtrimleft($string,stringlen($string) - 1) = 'x' Then; trims the line read by getting its actual length and subtracting 1
    ;if it does then thats not what you want so it does nothing
Else; has found the line with out x on the end so it does this stuff
    $chars = FileReadLine($file,$i); if the line didnt end in x then it goes here and assigns char variable with the line
    ExitLoop; exit the foor loop so in case there are other lines without x on the end they wont be read and assinged to char
Endif; ends the if end of line = x 
Next ; ends the for loop
If $chars <> "" Then ; if chars is greater or less then nothing meaning if theres some text there then
    MsgBox(0,"Success","Line found it is: " & $chars); says the line
Else; else if there isnt anything there
    MsgBox(0,"Failure","Line not found all have x on end"); gives error
EndIf;ends the check for text in char
Else; goes back to the very begging check if x was the 3rd character
    MsgBox(0,"Error","Text Files 3rd character is not x");gives error
EndIf;ends the check for 3rd character equalling x
Hey,

Getting closer....

If I change the first line to

$file = FileOpen("\\pathtotxt\Stationid.txt", 0)

I got an error on line 27 saying

If $chars <>"" Then

If^ ERROR

Error: Variable used without being declared....

Is there a way to specify the path to the text document. This path above worked in the other script.

Is there a section in there for writing an x to the line that gets successfully used for $char

Otherwise that is hot....thanks for being bored...;)

Mike....

Edited by hispeed_mike
Link to comment
Share on other sites

fileopen doesnt return the path to the file thats what your doing wrong it just opens it saying its not openable to others

i couldnt sleep so i got back up

Replace FileOpenDialog with your path in quotes

example "C:\path\stationid.txt"

#include <File.au3> ; includes file UDF(user defined functions)
Global $chars
$file = FileOpenDialog("","","All Files (*.*)")
$string = FileReadLine($file,1); assigns variable string with first line
If StringTrimLeft(StringLeft($string,3),2) = "x" Then ; grabs 3 characters to the left then trims 2 off and if it equals x then it
For $i = 2 to _FileCountLines($file); will go from line 2 to how ever many are in the file
$string = Filereadline($file,$i); assings string variable with line being read which corresponds to $i
If Stringtrimleft($string,stringlen($string) - 1) = 'x' Then; trims the line read by getting its actual length and subtracting 1
    ;if it does then thats not what you want so it does nothing
Else; has found the line with out x on the end so it does this stuff
    $chars = FileReadLine($file,$i); if the line didnt end in x then it goes here and assigns char variable with the line
    _ReplaceStringInFile($file,$chars,$chars & " x")
    ExitLoop; exit the foor loop so in case there are other lines without x on the end they wont be read and assinged to char
Endif; ends the if end of line = x
Next ; ends the for loop
If Not $chars = "" Then ; if chars is greater or less then nothing meaning if theres some text there then
    MsgBox(0,"Success","Line found it is: " & $chars); says the line
Elseif $chars = "" Then; else if there isnt anything there
    MsgBox(0,"Failure","Line not found all have x on end"); gives error
EndIf;ends the check for text in char
Else; goes back to the very begging check if x was the 3rd character
    MsgBox(0,"Error","Text Files 3rd character is not x");gives error
EndIf;ends the check for 3rd character equalling x
Link to comment
Share on other sites

fileopen doesnt return the path to the file thats what your doing wrong it just opens it saying its not openable to others

i couldnt sleep so i got back up

Replace FileOpenDialog with your path in quotes

example "C:\path\stationid.txt"

#include <File.au3> ; includes file UDF(user defined functions)
Global $chars
$file = FileOpenDialog("","","All Files (*.*)")
$string = FileReadLine($file,1); assigns variable string with first line
If StringTrimLeft(StringLeft($string,3),2) = "x" Then ; grabs 3 characters to the left then trims 2 off and if it equals x then it
For $i = 2 to _FileCountLines($file); will go from line 2 to how ever many are in the file
$string = Filereadline($file,$i); assings string variable with line being read which corresponds to $i
If Stringtrimleft($string,stringlen($string) - 1) = 'x' Then; trims the line read by getting its actual length and subtracting 1
    ;if it does then thats not what you want so it does nothing
Else; has found the line with out x on the end so it does this stuff
    $chars = FileReadLine($file,$i); if the line didnt end in x then it goes here and assigns char variable with the line
    _ReplaceStringInFile($file,$chars,$chars & " x")
    ExitLoop; exit the foor loop so in case there are other lines without x on the end they wont be read and assinged to char
Endif; ends the if end of line = x
Next ; ends the for loop
If Not $chars = "" Then ; if chars is greater or less then nothing meaning if theres some text there then
    MsgBox(0,"Success","Line found it is: " & $chars); says the line
Elseif $chars = "" Then; else if there isnt anything there
    MsgBox(0,"Failure","Line not found all have x on end"); gives error
EndIf;ends the check for text in char
Else; goes back to the very begging check if x was the 3rd character
    MsgBox(0,"Error","Text Files 3rd character is not x");gives error
EndIf;ends the check for 3rd character equalling x
That worked Dandy

Is there anyway to fit inside of there Where I would tell it to append an x onto the end of the successful lnie that was used for the $chars?

I bet once you finish this one, sleep will come....

Eve just point me in the right direction where I would script it and what statement I would start with and I will try to destroy your hard work ;)

Thanks again meistro

Mike

Link to comment
Share on other sites

thats what it does _ReplaceStringInFile it finds $char in the file then replaces it with $char the original line & " x" meaning and x on the end of it

That is Super, Somehow the copy and paste didn't have the line in there that pertained to replacing the character. I have to now make this work on another file and do something a little different. It will be good now to have something that works to referance.

Thanks you Again.

Mike

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...