Jump to content
Sign in to follow this  
Sebix

Edit Text

Recommended Posts

Sebix

Hello! i am creating a script.

This script, in one of his functions, have the work to edit a .txt.

For example, in the Txt say this:

Hello, my name is Alberto, and i have 60 years old. I can speak spanish, and portuguese.

So, the GUI, for example, is the next:

NAME : [ ]

Age: [ ]

Languages: (Checkbox)

[ ] Spanish

[ ] Portuguese

[ ] English

I need to edit the First Line.

If the user put the name John, i need to change the word "Alberto" to "John".

If the user put 10 years old, i need to change the numbers 60 to 10.

And if the user select only spanish, the phrase ", and portuguese" will be delete, or

if the user select Spanish, Portuguese and English, the word English will be put next to the word "Portuguese".

Sorry for my english, i'm from argentina. lol

Thanks, and i'll be waiting for a response

Bye

Edited by Sebix

Share this post


Link to post
Share on other sites
ileandros

Hello! i am creating a script.

This script, in one of his functions, have the work to edit a .txt.

For example

How about posting that example??? Would make it more easy for some1 to help you...


I feel nothing.It feels great.

Share this post


Link to post
Share on other sites
Danyfirex

If you always know the name could use something like this.

$string="Hello, my name is Alberto, and i have 60 years old. I can speak spanish, and portuguese."

Local $text = StringReplace($string, "Alberto", "Danyfirex")
Local $numreplacements = @extended
MsgBox(0, "New string is", $text)
MsgBox(0, "The number of replacements done was", $numreplacements)
  • Like 1

Share this post


Link to post
Share on other sites
ileandros

$string="Hello, my name is Alberto, and i have 60 years old. I can speak spanish, and portuguese."
$nameinput = GUICtrlCreateInput("",100,50)
Local $text = StringReplace($string, "Alberto", GUICtrlRead($nameinput)) ;This is for the name
;You can do this for all the things you want to change
MsgBox(0, "The number of replacements done was", $text)

Edited by ileandros
  • Like 1

I feel nothing.It feels great.

Share this post


Link to post
Share on other sites
Sebix

Mmmm, when i'm talking about "txt", i'm talking about a file ".txt".

So:

$Portuguese = GUICtrlCreateCheckbox("Portuguese", 10, 10)

While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
Exit
Case $Start
Local $File = FileOpen("text.txt", 0)
;What i need put? In the Text.txt is the phrase Hello, my name is Alberto, and i have 60 years old. I can speak spanish, and portuguese.
FileClose($File)
EndSwitch
WEnd

I think I misspoke.

Sorry for that. Now I explain correctly?

Edited by Sebix

Share this post


Link to post
Share on other sites
Danyfirex

Remember you can use FileRead to load the text into a variable.

  • Like 1

Share this post


Link to post
Share on other sites
JohnOne

There are many ways to do what you want to, depending on what functions you are comfortable with.

here is a start example using an array, you will notice that name is always element 5 etc..

#Include <Array.au3>

$Original_String = "Hello, my name is Alberto, and i have 60 years old. I can speak spanish, and portuguese."

$New_String = ""

$aSplit_String = StringSplit($Original_String, " ")

_ArrayDisplay($aSplit_String) ; just for debug

$New_Name = "Freddy"

$aSplit_String[5] = $New_Name

For $i = 1 To $aSplit_String[0] ;number of elements in array
$New_String &= $aSplit_String[$i] & " "
Next

ConsoleWrite($New_String & @LF)

You can remove, add, or replace elements of the array depending on what, and how many checkboxes are true, that's just logic.

  • Like 1

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
Sebix

If the text was much more extensive, how would I do to find the phrase "Hello, my name is Alberto, and i have 60 years old. I can speak spanish, and portuguese.", After that modify the name as it is wrote in Input.

This is a generic code:

#include<GUIConstantsEx.au3>
#include<EditConstants.au3>

GuiCreate("Example",150,305)
GuiSetState(@SW_SHOW)
GuiCtrlCreateLabel("Name", 5, 20)
$Name = GUICtrlCreateInput("", 35, 15, 75, 25)
GuiCtrlCreateLabel("Age", 5, 60)
$Age = GUICtrlCreateInput("", 35, 55, 75, 25)
GUICtrlCreateLabel("Languages", 15, 100)
$English = GUICtrlCreateCheckbox("English", 15, 120)
$Spanish = GUICtrlCreateCheckbox("Spanish", 15, 140)
$Portuguese = GUICtrlCreateCheckbox("Portuguese", 15, 160)
$START = GUICtrlCreateButton("Start", 35, 225, 75, 25)

While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
Exit
Case $Start
_Modiffy()
EndSwitch
WEnd

Func _Modiffy()
FileOpen("text.txt")
FileRead
;String? I don't understand how go to the original phrase, and moddify that.
EndFunc

The" text.txt" will be:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

Duis venenatis velit et metus. Duis lacinia leo in velit.

Sed tristique. Hello, my name is Alberto, and i have 60 years old. I can speak spanish, and portuguese.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

Remember you can use FileRead to load the text into a variable.

Sos venezolano? Se me facilitaría mucho si hablás en castellano jaja Edited by Sebix

Share this post


Link to post
Share on other sites
ileandros

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
Global $language[3]
Global $test[3]
GuiCreate("Example",150,305)
GuiCtrlCreateLabel("Name", 5, 20)
$Name = GUICtrlCreateInput("", 35, 15, 75, 25)
GuiCtrlCreateLabel("Age", 5, 60)
$Age = GUICtrlCreateInput("", 35, 55, 75, 25)
GUICtrlCreateLabel("Languages", 15, 100)
$language[0] = GUICtrlCreateCheckbox("English", 15, 120)
$language[1] = GUICtrlCreateCheckbox("Spanish", 15, 140)
$language[2] = GUICtrlCreateCheckbox("Portuguese", 15, 160)
$START = GUICtrlCreateButton("Start", 35, 225, 75, 25)
GuiSetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
Exit
Case $Start
_Modiffy()
EndSwitch
WEnd
Func _Modiffy()
$file = FileRead("text.txt")
$str = "Hello, my name is Alberto, and i have 60 years old. I can speak spanish, and portuguese"
If StringInStr($file,$str) Then
 $aSplit_String = StringSplit($str, " ")
 $New_String = ""
 $New_Name = GUICtrlRead($Name)
 $New_Age = GUICtrlRead($Age)
 $aSplit_String[5] = $New_Name
 $aSplit_String[9] = $New_Age
 For $i = 1 To $aSplit_String[0] ;number of elements in array
  $New_String &= $aSplit_String[$i] & " "
 Next
 MsgBox(0,"New String", $New_String)
 EndIf
EndFunc

As for the languages use a case statement cuz there are a lot of possibilities

Edit: Changed it making it easier to understand i think. But there are a lot of ways to do it anyway

Edited by ileandros
  • Like 1

I feel nothing.It feels great.

Share this post


Link to post
Share on other sites
Sebix

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
Global $language[3]
Global $test[3]
GuiCreate("Example",150,305)
GuiCtrlCreateLabel("Name", 5, 20)
$Name = GUICtrlCreateInput("", 35, 15, 75, 25)
GuiCtrlCreateLabel("Age", 5, 60)
$Age = GUICtrlCreateInput("", 35, 55, 75, 25)
GUICtrlCreateLabel("Languages", 15, 100)
$language[0] = GUICtrlCreateCheckbox("English", 15, 120)
$language[1] = GUICtrlCreateCheckbox("Spanish", 15, 140)
$language[2] = GUICtrlCreateCheckbox("Portuguese", 15, 160)
$START = GUICtrlCreateButton("Start", 35, 225, 75, 25)
GuiSetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
Exit
Case $Start
_Modiffy()
EndSwitch
WEnd
Func _Modiffy()
$file = FileRead("text.txt")
$str = "Hello, my name is Alberto, and i have 60 years old. I can speak spanish, and portuguese"
If StringInStr($file,$str) Then
$aSplit_String = StringSplit($str, " ")
$New_String = ""
$New_Name = GUICtrlRead($Name)
$New_Age = GUICtrlRead($Age)
$aSplit_String[5] = $New_Name
$aSplit_String[9] = $New_Age
For $i = 1 To $aSplit_String[0] ;number of elements in array
$New_String &= $aSplit_String[$i] & " "
Next
MsgBox(0,"New String", $New_String)
EndIf
EndFunc

As for the languages use a case statement cuz there are a lot of possibilities

Edit: Changed it making it easier to understand i think. But there are a lot of ways to do it anyway

Yes, that is the question.

I only need to moddify the "Text.txt" for one time.

If i don't select the name, the original name will not change.

And if i select the age, for example 30, the original age (60), will be change to 30.

How can i search the phrase in the text, and edit that?

I know the file an row of the phrase, but i don't know how apply to this.

So i have the .exe, and the .txt in the same folder.

When i open the exe, put the name "Seba" and put start, the .txt will be change, and

when i open that, the name will be "Seba".

And stop the func, i need do this for one time.

But If you know a way to edit it having any words, for example from "Seba" to "Pepe", that will be great

PD: In your code, only generates a msgbox with the info? I need EDIT the .txt.

The most that I could do was this:

Local $file = FileOpen("text.txt", 1)

Func _modify
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file 'Text.txt'.")
Exit
EndIf

FileWriteLine($file, "Added")

FileClose($file)
Edited by Sebix

Share this post


Link to post
Share on other sites
ileandros

Change the tittle of the topic to "Write the code for me"

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <Array.au3>
#include <File.au3>
Global $language[3]
Global $test[3]
GuiCreate("Example",150,305)
GuiCtrlCreateLabel("Name", 5, 20)
$Name = GUICtrlCreateInput("", 35, 15, 75, 25)
GuiCtrlCreateLabel("Age", 5, 60)
$Age = GUICtrlCreateInput("", 35, 55, 75, 25)
GUICtrlCreateLabel("Languages", 15, 100)
$language[0] = GUICtrlCreateCheckbox("English", 15, 120)
$language[1] = GUICtrlCreateCheckbox("Spanish", 15, 140)
$language[2] = GUICtrlCreateCheckbox("Portuguese", 15, 160)
$START = GUICtrlCreateButton("Start", 35, 225, 75, 25)
GuiSetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
Exit
Case $Start
_Modiffy()
EndSwitch
WEnd
Func _Modiffy()
Local $open = FileOpen("text.txt",1)
$file = FileRead("text.txt")
$str = "Hello, my name is Alberto, and i have 60 years old. I can speak spanish, and portuguese"
If StringInStr($file,$str) Then
 $aSplit_String = StringSplit($str, " ")
 $New_String = ""
 $New_Name = GUICtrlRead($Name)
 $New_Age = GUICtrlRead($Age)
 $aSplit_String[5] = $New_Name
 $aSplit_String[9] = $New_Age
 For $i = 1 To $aSplit_String[0] ;number of elements in array
  $New_String &= $aSplit_String[$i] & " "
 Next
 _FileWriteToLine("text.txt",3,"Sed tristique. " & $New_String,1)
 EndIf
EndFunc
  • Like 1

I feel nothing.It feels great.

Share this post


Link to post
Share on other sites
Sebix

Change the tittle of the topic to "Write the code for me"

Do not get me wrong! I'm not interested in give me a code without explanations.

If quench my appetite with fish, without giving me a fishing rod, the next time I'll ask for more fish. That's my philosophy.

I learn to compare, understand and see things, I appreciate your support, what happens is that I am very started on this, and I understand little, coupled with my bad English, I'm lost.

Edit: Viewing your code, i remember a thing

Global $language[3]

$language[0] = GUICtrlCreateCheckbox("English", 15, 120)

$language[1] = GUICtrlCreateCheckbox("Spanish", 15, 140)

$language[2] = GUICtrlCreateCheckbox("Portuguese", 15, 160)

That i learn with scripting for the game Counter-Strike, in the AMXX, using Pawn language or C++, i don't remember.

That was the Array...

I have a question, how can do a call to all of the var's?

For example, for a reset of the checkbox's i'll do this.

Global $language[3]
$language[0] = GUICtrlCreateCheckbox("English", 15, 120)
$language[1] = GUICtrlCreateCheckbox("Spanish", 15, 140)
$language[2] = GUICtrlCreateCheckbox("Portuguese", 15, 160)
$RESET = GUICtrlCreateButton("Reset", 35, 260, 75, 25)

While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
Exit
Case $Start
_Modiffy()
Case $RESET
GUICtrlSetState($language[0], $GUI_UNCHECKED) ; how can i contract all of them in one call? for example GUICtrlSetState($language[3], $GUI_UNCHECKED)?
GUICtrlSetState($language[1], $GUI_UNCHECKED)
GUICtrlSetState($language[2], $GUI_UNCHECKED)
EndSwitch
WEnd

How can i say, the $language[1] (English) is = to "English, "

So when the user clicks in the checkbox, "English, " will be appear next to I can Speak ...

_FileWriteToLine("text.txt",3,"I can speak " & $New_String,1)

I know there are many possibilities, if the user click English, but don't click Spanish, the phrase will change, I know. But don't do problems, i only need moddify numbers in my script, using my example I can understand all more fastly

Thanks.

Edited by Sebix

Share this post


Link to post
Share on other sites
Sebix

GUICtrlSetState($language[0], $GUI_UNCHECKED) ; how can i contract all of them in one call? for example GUICtrlSetState($language[3], $GUI_UNCHECKED)?

GUICtrlSetState($language[1], $GUI_UNCHECKED)

GUICtrlSetState($language[2], $GUI_UNCHECKED)

I think this is another way to do the same, more contracted/Neat.

For $language[0] = 0 To $language[2]
GUICtrlSetState($language[0], $GUI_UNCHECKED)
Next

I understand everything up to this part.

$aSplit_String[5] = $New_Name ; I don't understand the array [5]
$aSplit_String[9] = $New_Age ; here too
For $i = 1 To $aSplit_String[0] ;number of elements in array
$New_String &= $aSplit_String[$i] & " " ; $aSplit_String[u][b][$i][/b][/u] <-  All this I have the edge of understanding
Next
Edited by Sebix

Share this post


Link to post
Share on other sites
Danyfirex

Sos venezolano? Se me facilitaría mucho si hablás en castellano jaja

jaja es un foro en ingles.

well I modificate a little of you code.

#include<GUIConstantsEx.au3>
#include<EditConstants.au3>GuiCreate("Example",150,305)
GuiSetState(@SW_SHOW)
GuiCtrlCreateLabel("Name", 5, 20)
$Name = GUICtrlCreateInput("", 35, 15, 75, 25)
GuiCtrlCreateLabel("Age", 5, 60)
$Age = GUICtrlCreateInput("", 35, 55, 75, 25)
GUICtrlCreateLabel("Languages", 15, 100)
$English = GUICtrlCreateCheckbox("English", 15, 120)
$Spanish = GUICtrlCreateCheckbox("Spanish", 15, 140)
$Portuguese = GUICtrlCreateCheckbox("Portuguese", 15, 160)
$START = GUICtrlCreateButton("Start", 35, 225, 75, 25)
;No es una gran modificacion pero seguro te ayuda es mas una idea de la forma mas facil que lo podrias hacer.

While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
Exit
Case $Start
_Modiffy()
EndSwitch
WEnd

Func _Modiffy()
Local $file = FileOpen("test.txt", 0)

; Verificamos si el archivo se abre correctamtente.
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf


Local $texto = FileRead($file);$texto es igual al todo lo que contenga el archivo test.txt
FileClose($file);despues de almacenar todo cerramos el handle del archivo

$texto=StringReplace($texto,"Alberto",guictrlread($Name));remplazamos por el nombre en el cuadro de texto correspondiente a nombre
$texto=StringReplace($texto,"60",GUICtrlRead($Age))

;verificamos cual checkbox esta seleccionado y colocamos el idioma correspondiente
local $var=""
if GUICtrlRead($English) = $GUI_CHECKED then $var="i"

if GUICtrlRead($Spanish) = $GUI_CHECKED then $var&="e"

if GUICtrlRead($Portuguese) = $GUI_CHECKED then $var&="p"

;msgbox(0,"",$var)

Select
Case $var = "i"
$texto=StringReplace($texto,"English","ingles")
Case $var = "e"
$texto=StringReplace($texto,"English","Español")
Case $var = "p"
$texto=StringReplace($texto,"English","Portugues")
Case $var = "ie"
$texto=StringReplace($texto,"English","Ingles y Español")
Case $var = "iep"
$texto=StringReplace($texto,"English","Ingles, Español y Portugues")
Case $var = "ip"
$texto=StringReplace($texto,"English","Ingles y Portugues")
Case $var = "ep"
$texto=StringReplace($texto,"English","Español y Portugues")
Case Else
MsgBox(0, "", "No sabe nada")
EndSelect



;abrimos nuevo modo borrar lo existente
Local $file2 = FileOpen("test.txt", 2)
filewrite($file2,$texto) ;escribimos el nuevo contenido
FileClose($file2)
EndFunc

it a simple way to do that you want.

Modificalo a gusto. te coloque comentarios en Español. saludos

Edited by Danyfirex

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  

×