Jump to content

Problem with FileWrite


Recommended Posts

I can't figure this one out. I have a script that will ask for the time off of a terminal emulator and then parse the result into the format the _DateDiff command uses. Then it should tell me how many seconds between the requested time and actual time.

I have the script written but FileReadLine always returns blanks to the $parsetime variable. If I remove FileReadLine and let the second script run by itself it works. The first script leaves everything blank: $parsetime, $smo, $sday, $shour, etc...

Any ideas? (I'm using beta)

This script works...

#include "File.au3"
#include "String.au3"
#include "Misc.au3"
#include "Date.au3"

$starttime = "2006/08/02 01:00:00"

    $timefile = FileOpen("c:\time.txt", 0)
        If $timefile = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
        EndIf
    $parsetime = FileReadLine($timefile, 2) 
    FileClose($timefile)
    $parsetime = StringStripWS ($parsetime, 1);strips leading white space // begin time parse
    $parsetime = StringTrimLeft ($parsetime, 16)
    $smo = StringLeft ($parsetime, 2)
    $parsetime = StringTrimLeft ($parsetime, 3)
    $sday = StringLeft ($parsetime, 2)
    $parsetime = StringTrimLeft ($parsetime, 7)
    $shour = StringLeft ($parsetime, 2)
    $parsetime = StringTrimLeft ($parsetime, 2)
    $smin = StringLeft ($parsetime, 2)
    $parsetime = StringTrimLeft ($parsetime, 3)
    $ssec = StringLeft ($parsetime, 2) ;end time parse
        
;   $waitsec = _DateDiff( 's',@YEAR & "/08/01[ 23:18:[00]]",@YEAR & $smo & "/" & $sday "[ " & $shour & ":" & $smin & ":[" & $ssec & "]]" )  
    $waitsec = _DateDiff( 's',@YEAR & "/" & $smo & "/" & $sday & " " & $shour & ":" & $smin & ":" & $ssec,$starttime)
    MsgBox(4096, "Result", "The time difference is: " & $waitsec)

This script returns blanks...

#include "File.au3"
#include "String.au3"
#include "Array.au3"
#include "Misc.au3"
#include "Date.au3"

Dim $line[5]

$starttime = "2006/08/02 10:30:00" ;enter time to find difference from

If NOT FileExists("C:\drop.txt") Then
    MsgBox(4096, "", "c:\drop.txt data does not exist.")
    Exit
EndIf

If WinExists("MyApp") Then
    Opt("MouseCoordMode", 0)
    Opt("SendKeyDelay", 1)
    $countlines = _FileCountLines("C:\drop.txt")
    
    $timefile = FileOpen("C:\time.txt", 2);begin time check from terminal
            If $timefile = -1 Then
            MsgBox(0, "Error", "Unable to open file.")
            Exit
            EndIf
        WinActivate("MyApp") 
        MouseClick ("left", 50, 240, 1, 0)
        Send ("{BS}" & "'t" & "{ENTER}")
        Sleep(1000)
        Send ("^a")
        Send ("^c")
    $sabrecopy = ClipGet()
    FileWrite($timefile, $sabrecopy) ; by this point in the script the file is successfully created
                                        ;I have also tried FileWriteLine for the above command.
    $parsetime = FileReadLine($timefile, 2)
    FileClose($timefile)
    $parsetime = StringStripWS ($parsetime, 1);strips leading white space // begin time parse
    $parsetime = StringTrimLeft ($parsetime, 16)
    $smo = StringLeft ($parsetime, 2)
    $parsetime = StringTrimLeft ($parsetime, 3)
    $sday = StringLeft ($parsetime, 2)
    $parsetime = StringTrimLeft ($parsetime, 7)
    $shour = StringLeft ($parsetime, 2)
    $parsetime = StringTrimLeft ($parsetime, 2)
    $smin = StringLeft ($parsetime, 2)
    $parsetime = StringTrimLeft ($parsetime, 3)
    $ssec = StringLeft ($parsetime, 2) ;end time parse
    $waitsec = _DateDiff( 's',@YEAR & "/" & $smo & "/" & $sday & " " & $shour & ":" & $smin & ":" & $ssec,$starttime)  
    MsgBox(4096, "Result", "The time difference is: " & $waitsec)

    $file = FileOpen("C:\drop.txt", 0)
    $log = FileOpen("C:\droplog.txt", 1)

    For $I = 1 to $countlines
; looped commands go here
        Next
    FileClose($file)
    FileClose($log)
else
    MsgBox(4096, "", "Error: can't find MyApp window.")
EndIf
Exit

Here is the text file with the time that I'm parsing...

¥T«                                                            
         CENTRAL TIME IS 08/02 214 1538.06¶

Any ideas/input are greatly appreciated.

Link to comment
Share on other sites

You must close the File when it is in write mode and then reopen in read mode in order to read it.

$timefile = FileOpen("C:\time.txt", 2);begin time check from terminal
    ...
    FileWrite($timefile, $sabrecopy) ; by this point in the script the file is successfully created
    FileClose($timefile) ; add this line to close the file
    $timefile = FileOpen("C:\time.txt", 0); Then reopen it again in Read mode = 0    
   $parsetime = FileReadLine($timefile, 2) ; Now you can read the information in file

regards

Link to comment
Share on other sites

Actually, I suppose it's a problem with the FileReadLine function instead of the FileWrite function since c:\time.txt is being created no problem.

Edit: You beat me to it in you're previous post. Thanks.

Edited by herbgoat
Link to comment
Share on other sites

GioVit,

Thanks for your suggestions. I have tried both of them and still no luck.

I added the FileClose and reopen and also gave it a little sleep time. I also tried telling it to read line 1. Any more ideas? Here is my latest attempt.

$sabrecopy = ClipGet()
FileWriteLine($timefile, $sabrecopy)
FileClose($timefile)
Sleep(300)
FileOpen($timefile, 0)
$parsetime = FileReadLine($timefile, 2)
FileClose($timefile)
$parsetime = StringStripWS ($parsetime, 1)
Link to comment
Share on other sites

I made a workaround by seperating my script into 2 seperate files and having the first script launch the second. It works this way but I'm not really sure why. The only bad thing is I have to have the second script as an exe so it adds a step during editing.

Thanks for your input.

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...