Jump to content

How to replace a line of text in a text file?


Recommended Posts

Is there a way to replace a line of text in a text file using AutoIt? I want to replace "XXXXX" in a text file with a file name such as "File=C:\video.m2v".

This has been covered before. You should have tried searching first. If the file is an .ini , there are function specificly for it and they all can be found in the help file. If the neither search nor the help file does it, try posting the text file you want to change and we'll go from there. :lmao:
Link to comment
Share on other sites

Do a _FileReadtoArray, there's where you should start.

Need I explain more for the rest?

The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

Link to comment
Share on other sites

I wasn't able to get _FileReadToArray to work for me yet and I need to do something else now. But, I did find another way to do it. This uses a free program called TedNPad.exe which is a NotePad alternative.

;repalces this text "XXXXX" with the this text "File=C:\video.m2v" in the text file "c:\project.txt"

$insert_this_text = "File=C:\video.m2v"
run ( "TedNPad.exe" )
sleep(200)
send ("^o")
send ("c:\project.txt",1)
send ("{ENTER}")
sleep(200)
send ("^r")
send ("XXXXX" ,1)
send ("{TAB}")
send ($insert_this_text ,1)
send ("{TAB}")
send ("{TAB}")
send ("{TAB}")
send ("{ENTER}")
send ("{TAB}")
send ("{TAB}")
send ("{ENTER}")
send ("^s")
sleep(200)
send ("{ESC}")
exit
Link to comment
Share on other sites

not tested... but should work

Dim $aRecords
Dim $aFile = "c:\project.txt"
Dim $Old_info = "XXXXX"
Dim $New_info = "File=C:\video.m2v"


GuiCreate("MY GUI")
$input = GUICtrlCreateEdit("" , 20, 20, 350, 300)

If Not _FileReadToArray($aFile,$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
    If StringInStr($aRecords[$x], $Old_info) Then $aRecords[$x] = $New_info
    GUICtrlSetData( $input, $aRecords[$x] & @CRLF, 1)
Next

$Check= GUICtrlCreateButton("Exit", 150,350,100,30)
GUISetState(); display the GUI

While 1
    $msg = GUIGetMsg()
  
    Select
        
        Case $msg= $Check
            Exit
        Case $msg= $GUI_EVENT_CLOSE
            Exit
            
    EndSelect
WEnd

8)

NEWHeader1.png

Link to comment
Share on other sites

Valuater,

This is what I ended up with. Your suggestion failed to replace text but showed me what to do. Thanks for your help (I needed it). By the way, that was a very neat GUI suggestion.

; Creates new text file that has the text "XXXXX" replaced with the text "ABCDE"

#include <GUIConstants.au3>
#include<file.au3>
#include<array.au3>
Dim $text_array
$old_file = "project.txt"
$new_info = "ABCDE"
$old_info = "XXXXX"
FileOpen($old_file,0)
If Not _FileReadToArray($old_file,$text_array) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   FileClose($old_file)
   Exit
EndIf
FileDelete ("new_project.txt")
$new_file = FileOpen("new_project.txt",1)
For $x = 1 to $text_array[0]
    $match = StringInStr($text_array[$x], $old_info) 
    If $match<>0 Then $text_array[$x] = StringReplace ( $text_array[$x], $old_info, $new_info )     
    FileWrite($new_file, $text_array[$x] & @CRLF )  
Next
FileClose($new_file)
FileClose($old_file)
Edited by peter1234
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...