Jump to content

_FileWriteToLine


cdkid
 Share

Recommended Posts

Well, i've been wanting to write to a specific line for logging stuff w/o racking up a HUGE file so here is how i did it...

;========================================
;Function name:    _FileWriteToLine
;Description:        Write text to specified line in a file
;Parameters:
;                    $sFile - The file to write to
;                    $iLine - The line number to write to
;                    $sText - The text to write
;                    $fOverWrite - if set to 1 will overwrite the old line
;                    if set to 0 will not overwrite
;Requirement(s):      None
;Return Value(s):    On success - 1
;                     On Failure - 0 And sets @ERROR
;                                 @ERROR = 1 - File has less lines than $iLine
;                               @ERROR = 2 - File does not exist
;                               @ERROR = 3 - Error opening file
;                               @ERROR = 4 - $iLine is invalid
;                               @ERROR = 5 - $fOverWrite is invalid
;                               @ERROR = 6 - $sText is invalid
;Author(s):           cdkid
;Note(s):
;=========================================
Func _FileWriteToLine($sFile, $iLine, $sText, $fOverWrite = 0)
    If $iLine <= 0 Then
                                SetError(4)
        Return 0
    EndIf
    If Not IsString($sText) Then
        SetError(6)
        Return 0
    EndIf
    If $fOverWrite <> 0 And $fOverWrite <> 1 Then
        SetError(5)
                                Return 0
    EndIf
    If Not FileExists($sFile) Then
        SetError(2)
        Return 0
    EndIf
    $filtxt = FileRead($sFile, FileGetSize($sFile))
    $filtxt = StringSplit($filtxt, @CRLF, 1)
    If UBound($filtxt, 1) < $iLine Then
        SetError(1)
        Return 0
    EndIf
    $fil = FileOpen($sFile, 2)
    If $fil = -1 Then
                                SetError(3)
        Return 0
    EndIf
    For $i = 1 To UBound($filtxt) - 1
        If $i = $iLine Then
                                                if $fOverWrite = 1 then
            if $sText <> '' Then
            FileWrite($fil, $sText & @CRLF)
        Else
            filewrite($fil, $sText)
        EndIf
                                EndIf
            If $fOverWrite = 0 Then
                FileWrite($fil, $filtxt[$i] & @CRLF)
            EndIf
        ElseIf $i < UBound($filtxt, 1) - 1 Then
            FileWrite($fil, $filtxt[$i] & @CRLF)
        ElseIf $i = UBound($filtxt, 1) - 1 Then
            FileWrite($fil, $filtxt[$i])
        EndIf
    Next
    FileClose($fil)
    Return 1
EndFunc;==>_FileWriteToLine

:o

~cdkid

EDIT

updated it so i THINK it works with all the things MHz mentioned... tell me if i'm wrong

/EDIT

EDIT2

updated again, and have sent it in for the UDF library

/EDIT2

EDIT3

updated it with the version that has been sent in for the UDF library

/EDIT3

EDIT4

updated so if $sText is "" and $fOverWrite is 1 deletes line rather than replacing it with ""

/EDIT4

Edited by cdkid
AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!
Link to comment
Share on other sites

This will come in handy, nice work!

p.s. Where at in MO are you from?

thanks :o

oh and i live in Columbia, MO 1/2 ways between St Louis & Kansas City

AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!
Link to comment
Share on other sites

Nice, but If you check FileClose for @error, which FileClose does not support, so the Return value will always be 1. Another problem would be your StringSplit as @CRLF is 2 characters, so using the flag of 1 would split on the @CRLF only rather then @CR and @LF separately.

It should also be noted that this will not work with the current release of 3.1.1 as you use only 1 parameter on FileRead.

Just a few things above to tidy up on. :o

Link to comment
Share on other sites

thanks for the reply MHz, i'll work on that :o

edit

mmmk worked on it i think i fixed everything :geek:

~cdkid

Edited by cdkid
AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!
Link to comment
Share on other sites

Hi ,

I want to overwrite a special line in a file.

The line includes a special string.

When the script finds the string the script should overwrite the line with a new string

and I don't need any @errors and functions.

So I modified your first script for my purpose.

But I am not sure if that is a proper way to do this.

Here is the code:

$sFile              = "THE FILE"

$sText             = "THIS IS THE NEW TEXT"

$search           = 'Located the "string"'

   

    $filtxt = FileRead($sFile, FileGetSize($sFile))

    $filtxt = StringSplit($filtxt,@CRLF,1)

   

    $iLine = 0

    $temp = 0

    for $i = 1 to $filtxt[0]

        $temp = StringInStr($filtxt[$i],$search)

           if  $temp <> 0 Then

           $iLine =  $i

           EndIf

    Next

  

$filtxt[$iLine] =  $sText

$fil = FileOpen($sFile,2)

    for $i = 1 to $filtxt[0]

        FileWrite($fil,$filtxt[$i] & @CRLF)

    Next

fileclose($fil)

Link to comment
Share on other sites

Hi ,

I want to overwrite a special line in a file.

The line includes a special string.

When the script finds the string the script should overwrite the line with a new string

and I don't need any @errors and functions.

So I modified your first script for my purpose.

But I am not sure if that is a proper way to do this.

Here is the code:

$sFile              = "THE FILE"

$sText             = "THIS IS THE NEW TEXT"

$search           = 'Located the "string"'

   

    $filtxt = FileRead($sFile, FileGetSize($sFile))

    $filtxt = StringSplit($filtxt,@CRLF,1)

   

    $iLine = 0

    $temp = 0

    for $i = 1 to $filtxt[0]

        $temp = StringInStr($filtxt[$i],$search)

           if  $temp <> 0 Then

           $iLine =  $i

           EndIf

    Next

  

$filtxt[$iLine] =  $sText

$fil = FileOpen($sFile,2)

    for $i = 1 to $filtxt[0]

        FileWrite($fil,$filtxt[$i] & @CRLF)

    Next

fileclose($fil)

ok, i replied to your email with the code i would use. for anyone else interested, here it is

$myfile = "put your file name here"
$file = FileRead($myfile, FileGetSize($myfile))
$file = StringSplit($file, @CRLF, 1)
for $i = 0 to $file[0] Step 1
      If StringInStr($file[$i], "put your search string here") Then
           _FileWriteToLine($myfile, $i, "my replacement string", 1)
      EndIf
Next

remember to include the file with _FileWriteToLine in it :o

~cdkid

AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!
Link to comment
Share on other sites

Hi,

i have updated with SQLite3.exe v 3.3.4 [sql UDF link in signature]

As well, new function for writing to a file line (replacing) is added;

This solution allows;

1. Opening huge files (eg >80Mb) which fileread method won't do; [Edit; filread opens; I presume stringsplit or the array..?? have problem with large file/ large number replacements?].

2. Quicker than filereadline/ writeline if large file with many replacement lines.

3. Option of being even quicker; transform data to SQLite db3, then replaces 2-3 secs even in huge database

best, Randall

Edited by randallc
Link to comment
Share on other sites

Thanks, val... updating now

AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!
Link to comment
Share on other sites

  • 1 month later...

Hi,

I think that there is an error in _FileWriteToLine function.

I made a file c:\test.txt with 10 lines

_FileWriteToLine("c:\test.txt", 3, "my replacement for line 3", 1) works great

_FileWriteToLine("c:\test.txt", 3, "my insertion", 0) doesn't work

Can you please fix it?

Thank you

Link to comment
Share on other sites

  • Developers

Hi,

I think that there is an error in _FileWriteToLine function.

I made a file c:\test.txt with 10 lines

_FileWriteToLine("c:\test.txt", 3, "my replacement for line 3", 1) works great

_FileWriteToLine("c:\test.txt", 3, "my insertion", 0) doesn't work

Can you please fix it?

Thank you

You are right... the code should read in file.au3 line 360

If $fOverWrite = 0 Then
                FileWrite($fil, $sText & @CRLF)
                FileWrite($fil, $filtxt[$i] & @CRLF)
            EndIf

Will fix for the next release....

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

You are right... the code should read in file.au3 line 360

If $fOverWrite = 0 Then
                FileWrite($fil, $sText & @CRLF)
                FileWrite($fil, $filtxt[$i] & @CRLF)
            EndIf

Will fix for the next release....

After reading source I made the same change and It worked.

It was small mistake :)

But I will not use it, till updated official beta version.

I can use overwrite.

Edited by sasdad
Link to comment
Share on other sites

(cdkid here)

:)

Sorry about that :mellow:

Edited by cdkid
AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!
Link to comment
Share on other sites

  • 2 years later...

(*trying to write in an English board for the first time* ["Coucou" from France. ^^'] )

Hello, cdkid. -and everyone.^^

I used "_FileWrieToLine()" in my script to generate a text-file (if not existing), including (exactly !) the line that I've send in option.

List of stuff modified : (tell me if it's an incorrect way of scripting. ^^)

- If the file does not pre-exist, it simply creates it, and insert the line in option in it

- If the line to write is "greater than number of lines", then simply add enough (blank) lines, attempting to write to the line selected

- The "first line" of the text is now usable

- There is no more "blank line" added in the end of file

Now, the script modified :

;========================================
;Function name:    _FileWriteToLine
;Description:        Write text to specified line in a file
;Parameters:
;                    $sFile - The file to write to
;                    $iLine - The line number to write to
;                    $sText - The text to write
;                    $fOverWrite - if set to 1 will overwrite the old line
;                    if set to 0 will not overwrite
;Requirement(s):      None
;Return Value(s):    On success - 1
;                    On Failure - 0 And sets @ERROR
; -No more-             @ERROR = 1 - File has less lines than $iLine
; -No more-             @ERROR = 2 - File does not exist
;                               @ERROR = 3 - Error opening file
;                               @ERROR = 4 - $iLine is invalid
;                               @ERROR = 5 - $fOverWrite is invalid
;                               @ERROR = 6 - $sText is invalid
;Author(s):        cdkid
;Note(s):          modified by Dinosaure
;=========================================
Func _FileWriteToLine($sFile, $iLine, $sText, $fOverWrite = 0)
    If $iLine <= 0 Then
        SetError(4)
         Return 0
     EndIf
     If Not IsString($sText) 
        SetError(6)
         Return 0
     EndIf
     If $fOverWrite <> 0 And $fOverWrite <> 1 Then
        SetError(5)
         Return 0
     EndIf
     If Not FileExists($sFile) Then
        FileWrite($sFile, @CRLF) ;Create the file with a blank line in it if not existing before
     EndIf
     Local $filtxt = FileRead($sFile, FileGetSize($sFile))
    $filtxt = StringSplit($filtxt, @CRLF, 1)
     If $filtxt[0] < $iLine Then
        ReDim $filtxt[$iLine + 1];Add blank Lines until enough Lines in the text file
     EndIf
     Local $fil = FileOpen($sFile, 2)
     If $fil = -1 Then
        SetError(3)
         Return 0
     EndIf
     For $i = 1 To UBound($filtxt) - 1
         If $i = $iLine Then
             If $fOverWrite = 1 Then
                 If $sText <> '' Then
                     If $i < UBound($filtxt, 1) - 1 Then        ;If this is the line to write to, option overwrite, there is text, and NOT 'last line'
                        FileWrite($fil, $sText & @CRLF)
                     ElseIf $i = UBound($filtxt, 1) - 1 Then    ;If this is the line to write to, option overwrite, there is text, and this IS 'last line'
                        FileWrite($fil, $sText)
                     EndIf
                 Else
                     If $i < UBound($filtxt, 1) - 1 Then        ;If this is the line to write to, option overwrite, there is NO text
                        FileWrite($fil, $sText)
                     EndIf
                 EndIf
             EndIf
             If $fOverWrite = 0 Then
                 If $i < UBound($filtxt, 1) - 1 Then        ;If this is the line to write to, no overwrite, and NOT 'last line'
                    FileWrite($fil, $sText & @CRLF)
                    FileWrite($fil, $filtxt[$i] & @CRLF)
                 ElseIf $i = UBound($filtxt, 1) - 1 Then    ;If this is the line to write to, no overwrite, and this IS 'last line'
                    FileWrite($fil, $sText & @CRLF)
                    FileWrite($fil, $filtxt[$i])
                 EndIf
             EndIf
         ElseIf $i < UBound($filtxt, 1) - 1 Then
            FileWrite($fil, $filtxt[$i] & @CRLF)
         ElseIf $i = UBound($filtxt, 1) - 1 Then
            FileWrite($fil, $filtxt[$i])
         EndIf
    Next
    FileClose($fil)
     Return 1
EndFunc  ;==>_FileWriteToLine

Now I'm reading the modified script, there is an other way to add "last line" blank or not before the block "For..Next" : testing If "$Line" = "Last Line", then (nothing) else (add "@CRLF" at the end of $sText), and remove the lines of testing in the rest of the block. (Just write "$sText" or add "$sText" once, depending on "overwrite" at the line selected.)

Reacts ..?

(Pleeease, don't anwer that's awfull ! :P -I'm beginner in scripting..)

Denver.

P.S : The last comment is from 2006... Never mind if it's a long time ago ? :)

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