Sign in to follow this  
Followers 0
MeepZero

Outputting to a file and then reading from the file

4 posts in this topic

I'm trying to make some code to throw into the end of one script and at the begining of another. The idea is when we image new machines, the current script needs to reboot the machine to continue our processes, but when it comes back up I'd like it to retain the entered information from the first boot.

I've got the top section working, it will take some inputs and then write them to the first few lines of a file. Then in the second section it is supposed to read those three lines and push them into an array. I thought about just using three variables but it seemed easier to use an array instead since the while loop complicated things.

Apologies for the lack of tabs, they never seem to carry over when I copy paste this stuff into the forum forms.

;This code is left in the first script
Global $OutUser, $OutPass, $OutOwner
$OutOwner = "Owner"
$OutPass = "Pass"
$OutUser = "User"

;$value = InputBox ( "Title", "Enter IP.", "", "" )
$file = FileOpen ( "C:\temp\PrepTemp.txt", 1 )
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf

FileWriteLine($file,$OutUser)
FileWriteLine($file,$OutPass)
FileWriteLine($file,$OutOwner)

ConsoleWrite("finished writing to file, closing" & @CRLF)

;FileWrite ( $file, $value ) ;Output file and data going to said file
FileClose ( $file )

ConsoleWrite("Resetting variables" & @CRLF)
$OutOwner = "FAIL"
$OutPass = "FAIL"
$OutUser = "FAIL"

;From here forward, code is moved into the begining of the next script so it can read what was left behind previously.
Global $cred[3]

$file = FileOpen ( "C:\temp\PrepTemp.txt", 0 )
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf

ConsoleWrite("trying to do magic" & @CRLF)
While 1
Local $line = FileReadLine($file)
ConsoleWrite($line & @CRLF)
Local $var
$var = 0
$cred[$var] = $line
If @error = -1 Then ExitLoop
$var = $var + 1
WEnd

ConsoleWrite($cred[0] & @CRLF)
ConsoleWrite($cred[1] & @CRLF)
ConsoleWrite($cred[2] & @CRLF)

In its current form, I can't get the script to actually output anything in the console and show what was in the file. Any ideas on this?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

looked like it was working, just never stopping the loop (so it kept writing blanks).

Heres another way to do the read loop:

#include<file.au3>

;This code is left in the first script
Global $OutUser, $OutPass, $OutOwner
$OutOwner = "Owner"
$OutPass = "Pass"
$OutUser = "User"

;$value = InputBox ( "Title", "Enter IP.", "", "" )
$file = FileOpen ("C:tempPrepTemp.txt", 2)
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf

FileWriteLine($file,$OutUser)
FileWriteLine($file,$OutPass)
FileWriteLine($file,$OutOwner)

ConsoleWrite("finished writing to file, closing" & @CRLF)

;FileWrite ( $file, $value ) ;Output file and data going to said file
FileClose ( $file )

ConsoleWrite("Resetting variables" & @CRLF)

$OutOwner = "FAIL"
$OutPass = "FAIL"
$OutUser = "FAIL"

;From here forward, code is moved into the begining of the next script so it can read what was left behind previously.
Global $cred[3]

$max = _filecountlines("C:tempPrepTemp.txt")
$file = FileOpen ("C:tempPrepTemp.txt", 0)

If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf

ConsoleWrite("trying to do magic" & @CRLF)

for $x = 1 to $max
Local $line = FileReadLine($file)
ConsoleWrite($line & @CRLF)
$cred[$x - 1] = $line
next

ConsoleWrite($cred[0] & @CRLF)
ConsoleWrite($cred[1] & @CRLF)
ConsoleWrite($cred[2] & @CRLF)
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Just to point out the crux of the problem (in case it didn't jump out at you)...

@error is reset by nearly every internal Autoit function, so your test of @error was checking the (always successful) result of the ConsoleWrite(), which has overwritten any @error result from the preceding FileReadLine() function.

Why 2 scripts? It seems like some initial processing similar to :

$sFile = "C:tempPrepTemp.txt"

If FileExists($sFile) Then
    GetDataFromFile()
    DoPhase2()
Else
    GetDataFromConsole()
    DoPhase1()
    WriteTempFile()
EndIf

...might work for you?

Or maybe output a phase or status number as the first line of the temp file. Then even if you had to reboot 5 times you could always keep track of where you were...

$sFile = "C:tempPrepTemp.txt"

If FileExists($sFile) Then
    GetDataFromFile()
Else
    GetDataFromConsole()
EndIf

Switch cred(0)
    Case 1
        DoPhase1()
    Case 2
        DoPhase3()
    Case 3
        DoPhase3()
EndSwitch
Edited by Spiff59

Share this post


Link to post
Share on other sites

looked like it was working, just never stopping the loop (so it kept writing blanks).

Heres another way to do the read loop:

#include<file.au3>

;This code is left in the first script
Global $OutUser, $OutPass, $OutOwner
$OutOwner = "Owner"
$OutPass = "Pass"
$OutUser = "User"

;$value = InputBox ( "Title", "Enter IP.", "", "" )
$file = FileOpen ("C:tempPrepTemp.txt", 2)
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf

FileWriteLine($file,$OutUser)
FileWriteLine($file,$OutPass)
FileWriteLine($file,$OutOwner)

ConsoleWrite("finished writing to file, closing" & @CRLF)

;FileWrite ( $file, $value ) ;Output file and data going to said file
FileClose ( $file )

ConsoleWrite("Resetting variables" & @CRLF)

$OutOwner = "FAIL"
$OutPass = "FAIL"
$OutUser = "FAIL"

;From here forward, code is moved into the begining of the next script so it can read what was left behind previously.
Global $cred[3]

$max = _filecountlines("C:tempPrepTemp.txt")
$file = FileOpen ("C:tempPrepTemp.txt", 0)

If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf

ConsoleWrite("trying to do magic" & @CRLF)

for $x = 1 to $max
Local $line = FileReadLine($file)
ConsoleWrite($line & @CRLF)
$cred[$x - 1] = $line
next

ConsoleWrite($cred[0] & @CRLF)
ConsoleWrite($cred[1] & @CRLF)
ConsoleWrite($cred[2] & @CRLF)

That did it, thanks a ton for the help :D

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  
Followers 0