Jump to content

write string at certain position in a line


Recommended Posts

You can use _FileWriteToLine to insert a new line.  You could then use FileReadToArray to get the last line and check if it only includes whites whitespace and if not add an @CRLF to the end of the file.

Actually just remembered FileReadToArray won't return white space, so please try this example instead:

#include <File.au3>
Local $sFileName = @ScriptDir & "\Filename.txt"
_FileWriteToLine($sFileName, 3, "Inserted Line 3")
Local $sFileData = FileRead($sFileName)
$sFileData = StringStripWS($sFileData, 2)
Local $hFileName = FileOpen($sFileName, 2)
    FileWrite($sFileName, $sFileData & @CRLF)
FileClose($hFileName)

Filename.txt

Line 1
Line 2
Line 4
Line 5

 

Edited by Subz
Link to post
Share on other sites

StringLen will allow you to measure a line.  FileReadLine will allow you to read a single line in a multi-line file, as long as each line is separated by a line feed char (or carriage return I believe).  There is no one-stop function I am aware of to do what you want, so you'll have to build some logic to measure the string and insert/append/replace as necessary.

Link to post
Share on other sites

Here are two methods.

#include <File.au3>
#include <String.au3>

Local $sFileName = @ScriptDir & "\Filename.txt"
If FileExists($sFileName) Then FileDelete($sFileName)
FileWrite($sFileName, "12345678901234567890" & @CRLF & _ ; 78901234567890
        "Line 2 " & @CRLF & _
        "Line 3" & @CRLF & _
        "Line 4")

_FileInsertStringAtPosOnlineNo($sFileName, 1, 10, "Insert Example") ; Reg Exp to get line.
; Or
;_FileInsertStringAtPosOnlineNoA($sFileName, 1, 10, "Insert Example") ; Array to get line.

ShellExecute($sFileName)


Func _FileInsertStringAtPosOnlineNo(ByRef $sFilePath_Name, $iLineNo, $iCharPosInsert, $sInsertText)
    Local $sFileDataLine = StringRegExpReplace(FileRead($sFilePath_Name), "^(?s)(\V*\v*){" & $iLineNo - 1 & "}(\V+).*$", "${2}")
    Local $sNewLine = StringLeft(StringLeft($sFileDataLine, $iCharPosInsert - 1) & _StringRepeat(' ', $iCharPosInsert), $iCharPosInsert - 1) & $sInsertText & StringTrimLeft($sFileDataLine, $iCharPosInsert - 1)
    _FileWriteToLine($sFilePath_Name, $iLineNo, $sNewLine, True)
EndFunc   ;==>_FileInsertStringAtPosOnlineNo


Func _FileInsertStringAtPosOnlineNoA(ByRef $sFilePath_Name, $iLineNo, $iCharPosInsert, $sInsertText)
    Local $arr = FileReadToArray($sFilePath_Name)
    $arr[$iLineNo - 1] = StringLeft(StringLeft($arr[$iLineNo - 1], $iCharPosInsert - 1) & _StringRepeat(' ', $iCharPosInsert), $iCharPosInsert - 1) & $sInsertText & StringTrimLeft($arr[$iLineNo - 1], $iCharPosInsert - 1)
    _FileWriteFromArray($sFilePath_Name, $arr)
EndFunc   ;==>_FileInsertStringAtPosOnlineNoA

 

Link to post
Share on other sites
  • 2 weeks later...

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...