Jump to content

Problem with FileReadLine


Dym71
 Share

Recommended Posts

Hi,

I have problem with my script and I do not know how can I resolve this (probably simple) problem. I open 2 files and try read line by line and compare - if something is different then I copy files to target folder to check it later manually. I have problem with FileReadLine function because it always return -1 that means end-of-file is reached. Right now I changed my variables to Global but this is not a problem. Of course source files are OK and have a few hundred lines.

 

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

Global $BaseFolder = "C:\2017_BASE"
Global $ModFolder = "C:\2017_MOD"
Global $TargetFolder = "C:\MERGE"
Global $BaseFile
Global $ModFile
Global $Line1
Global $Line2

Global $FileList = _FileListToArray($BaseFolder, "*")
If @error = 1 Then
    MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
    Exit
EndIf
If @error = 4 Then
    MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
    Exit
EndIf

;For $i = 1 To UBound($FileList)
;Short test
For $i = 2 To 4
    If FileExists($BaseFolder & "\" & $FileList[$i]) Then
        If FileExists($ModFolder & "\" & $FileList[$i]) Then
            $BaseFile = FileOpen($BaseFolder & "\" & $FileList[$i],520)
            If @error <> 0 Then
                MsgBox(16,"Blad","Nie wybrano pliku lub jest on niepoprawny!" & @CRLF & $BaseFolder & "\" & $FileList[$i])
                Exit
            EndIf
            $ModFile = FileOpen($ModFolder & "\" & $FileList[$i],520)
            If @error <> 0 Then
                MsgBox(16,"Blad","Nie wybrano pliku lub jest on niepoprawny!" & @CRLF & $ModFolder & "\" & $FileList[$i])
                Exit
            EndIf
            While (True)
                $Line1 = FileReadLine($BaseFile,1)
                Local $Line1Error = @error
                $Line1Error = $Line1Error <> 0
                $Line2 = FileReadLine($ModFile)
                Local $Line2Error = @error
                $Line2Error = $Line2Error <> 0
                If ($Line1Error) And ($Line2Error) Then
                    FileClose($BaseFile)
                    FileClose($ModFile)
                    ExitLoop
                ElseIf (($Line1Error) And (Not $Line2Error)) Or ((Not $Line1Error) And ($Line2Error)) Then
                    FileClose($BaseFile)
                    FileClose($ModFile)
                    FileCopy($BaseFolder & "\" & $FileList[$i], $TargetFolder & "\" & StringLeft($FileList[$i],StringLen($FileList[$i])-4) & "_BASE.txt", 9)
                    FileCopy($ModFile & "\" & $FileList[$i], $TargetFolder & "\" & StringLeft($FileList[$i],StringLen($FileList[$i])-4) & "_MOD.txt", 9)
                    ExitLoop
                ElseIf StringCompare($Line1, $Line2) <> 0 Then
                    FileClose($BaseFile)
                    FileClose($ModFile)
                    FileCopy($BaseFolder & "\" & $FileList[$i], $TargetFolder & "\" & StringLeft($FileList[$i],StringLen($FileList[$i])-4) & "_BASE.txt", 9)
                    FileCopy($ModFile & "\" & $FileList[$i], $TargetFolder & "\" & StringLeft($FileList[$i],StringLen($FileList[$i])-4) & "_MOD.txt", 9)
                    ExitLoop
                EndIf
            WEnd
        Else
            FileCopy($BaseFolder & "\" & $FileList[$i], $TargetFolder & "\" & $FileList[$i], 9)
        EndIf
    EndIf
Next

Could you explain what is wrong with my script? Thank you.

Link to comment
Share on other sites

Try changing your FileOpen to this.

$BaseFile = FileOpen($BaseFolder & "\" & $FileList[$i])

Unless there's a specific reason to use ANSI to read the file, you shouldn't need to use it. Also, the 8 specifies that the path will be created if it doesn't exist which in this case is not at all necessary.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

1 hour ago, Dym71 said:

I open 2 files and try read line by line and compare

Since the files are only few hundreds lines, read the whole content of the files, and compare them, it will save you lot of code and complexity, like this :

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

Const $BaseFolder = "C:\2017_BASE\"
Const $ModFolder = "C:\2017_MOD\"
Const $TargetFolder = "C:\MERGE\"

Local $FileList = _FileListToArray($BaseFolder, "*", $FLTA_FILES)
If @error = 1 Then Exit MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
If @error = 4 Then Exit MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")

;For $i = 1 To UBound($FileList)
;Short test
For $i = 2 To 4
  If FileExists($ModFolder & $FileList[$i]) Then
    If FileRead ($BaseFolder & $FileList[$i]) = FileRead ($ModFolder & $FileList[$i]) Then ContinueLoop
    FileCopy($BaseFolder & $FileList[$i], $TargetFolder & StringLeft($FileList[$i],StringLen($FileList[$i])-4) & "_BASE.txt", 9)
    FileCopy($ModFolder & $FileList[$i], $TargetFolder & StringLeft($FileList[$i],StringLen($FileList[$i])-4) & "_MOD.txt", 9)
  Else
    FileCopy($BaseFolder & $FileList[$i], $TargetFolder & $FileList[$i], 9)
  EndIf
Next

(untested) but you will get the idea

Edited by Nine
increase readability and read only files not folders
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...