realshyfox Posted May 16, 2017 Share Posted May 16, 2017 Hy, I wanna to write a file with a single line but put some strings at a certain position let´s say line 1 - position 67. And if it doesnt have the length required add some blank spaces to the final string. Learn, learn and ... learn Link to comment Share on other sites More sharing options...
Subz Posted May 16, 2017 Share Posted May 16, 2017 (edited) 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 May 16, 2017 by Subz Link to comment Share on other sites More sharing options...
spudw2k Posted May 17, 2017 Share Posted May 17, 2017 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. Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc Cool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF Link to comment Share on other sites More sharing options...
Malkey Posted May 18, 2017 Share Posted May 18, 2017 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 realshyfox 1 Link to comment Share on other sites More sharing options...
realshyfox Posted May 31, 2017 Author Share Posted May 31, 2017 This is what I was looking for. Thank you Malkey Learn, learn and ... learn Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now