Sign in to follow this  
Followers 0
nf67

Replacing a line in a file.

9 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I modified my post #2

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Hi,

Do you try StringRegExpReplace function ?

Regards

Ch'Man

Share this post


Link to post
Share on other sites

Thanks for your help so far guys :) I have yet to try things out.

Share this post


Link to post
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)
  ;

Share this post


Link to post
Share on other sites

#9 ·  Posted (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)
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

Share this post


Link to post
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
Sign in to follow this  
Followers 0