nf67 Posted July 7, 2009 Share Posted July 7, 2009 Hi,I was wondering how I can replace a certain line in a file with something else.I need to change the value listed after something.The file which I need to modify looks somewhat like this:This is some textAnd another line of text after which the "index" starts:Value 1 267Value 2 291Value 3 778Value 4 771Value 5 335So I could either say "replace the piece of text after "Value X" with " Y" (no idea how to do this), or something which seems easier:Replace line $ValueNumber + 2 with "Value " & $ValueNumber & " " & $ValueIn which the $ValueNumber (to change) and the $Value (to change it to) could be collected through inputs.Help would be greatly appreciated Link to comment Share on other sites More sharing options...
water Posted July 7, 2009 Share Posted July 7, 2009 (edited) It depends on the size of the file. If it's not too big would use something like:_FileReadToArray("C:\temp\test.txt",$aArray) For $x = 1 to $aArray[0] If StringLeft($aArray[$x],7) = "Value " & $ValueNumber+2 Then $aArray[$x] = "Value " & $ValueNumber & " " & $Value Exitloop EndIf Next _FileWriteFromArray("C:\temp\test_altered.txt",$aArray) Edited July 7, 2009 by water My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
nf67 Posted July 7, 2009 Author Share Posted July 7, 2009 If $aArray[$x] meets condition then change $aArray[$x] I'm having some problems with this... If StringInStr($aArray[$x], "Value " & $ValueNumber) then ...? Something like this? Link to comment Share on other sites More sharing options...
Yashied Posted July 7, 2009 Share Posted July 7, 2009 _FileWriteToLine()? My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More... Link to comment Share on other sites More sharing options...
water Posted July 7, 2009 Share Posted July 7, 2009 (edited) I modified my post #2 Edited July 7, 2009 by water My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
chMan Posted July 7, 2009 Share Posted July 7, 2009 Hi, Do you try StringRegExpReplace function ? Regards Ch'Man Link to comment Share on other sites More sharing options...
nf67 Posted July 7, 2009 Author Share Posted July 7, 2009 Thanks for your help so far guys I have yet to try things out. Link to comment Share on other sites More sharing options...
Malkey Posted July 7, 2009 Share Posted July 7, 2009 Hi, I was wondering how I can replace a certain line in a file with something else. I need to change the value listed after something. The file which I need to modify looks somewhat like this: QUOTE This is some text And another line of text after which the "index" starts: Value 1 267 Value 2 291 Value 3 778 Value 4 771 Value 5 335 So I could either say "replace the piece of text after "Value X" with " Y" (no idea how to do this), or something which seems easier: Replace line $ValueNumber + 2 with "Value " & $ValueNumber & " " & $Value In which the $ValueNumber (to change) and the $Value (to change it to) could be collected through inputs. Help would be greatly appreciated I put your QUOTE example into a file, "Test.txt". ; local $sFileName = "C:\Full Path\Desktop\Test.txt" local $sFile = FileRead($sFileName) local $iLineNum = 3 ; Get existing value $a = StringRegExp($sFile,"Value " & $iLineNum & " (\d{0,4})",3) ConsoleWrite("Existing Value = " & $a[0] & @CRLF & @CRLF) ;Replace with new value local $iNewValue = 101 local $sUpdatedFile = StringRegExpReplace($sFile,"(Value " & $iLineNum & ") (\d{0,4})","\1 " & $iNewValue) ConsoleWrite($sUpdatedFile & @CRLF) local $file = FileOpen($sFileName,2) FileWrite($file,$sUpdatedFile) FileClose($file) ; Link to comment Share on other sites More sharing options...
nf67 Posted July 7, 2009 Author Share Posted July 7, 2009 (edited) @Malkey $a = StringRegExp($sFile,"Value " & $iLineNum & " (\d{0,4})",3) What does it mean?I currently have this code:;VPP = ValueNumber ;Value = Value ;LineNumber = LineNumber #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Include <File.au3> $GUI = GUICreate("VPPC", 136, 22, 192, 124) $VPPinput = GUICtrlCreateInput("VPP #", 0, 0, 50, 21) $ValueInput = GUICtrlCreateInput("Value", 55, 0, 80, 21) GUISetState(@SW_SHOW) HotKeySet("{ENTER}", "GoCheck") While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func GoCheck() If WinActive("VPPC") Then Call("Go") EndIf EndFunc Func Go() $VPP = GUICtrlRead($VPPinput) $Value = GUICtrlRead($ValueInput) $LineNumber = $VPP + 2 _FileWriteToLine(@ScriptDir & "\VehiclesParam.tsc", $LineNumber, "VehiclePP "& $VPP & " " & $Value, 1) EndFuncThe above code does the trick, but only once, while there are actually multiple values (with the same number) that need to be changed.This only changes the 1st one...Thanks for your help :-) Edited July 7, 2009 by nf67 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