Jump to content

Replacing a line in a file.


Recommended Posts

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 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 :)

Link to comment
Share on other sites

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 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 - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I modified my post #2

Edited 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 - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

@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)
EndFunc

The 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 by nf67
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...