Jump to content

Read - Modify & Write


Recommended Posts

Hello,

I am trying to make an programm that would read from a file (1 string) modify it and than write it to another file .

Lets say the file we want to read the 1 st line is in thisformat <tryage>Somethink</tryage> and in the output file we want to make

it rewrite it into [boomsakalaka]Tryage[/boomsakalaka]

Any ideas if this is posible ? ;)

Thank you for your time

Link to comment
Share on other sites

Something like this?

#include <WindowsConstants.au3>
#include <GuiEdit.au3>

Global $sFile1 = "text.txt"
Global $sFile2 = "text2.txt"

If Not FileExists($sFile1) Then FileWrite($sFile1,"one plus one equals three")
If Not FileExists($sFile2) Then FileWrite($sFile2,"")

$Form1 = GUICreate("Text Modify", 633, 175, -1, -1)
$Input1 = GUICtrlCreateInput("three", 8, 8, 105, 21)
$Input2 = GUICtrlCreateInput("two", 118, 8, 105, 21)
$Input3 = GUICtrlCreateInput("", 180, 120, 150, 21)
$Button1 = GUICtrlCreateButton("Replace", 8, 34, 75, 25, 0)
$Button2 = GUICtrlCreateButton("Write1", 180, 145, 75, 25, 0)
$Button3 = GUICtrlCreateButton("Write2", 255, 145, 75, 25, 0)
$Edit1 = GUICtrlCreateEdit(FileRead($sFile1), 344, 8, 281, 81, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL))
$Edit2 = GUICtrlCreateEdit(FileRead($sFile2), 344, 90, 281, 81, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL))
GUISetState(@SW_SHOW)

While 1
    $read1 = FileRead($sFile1)
    $read2 = FileRead($sFile2)
    If $read1 <> GUICtrlRead($Edit1) Then GUICtrlSetData($Edit1, $read1)
    If $read2 <> GUICtrlRead($Edit2) Then GUICtrlSetData($Edit2, $read2)
    Switch GUIGetMsg()
        Case -3
            _Close()
        Case $Button1
            $open = FileOpen($sFile2, 2)
            If Not FileExists($sFile2) Then FileCopy($sFile1,$sFile2)
            FileWrite($open,StringReplace($read1, GUICtrlRead($Input1), GUICtrlRead($Input2)))
            FileClose($open)
        Case $Button2
            $open = FileOpen($sFile1, 2)
            FileWrite($open, GUICtrlRead($Input3))
            FileClose($open)
        Case $Button3
            $open = FileOpen($sFile2, 2)
            FileWrite($open, GUICtrlRead($Input3))
            FileClose($open)
    EndSwitch
WEnd

Func _Close()
    GUIDelete($Form1)
    FileDelete($sFile1)
    FileDelete($sFile2)
    Exit
EndFunc   ;==>_Close
Edited by Info
Link to comment
Share on other sites

Something like this?

#include <WindowsConstants.au3>
#include <GuiEdit.au3>

Global $sFile1 = "text.txt"
Global $sFile2 = "text2.txt"

If Not FileExists($sFile1) Then FileWrite($sFile1,"one plus one equals three")
If Not FileExists($sFile2) Then FileWrite($sFile2,"")

$Form1 = GUICreate("Text Modify", 633, 175, -1, -1)
$Input1 = GUICtrlCreateInput("three", 8, 8, 105, 21)
$Input2 = GUICtrlCreateInput("two", 118, 8, 105, 21)
$Input3 = GUICtrlCreateInput("", 180, 120, 150, 21)
$Button1 = GUICtrlCreateButton("Replace", 8, 34, 75, 25, 0)
$Button2 = GUICtrlCreateButton("Write1", 180, 145, 75, 25, 0)
$Button3 = GUICtrlCreateButton("Write2", 255, 145, 75, 25, 0)
$Edit1 = GUICtrlCreateEdit(FileRead($sFile1), 344, 8, 281, 81, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL))
$Edit2 = GUICtrlCreateEdit(FileRead($sFile2), 344, 90, 281, 81, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL))
GUISetState(@SW_SHOW)

While 1
    $read1 = FileRead($sFile1)
    $read2 = FileRead($sFile2)
    If $read1 <> GUICtrlRead($Edit1) Then GUICtrlSetData($Edit1, $read1)
    If $read2 <> GUICtrlRead($Edit2) Then GUICtrlSetData($Edit2, $read2)
    Switch GUIGetMsg()
        Case -3
            _Close()
        Case $Button1
            $open = FileOpen($sFile2, 2)
            If Not FileExists($sFile2) Then FileCopy($sFile1,$sFile2)
            FileWrite($open,StringReplace($read1, GUICtrlRead($Input1), GUICtrlRead($Input2)))
            FileClose($open)
        Case $Button2
            $open = FileOpen($sFile1, 2)
            FileWrite($open, GUICtrlRead($Input3))
            FileClose($open)
        Case $Button3
            $open = FileOpen($sFile2, 2)
            FileWrite($open, GUICtrlRead($Input3))
            FileClose($open)
    EndSwitch
WEnd

Func _Close()
    GUIDelete($Form1)
    FileDelete($sFile1)
    FileDelete($sFile2)
    Exit
EndFunc   ;==>_Close

Well thanks for this but i probably sayd what i didnt want to . Acually i wanted to make smthink that has format <Blablabla>$Needet</Blablabla> Appear <Somethink>$Needet</somethink> so the mid information stays is it possible? ;) Anyway thanks for your script above its an amazing example

Link to comment
Share on other sites

  • Moderators

ForsakenGod,

If all your lines are of the format <Blablabla>UseThisBit</Blablabla> with NO other <> characters, and you want them all to read <Somethink>UseThisBit<Somethink>, than the following should work:

#include <Array.au3>

; Simulate loading a file into an array with _FileReadToArray

Global $aFile[4] = [3, "<Blablabla>$Needet</Blablabla>", "<Blablabla>$Needet</Blablabla>", "<Blablabla>$Needet</Blablabla>"]

; Show what we have at the start
_ArrayDisplay($aFile, "Initial")

For $i = 1 To $aFile[0]

    $sMiddleText = StringRegExpReplace($aFile[$i], "(?i)<.*>(.*)<.*>", "$1")
    $aNewLine = "<Somethink>" & $sMiddleText & "<Somethink>"
    $aFile[$i] = $aNewLine

Next

; then use FileWriteToArray to rewrite the file

; Show what we have at the end
_ArrayDisplay($aFile, "Final")

Note that the _ArrayDisplay lines are just to show you the result and the #include <Array.au3> is only there for that function. You will need to add another include to getthe _File* functions to work whe you come to use them.

M23

P.S>

If you want to look really snazzy, than you can replace the 3 lines in the For...Next loop with just the one: ;)

$aFile[$i] = "<Somethink>" & StringRegExpReplace($aFile[$i], "(?i)<.*>(.*)<.*>", "$1") & "<Somethink>"

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

ForsakenGod,

If all your lines are of the format <Blablabla>UseThisBit</Blablabla> with NO other <> characters, and you want them all to read <Somethink>UseThisBit<Somethink>, than the following should work:

#include <Array.au3>

; Simulate loading a file into an array with _FileReadToArray

Global $aFile[4] = [3, "<Blablabla>$Needet</Blablabla>", "<Blablabla>$Needet</Blablabla>", "<Blablabla>$Needet</Blablabla>"]

; Show what we have at the start
_ArrayDisplay($aFile, "Initial")

For $i = 1 To $aFile[0]

    $sMiddleText = StringRegExpReplace($aFile[$i], "(?i)<.*>(.*)<.*>", "$1")
    $aNewLine = "<Somethink>" & $sMiddleText & "<Somethink>"
    $aFile[$i] = $aNewLine

Next

; then use FileWriteToArray to rewrite the file

; Show what we have at the end
_ArrayDisplay($aFile, "Final")

Note that the _ArrayDisplay lines are just to show you the result and the #include <Array.au3> is only there for that function. You will need to add another include to getthe _File* functions to work whe you come to use them.

M23

P.S>

If you want to look really snazzy, than you can replace the 3 lines in the For...Next loop with just the one: ;)

$aFile[$i] = "<Somethink>" & StringRegExpReplace($aFile[$i], "(?i)<.*>(.*)<.*>", "$1") & "<Somethink>"

Ohh mow i understand ! :evil: Thank you

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...