MrNice21

Find text @ file and edit this

11 posts in this topic

hi all

i want help

i will code a find text at "test.txt" (234) and edit (replace) this to 43234242 how can i make this ? help me plz

thx

Share this post


Link to post
Share on other sites



I would love to help, but I just don't understand your question.

If English is not your native language, try posting in German/French/Swahili/watever. There are people from all over who visit here and they may be able to help/translate for you.


[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

hi all

i want help

i will code a find text at "test.txt" (234) and edit (replace) this to 43234242 how can i make this ? help me plz

thx

Ich möchte einfach ein prog coden was eine datei öffnet z.b. test.dll und in dieser datei einen bestimmten text z.b. 234 sucht und ihn in 234243423 ändert mehr net hoffe mir kann einer helfen danke

ja english is net so mein ding lol

*****************

is this better ? :)

how can i make a prog was open a file (abc.dll) and find in this

file a address "32323" and change this address to "34543535".

plz help thx

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

So you want to replace text found in a text file?

Here is what I would do:

1. Read the file and store it in an array

2. Use StringReplace to replace the text you want to be replaced with (makes sense)

3. Overwrite the file you first read from

This works:

#include <File.au3>
Dim $aRecords
Global $File = "text.txt"
If Not _FileReadToArray($File, $aRecords) Then
    MsgBox(4096, @ScriptName, "There was an error reading file!")
    Exit
EndIf
For $x = 1 To $aRecords[0]
    $Replaced = StringReplace($aRecords[$x], "hallo", "hello")
    MsgBox(0, @ScriptName, $Replaced)
Next
$New = FileOpen($File, 2)
FileWrite($New, $Replaced)
FileClose($New)

Create a new file in the same directory as the script called "text.txt" in that file write "hello" (without quotes). Run the script. Check the file!

Just modified some code from the Helpfile :)

Edited by JamesB

Share this post


Link to post
Share on other sites

Here is a better script which I made into a function!

#include <File.au3>
;#include <Array.au3>

ReplaceInFile(@ScriptDir & '\text.txt', "hello", "hallo")

Func ReplaceInFile($io_file, $io_word, $io_replacement)
    Dim $Records
    $Lines = _FileCountLines($io_File)
    If Not _FileReadToArray($io_file, $Records) Then
        ConsoleWriteError("There was an error reading > " & $io_file & @CRLF)
        Exit
    ElseIf _FileReadToArray($io_file, $Records) == "" Then
        ConsoleWriteError("File was found to be blank!" & @CRLF)
    ElseIf Not @error Then
        ConsoleWrite("> File was all good!" & @CRLF)
    EndIf
    $File = FileOpen($io_file, 2)
; DEBUG = ConsoleWrite("+> Found: " & $Lines & " lines!" & @CRLF)
; DEBUG = _ArrayDisplay($Records)
    For $ax = 1 To $Records[0]
        ConsoleWrite("+> " & $Records[$ax] & @CRLF)
        $Replace = StringReplace($Records[$ax], $io_word, $io_replacement)
        ConsoleWrite("+> " & $Replace & @CRLF)
    ;For $ay = $Lines To UBound($Lines) - 1
    ;   FileWrite($File, $Replace)
    ;Next
        If _FileCountLines($io_file) < UBound($Lines) - 1 Then
            FileWrite($File, $Replace & @CRLF)
        Else
            FileWrite($File, $Replace)
        EndIf
        If Not @error Then
            ConsoleWrite("> Replaced String!" & @CRLF)
        Else
            ConsoleWriteError("Error replacing text!" & @CRLF)
        EndIf
    Next
    FileClose($File)
EndFunc

Same instructions as before :)

Share this post


Link to post
Share on other sites

very very nice big thx for help nice forum!

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Hello all,

I tried adapting James's function to something I'm trying to do--changing a constant text string in an XQuery file. It works great as far as the text string is concerned, but unfortunately, it strips out all line breaks. Being a total newbie, I'm not sure whether this happens at the time of reading the file into the array or at the time of output, even less how to fix it. Can anyone point me in the right direction?

 

Many thanks!

 

Edit: I hacked it, without really understanding what I'm doing: I just took out the condition and it works:

;If _FileCountLines($io_file) < UBound($Lines) - 1 Then
            FileWrite($File, $Replace & @CRLF)
        ;Else
            ;FileWrite($File, $Replace)
        ;EndIf

Edited by RH

Share this post


Link to post
Share on other sites

This thread is 7 year old. Don't you think it's time to start one from scratch and expose your actual problem in full grace?


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

RH,

I agree with jchd - please start a new thread and explain a little more clearly the problem you are having.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.