Jump to content

Got a problem with my script...


Recommended Posts

The problems I have are 1) it goes into an endless loop 2) doesn't change the Label 3) doesn't write to the Log.txt file.

; UNICODE to ANSI conversion and then reads the Ansi data
; Description: Use to convert (copy) files saved with Unicode to Ansi
; Also changes the Label to the current line of data
; 3 Files are needed in the 'C:\' directory - or modify to your needs
; Example.log - saved as Unicode with some sample data
; Example.txt - saved as Ansi
; Log.txt - saved as Ansi

; Once you start the script go add some data to Example.log with notepad (close and save)
; Once this is done the program will notify you that the file has changed and start processing

;Incluses
#include <file.au3>
#include <Process.au3>
#include <GUIConstants.au3>

;Set variables
$Path = "C:\"
$FileName1 = "Example.log"; UNICODE file
$FileName2 = "Example.txt"; ANSI file
$FileName3 = "Log.txt"; DIFF file
$File1 = $Path & $FileName1
$File2 = $Path & $FileName2
$File3 = $Path & $FileName3
$FGT = FileGetTime($File1,0,1)
$CountLines1 = _FileCountLines($File2);Count lines in files
$CountLines2 = _FileCountLines($File3);Count lines in files
$CountDiff = $CountLines1 - $CountLines2;Calculate difference in files
$StartRead = ($CountLines1 - $CountDiff)+1;Calculate point to start reading new data

;The form used
$Form1 = GUICreate("", 191, 58, 192, 125)
$Label1 = GUICtrlCreateLabel("", 24, 16, 139, 17)
GUISetState(@SW_SHOW)

While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case Else

;Convert from UNICODE to ANSI
        While FileGetTime($File1,0,1) <> $FGT
            MsgBox(0,"WARNING...", "Files are different - executing UNICODE conversion")
        ; Run UNICODE to ANSI conversion
            Run(@ComSpec & ' /c Type "' & $File1 & '" > "' & $File2 & '"',"",@SW_HIDE); Overwrites the ANSI txt file each time
            ;Open files
                $ANSI = FileOpen($File2, 0); Opens the ANSI txt file
                $DIFF = FileOpen($File3, 1); Opens the DIFF file
                    If $ANSI = -1 Then
                        MsgBox(0, "Error", "Unable to open " & $File2)
                        Exit
                        ElseIf $DIFF = -1 Then
                        MsgBox(0, "Error", "Unable to open " & $File3)
                        Exit
                    EndIf   
                    While $CountLines1 + 1 <> $StartRead        ; ===================================================
                    ; Read a line of data                   ;
                        $Text1 = FileReadLine($ANSI,$StartRead) ;
                        GUICtrlSetData($Label1,$Text1)          ;    This is supposed to be a nested While...WEnd
                    ;Write data to DIFF file                ;
                        FileWriteLine($DIFF, $Text1)            ;
                        $StartRead = $StartRead +1              ;
                    WEnd                                        ; ===================================================
            ;Close files
                FileClose($ANSI)
                FileClose($DIFF)
                $FTG = FileGetTime($File1,0,1); updates the varible with the current time
        WEnd
        EndSelect
    WEnd

Exit

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Link to comment
Share on other sites

The problems I have are 1) it goes into an endless loop 2) doesn't change the Label 3) doesn't write to the Log.txt file.

; UNICODE to ANSI conversion and then reads the Ansi data
; Description: Use to convert (copy) files saved with Unicode to Ansi
; Also changes the Label to the current line of data
; 3 Files are needed in the 'C:\' directory - or modify to your needs
; Example.log - saved as Unicode with some sample data
; Example.txt - saved as Ansi
; Log.txt - saved as Ansi

; Once you start the script go add some data to Example.log with notepad (close and save)
; Once this is done the program will notify you that the file has changed and start processing

;Incluses
#include <file.au3>
#include <Process.au3>
#include <GUIConstants.au3>

;Set variables
$Path = "C:\"
$FileName1 = "Example.log"; UNICODE file
$FileName2 = "Example.txt"; ANSI file
$FileName3 = "Log.txt"; DIFF file
$File1 = $Path & $FileName1
$File2 = $Path & $FileName2
$File3 = $Path & $FileName3
$FGT = FileGetTime($File1,0,1)
$CountLines1 = _FileCountLines($File2);Count lines in files
$CountLines2 = _FileCountLines($File3);Count lines in files
$CountDiff = $CountLines1 - $CountLines2;Calculate difference in files
$StartRead = ($CountLines1 - $CountDiff)+1;Calculate point to start reading new data

;The form used
$Form1 = GUICreate("", 191, 58, 192, 125)
$Label1 = GUICtrlCreateLabel("", 24, 16, 139, 17)
GUISetState(@SW_SHOW)

While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case Else

;Convert from UNICODE to ANSI
        While FileGetTime($File1,0,1) <> $FGT
            MsgBox(0,"WARNING...", "Files are different - executing UNICODE conversion")
    ; Run UNICODE to ANSI conversion
            Run(@ComSpec & ' /c Type "' & $File1 & '" > "' & $File2 & '"',"",@SW_HIDE); Overwrites the ANSI txt file each time
        ;Open files
                $ANSI = FileOpen($File2, 0); Opens the ANSI txt file
                $DIFF = FileOpen($File3, 1); Opens the DIFF file
                    If $ANSI = -1 Then
                        MsgBox(0, "Error", "Unable to open " & $File2)
                        Exit
                        ElseIf $DIFF = -1 Then
                        MsgBox(0, "Error", "Unable to open " & $File3)
                        Exit
                    EndIf   
                    While $CountLines1 + 1 <> $StartRead    ; ===================================================
                ; Read a line of data               ;
                        $Text1 = FileReadLine($ANSI,$StartRead);
                        GUICtrlSetData($Label1,$Text1)      ;    This is supposed to be a nested While...WEnd
                ;Write data to DIFF file            ;
                        FileWriteLine($DIFF, $Text1)        ;
                        $StartRead = $StartRead +1          ;
                    WEnd                                    ; ===================================================
        ;Close files
                FileClose($ANSI)
                FileClose($DIFF)
                $FTG = FileGetTime($File1,0,1); updates the varible with the current time
        WEnd
        EndSelect
    WEnd

Exit
move your window, i bet that will activate the script. what's going on is you have the real work being done on any $msg other than the close. so you need the window to generate a message. move it, resize it, etc, see if that activates it. To actually fix the problem though, i'd say to move that whole case else out of the select statement. change you select into just an if... (if $msg = close then exit basically) then have the other section. it will then do the work regardless of what's going on with the window. i'd also scrap the gui idea anyway, no real reason for it. use ToolTip() to output the filename real time so that you can still see it, but don't have to tie up realestate on your screen, etc
Link to comment
Share on other sites

in consideration of what cameronsdad said..and maybe

************* this may be a problem here

$CountLines1 + 1 <> $StartRead

the ideas is to continue until $CountLines1 + 1 = $StartRead

change this

While $CountLines1 + 1 <> $StartRead; ===================================================
              ; Read a line of data           ;
                        $Text1 = FileReadLine($ANSI,$StartRead);
                        GUICtrlSetData($Label1,$Text1)    ;  This is supposed to be a nested While...WEnd
              ;Write data to DIFF file        ;
                        FileWriteLine($DIFF, $Text1)      ;
                        $StartRead = $StartRead +1        ;
                    WEnd

to this

Do 
              ; Read a line of data           ;
                        $Text1 = FileReadLine($ANSI,$StartRead);
                        GUICtrlSetData($Label1,$Text1)    ;  This is supposed to be a nested While...WEnd
              ;Write data to DIFF file        ;
                        FileWriteLine($DIFF, $Text1)      ;
                        $StartRead = $StartRead +1        ;

                    Until  $CountLines1 + 1 <= $StartRead

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Fossil Rock Try this, replace your main WHILE 1 for this one:

While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
;Convert from UNICODE to ANSI
    Case FileGetTime($File1,0,1) <> $FGT
        MsgBox(0,"WARNING...", "Files are different - executing UNICODE conversion")
; Run UNICODE to ANSI conversion
        RunWait(@ComSpec & ' /c Type "' & $File1 & '" > "' & $File2 & '"',"",@SW_HIDE); Overwrites the ANSI txt file each time
;Open files
        $ANSI = FileOpen($File2, 0); Opens the ANSI txt file
        $DIFF = FileOpen($File3, 1); Opens the DIFF file
        If $ANSI = -1 Then
            MsgBox(0, "Error", "Unable to open " & $File2)
            Exit
        ElseIf $DIFF = -1 Then
            MsgBox(0, "Error", "Unable to open " & $File3)
            Exit
        EndIf   
        While $CountLines1 + 1 <> $StartRead; ===================================================
    ; Read a line of data           ;
            $Text1 = FileReadLine($ANSI,$StartRead);
            GUICtrlSetData($Label1,$Text1)  ;    This is supposed to be a nested While...WEnd
    ;Write data to DIFF file        ;
            FileWriteLine($DIFF, $Text1)    ;
            $StartRead = $StartRead +1      ;
        WEnd                                ; ===================================================
;Close files
        FileClose($ANSI)
        FileClose($DIFF)
        $FGT = FileGetTime($File1,0,1); updates the varible with the current time
       
        Case Else
       
        EndSelect
WEnd
The only change that I did is:

1) Switch RUN() for RUNWAIT() this way you will no have problem open ANSI file before the run command finish.

2) I remove one WHILE() statement and replaced for CASE

3) The real problem the why it was in an infinit loop is in this line after the FileClose():

$FTG = FileGetTime($File1,0,1); updates the varible with the current time

should be $FGT

Edited by Danny35d
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

The only change that I did is:

1) Switch RUN() for RUNWAIT() this way you will no have problem open ANSI file before the run command finish.

2) I remove one WHILE() statement and replaced for CASE

3) The real problem the why it was in an infinit loop is in this line after the FileClose():

$FTG = FileGetTime($File1,0,1); updates the varible with the current time

should be $FGT

Ok,

1) Done - helped, and adding a sleep(1000) got me closer

2) Don't completely understand why.... ????

3) Done ... typo

now I have a problem with

D:\Program Files\AutoIt3\Include\file.au3 (27) : ==> Invalid file handle used.: 
Return StringLen(StringAddCR(FileRead($sFilePath, $N))) - $N + 1

Current code:

; UNICODE to ANSI conversion and then reads the Ansi data
; Description: Use to convert (copy) files saved with Unicode to Ansi
; Also changes the Label to the current line of data
; 3 Files are needed in the 'C:\' directory - or modify to your needs
; Example.log - saved as Unicode with some sample data
; Example.txt - saved as Ansi (empty)
; Log.txt - saved as Ansi (empty)

; Once you start the script go add some data to Example.log with notepad (close and save)
; Once this is done the program will notify you that the file has changed and start processing

;Incluses
#include <file.au3>
#include <Process.au3>
#include <GUIConstants.au3>

;Set variables
$Path = "C:\"
$FN1 = "Example.log"; UNICODE file
$FN2 = "Example.txt"; ANSI file
$FN3 = "Log.txt"; DIFF file
$UNICODE = $Path & $FN1; Combine path & name
$ANSI = $Path & $FN2; Combine path & name
$DIFF = $Path & $FN3; Combine path & name
$FGT = FileGetTime($UNICODE, 0, 1); Get timestamp of $UNICODE

; Wait until UNICODE is created - continue if already exist

; Convert UNICODE to ANSI
RunWait(@ComSpec & ' /c Type "' & $UNICODE & '" > "' & $ANSI & '"', "", @SW_HIDE); Converts UNICODE to ANSI

$CL2 = _FileCountLines($ANSI); Count lines in ANSI file
$CL3 = _FileCountLines($DIFF); Count lines in DIFF file
$CountDiff = $CL2 - $CL3; Calculate difference in ANSI and DIFF files
$StartRead = ($CL2 - $CountDiff) + 1; Calculate point to start reading new data

; Show information
MsgBox(0, "", $FN2 & " has " & $CL2 & " lines." & @CRLF & $FN3 & " has " & $CL3 & " lines." & @CRLF & "a difference of " & $CountDiff & " lines." & @CRLF & "Reading starts on line " & $StartRead & "." & @CRLF & $FGT & @CRLF & FileGetTime($UNICODE, 0, 1))

; Looks fine so far

;The form used
$Form1 = GUICreate("", 191, 58, 192, 125)
$Label1 = GUICtrlCreateLabel("", 24, 16, 139, 17)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case Else
            While FileGetTime($UNICODE, 0, 1) <> $FGT
                MsgBox(0, "WARNING...", "File has been modified - executing UNICODE conversion")
            ; Run UNICODE to ANSI conversion
                RunWait(@ComSpec & ' /c Type "' & $UNICODE & '" > "' & $ANSI & '"', "", @SW_HIDE); Overwrites the ANSI txt file each time
            ;Open files
                $ANSI = FileOpen($ANSI, 0); Opens the ANSI txt file
                $DIFF = FileOpen($DIFF, 1); Opens the DIFF file
                If $ANSI = -1 Then
                    MsgBox(0, "Error", "Unable to open " & $ANSI)
                    Exit
                ElseIf $DIFF = -1 Then
                    MsgBox(0, "Error", "Unable to open " & $DIFF)
                    Exit
                EndIf
                While $CL2 + 1 <> $StartRead
                ; Read a line of data
                    $Text1 = FileReadLine($ANSI, $StartRead)
                    GUICtrlSetData($Label1, $Text1)
                    Sleep(1000)
                ;Write data to DIFF file
                    FileWriteLine($DIFF, $Text1)
                    $StartRead = $StartRead + 1
                WEnd
            ; Works fine until here - it then exits ... no explaination
            ;Close files
                FileClose($ANSI)
                FileClose($DIFF)

??????????????????????????????????????????????????????????????              
????????????????????? exits right here ???????????????????????
??????????????????????????????????????????????????????????????
                
            ;Update varibles
                $CL2 = _FileCountLines($ANSI);Count lines in ANSI file
                $CL3 = _FileCountLines($DIFF);Count lines in DIFF file
                $CountDiff = $CL2 - $CL3;Calculate difference in ANSI and DIFF files
                $StartRead = ($CL2 - $CountDiff) + 1;Calculate point to start reading new data
                $FGT = FileGetTime($UNICODE, 0, 1); updates the varible with the current time
            ;MsgBox(0,"","made it to here...")
            ; Show updated information
                MsgBox(0, "", $FN2 & " has " & $CL2 & " lines." & @CRLF & $FN3 & " has " & $CL3 & " lines." & @CRLF & "a difference of " & $CountDiff & " lines." & @CRLF & "Reading starts on line " & $StartRead & "." & @CRLF & $FGT)
            WEnd
    EndSelect
WEnd


Exit

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Link to comment
Share on other sites

Fossil Rock I copy and paste your code and run find on my computer when I used the beta version of AutoIt. The only time I get the same error is when I try to run your script with the AutoIt reales 3.1.1

Edited by Danny35d
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

D:\Program Files\AutoIt3\Include\file.au3 (27) : ==> Invalid file handle used.:
Return StringLen(StringAddCR(FileRead($sFilePath, $N))) - $N + 1

AutoIt is installed on 'D:' - The error is specific to line (27) in <file.au3>.

When it gets to the location marked and exits, it doesn't make sense (to me).

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Link to comment
Share on other sites

Ok, I've isolated it to the 2 lines below, but still don't know why it fails. The 2 lines are used at the beginning of the script with no error.

$CL2 = _FileCountLines($ANSI);Count lines in ANSI file
    $CL3 = _FileCountLines($DIFF);Count lines in DIFF file
Edited by Fossil Rock

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Link to comment
Share on other sites

Ok, I wrote a little script to isolate the problem.

It appears that the Func to count the lines only works 1 time. Test this code and see if you get 2 msgboxes.

; Set variables
$Path = "C:\"
$FN2 = "Example.txt"; ANSI file
$FN3 = "Log.txt"; DIFF file
$ANSI = $Path & $FN2; Combine path & name
$DIFF = $Path & $FN3; Combine path & name
$CL2 = _FCL($ANSI); Count lines in ANSI file   <:::::::::::::::: Works
$CL3 = _FCL($DIFF); Count lines in DIFF file   <:::::::::::::::: Doesn't Work

Func _FCL($Var)
    Local $N = FileGetSize($Var) - 1
    If @error Or $N = -1 Then Return 0
    Local $FCL = StringLen(StringAddCR(FileRead($Var, $N))) - $N + 1
    MsgBox(0,"",$Var & " Contains " & $FCL & " lines.")
EndFunc; ==>_FCL

Exit

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

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