Jump to content



Photo

_FileWriteToLine


  • Please log in to reply
16 replies to this topic

#1 cdkid

cdkid

    El Spamino

  • Active Members
  • PipPipPipPipPipPip
  • 748 posts

Posted 16 February 2006 - 07:09 PM

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...
Plain Text         
;======================================== ;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, 21 February 2006 - 08:13 PM.

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!







#2 big_daddy

big_daddy

  • Moderators
  • 2,591 posts

Posted 16 February 2006 - 09:52 PM

This will come in handy, nice work!

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

#3 cdkid

cdkid

    El Spamino

  • Active Members
  • PipPipPipPipPipPip
  • 748 posts

Posted 16 February 2006 - 11:07 PM

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!

#4 MHz

MHz

    Just simple

  • MVPs
  • 5,737 posts

Posted 17 February 2006 - 01:11 AM

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

#5 cdkid

cdkid

    El Spamino

  • Active Members
  • PipPipPipPipPipPip
  • 748 posts

Posted 17 February 2006 - 03:56 AM

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, 17 February 2006 - 04:30 AM.

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!

#6 JOHNe

JOHNe

    Seeker

  • Active Members
  • 8 posts

Posted 18 February 2006 - 10:02 AM

Thankx just what I was looking for ...:o
"Our deepest fear is not that we are inadequate.Our deepest fear is that we are powerful beyond measure.It is our light, not our darkness that most frightens us."

#7 das_mc

das_mc

    Seeker

  • New Members
  • 1 posts

Posted 18 February 2006 - 11:01 AM

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)

#8 cdkid

cdkid

    El Spamino

  • Active Members
  • PipPipPipPipPipPip
  • 748 posts

Posted 18 February 2006 - 05:05 PM

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!

#9 randallc

randallc

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,902 posts

Posted 20 February 2006 - 06:17 AM

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, 20 February 2006 - 08:46 PM.


#10 Valuater

Valuater

    www.PayFreeWireless.com

  • MVPs
  • 11,279 posts

Posted 21 February 2006 - 07:30 PM

i am not positive... but pretty sure you need to set the error before your return

seterror()
return 0

8)

Posted Image


#11 cdkid

cdkid

    El Spamino

  • Active Members
  • PipPipPipPipPipPip
  • 748 posts

Posted 21 February 2006 - 08:12 PM

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!

#12 sasdad

sasdad

    Seeker

  • Active Members
  • 15 posts

Posted 01 April 2006 - 09:21 PM

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

#13 Jos

Jos

    Je maintiendrai

  • Developers
  • 23,829 posts

Posted 01 April 2006 - 09:31 PM

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

Visit the SciTE4AutoIt3 Download page for the latest versions                                                                 Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  smile.png


#14 sasdad

sasdad

    Seeker

  • Active Members
  • 15 posts

Posted 01 April 2006 - 09:36 PM

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, 01 April 2006 - 09:36 PM.


#15 sasdad

sasdad

    Seeker

  • Active Members
  • 15 posts

Posted 01 April 2006 - 09:37 PM

And also thank you for fast answer.

:)

#16 cdkid

cdkid

    El Spamino

  • Active Members
  • PipPipPipPipPipPip
  • 748 posts

Posted 01 April 2006 - 11:47 PM

(cdkid here)
:)

Sorry about that :mellow:

Edited by cdkid, 01 April 2006 - 11:48 PM.

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!

#17 Denver

Denver

    Seeker

  • Active Members
  • 7 posts

Posted 14 June 2008 - 09:38 PM

(*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 :
Plain Text         
;======================================== ;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 ? :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users