Jump to content
dascondor

Copy and and replace Text From One file to another - (Moved)

Recommended Posts

So basically need to pull text from a .txt file or .sql and use that text to replace something within a different file.

 

First File:

jumps

 

Or:

Text1 = jumps

Text2 = fence

Second File:

My Dog XXXXX over the YYYYY.

 

 

After Script:

My Dog jumps over the fence

 

I have found a good start, I'm basic programmer at best, I have some experience but this gets out of my wheel house.

 

Any help is appreciated.

 

Quote
#include <File.au3>

Global $TestFile = @ScriptDir & "\test.txt"
Dim $MyArray

_FileReadToArray($TestFile, $MyArray)
For $i = 1 To $MyArray[0]
    If StringInStr($MyArray[$i], "street") Then $MyArray[$i] = StringReplace($MyArray[$i], "street", "road")
Next
_FileWriteFromArray($TestFile, $MyArray, 1)

 

 

Share this post


Link to post
Share on other sites

Moved to the appropriate forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions. If it's super geeky and you don't know where to put it - it's probably here.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

Share this post


Link to post
Share on other sites
Posted (edited)

Hi @dascondor, and welcome to the AutoIt forums :welcome:

The script you posted seems have the behaviour you described.

What should it do more? :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

@FrancescoDiMuro Thanks,

The Script isn't able to pull text from another file then use that text as the source to replace. 

so source text like this:

Before:

 

System= Example1

Distract= Example2

Type= Example3

 

Source File:

Example1= Text1

Example2= Text2

Example3= Text3

 

Existing File:

 

System= Text1

Distract= Text2

Type= Text3

 

I have basic script that does a very very basic version that just replaces 1 text at a time from 1 file. I trying to do multiple different  Texts and multiple different files.

Share this post


Link to post
Share on other sites

I can figure the Replace text part of this. I really just need Help with the pulling text from a source file then using those different texts.

 

Such as:

Source File:

Text1= System
Text2= File
Text3= Documents

File:
Text1 is Down
Text2 is located in Text3.


Aftermath:
System is Down
File is located in Documents.

 

Share this post


Link to post
Share on other sites
Posted (edited)

@dascondor
You could do something like this:

#include <Array.au3>
#include <StringConstants.au3>

Global $strSourceFileName = @ScriptDir & "\SourceFile.txt", _
       $strReplacementFileName = @ScriptDir & "\ReplacementFile.txt", _
       $strReplacementsFileContent, _
       $arrReplacements[0][2]   ; Array in which there are all the TextToFind|TextToReplace elements


; Read the file in which replacements have to be done
$strReplacementsFileContent = FileRead($strReplacementFileName)

; Display the file content before the replacements
MsgBox($MB_ICONINFORMATION, "Before the replacement", $strReplacementsFileContent)

; Create the array $arrReplacements
_ArrayAdd($arrReplacements, StringRegExpReplace(FileRead($strSourceFileName), '([^=]+)=\s(.+)', '$1|$2'))

; Do all the replacements in the file
For $i = 0 To UBound($arrReplacements) - 1 Step 1
     $strReplacementsFileContent = StringRegExpReplace($strReplacementsFileContent, $arrReplacements[$i][0], $arrReplacements[$i][1])
Next

; Display the file content after the replacements
MsgBox($MB_ICONINFORMATION, "After the replacement", $strReplacementsFileContent)

SourceFile.txt content:

Spoiler

Text1= System
Text2= File
Text3= Documents

ReplacementFile.txt content:

Spoiler

Text1 is Down
Text2 is located in Text3.

:)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

@FrancescoDiMuro just for my understanding is there any reason for the global scope of 

$strSourceFileName

Please :) ?


My video tutorials : HERE ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
2 minutes ago, caramen said:

is there any reason for the global scope of 

How would you have declared it? It's not in a function, and other than the UDF functions, there aren't any in the snippet, so Global is the only reasonable declaration of it.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

@caramen
From Variables section in the Wiki:

Quote

For example, you can declare a variable as Global within a function - although you may get a gentle reminder when you run your script within SciTE that this is not recommended.
But there is no point declaring any variables in the main body of a script as Local - they will be Global regardless.

:)


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
Posted (edited)

I should precise my question becose I know all these already, I mean why do you specify it s global there ? 

 

Edit: If you remove the Global there everything will be fine right ?

Edited by caramen

My video tutorials : HERE ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

@caramen
If you read the link attached in the previous post, there is clearely stated:

Quote

But there is no point declaring any variables in the main body of a script as Local - they will be Global regardless.

:)


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

So, you're asking why he's doing the right thing by declaring the variables, rather than which scope he's using?

Good coding practice tells you you should always declare your variables with the proper scope. Without the Global, he'd need a new line for all of the variables he's declaring, which in this case he is by using the continuation character, but didn't need to. It's true, that the variables would have been automatically declared in the global scope, but he's showing the proper method.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Posted (edited)

😱Oh after my fourth watch of the code. Haha i missed the "_" Damn it ok i got it now. It is totaly more clear.

Edited by caramen

My video tutorials : HERE ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Posted (edited)
On 3/11/2019 at 9:18 PM, dascondor said:

Example1= Text
Example2= Text2
Example3= Text3

Such a formatting makes me feel that using a .ini file - and Ini* funcs - could make the whole thing much easier 
Something like this :

"source.ini" :
[source]
Text1= System
Text2= File
Text3= Documents

"file.txt" :
Text1 is Down
Text2 is located in Text3.

$sources = IniReadSection("source.ini", "source")
$txt = FileRead("file.txt")
For $i = 1 to $sources[0][0]
   $txt = StringReplace($txt, $sources[$i][0], $sources[$i][1])
Next
Msgbox(0,"", $txt)

and using several different sections in the .ini could allow various treatments

:)

 

Edited by mikell

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

  • Similar Content

    • By Kiko745
      Hello!
      I am new to autoit so please if someone could help me. I am trying to combine multiple .txt files but I can't get it right. In every file are the exact amout of lines ( for example here are 2 ) ,            I don't want to make it for just 2 lines for each txt files but for all the lines the txt files contains. The thing I want to do is :
      Text File 1
      Hello my name
      I am a big
      ...
      Text File 2
      is Fred,
      Potato head for
      ...
      Text File 3
      and I like to eat pizza.
      Not getting this right.
      ...
      ------------------- Combine them all into one txt file like here ------------------------------
      Final Text file
      Hello my name is Fred, and I like to eat pizza.
      I am a big Potato head for Not getting this right.
      ...
       
      Thanks for any advice!
      P.S. Sorry for my English, not my first language.
       
    • By Kruxe
      Hello Everyone,
      I am very green when it comes to AutoIT, I have recently started using it to automate a job that i perform using MS Excel and that is working great. Recently I have been faced with a challenge I which i need to copy a specific set of lines from one .txt file and past them, overwriting a specific set of lines in a different .txt file. I honestly have no clue how I can copy the specified lines and transfer them. What functions / commands could I use and how could I lay them out I AutoIT? Any and all help will be appreciated. 
      Thanks!
      Kruxe
    • By AutoitMike
      Scite 3.4.4
      Win 10
      I click "Help" or press F1, there is no response
      If I use the file explorer and double click Autoit.chm or Autoit3.chm help opens.
      There is no dialog to check or uncheck "Always ask before opening this file" when clicking on these files.
       
      If you are curious as to why I dont have the latest version, I am creating a back up laptop that has a VERY extensive automation application that I have written over the past 15 years.
       
      An extremely potent, powerful, needed function has been deleted in the upgrade of Autoit in recent years that I can not do without. If my main laptop dies, which it almost did, I am in a very bad position. So I bought the exact same laptop and I am "cofiguring" it to work exactly the same as my main laptop. However, this one has been "Upgraded" to Win 10 which I hope is not the problem. 
      Thanks for any help
    • By jmp
      I am adding labour charge to total paid amount using : 
      #include <IE.au3> #include <Array.au3> $oIE = _IEAttach ("Shop") $oTable = _IETableGetCollection ($oIE, 1) $aTableData3 = _IETableWriteToArray ($oTable) Local $sitem1 = $aTableData3[5][1] Local $sitem2 = $aTableData3[5][2] Local $lcharge = "10" ;add manualy using inputbox, becuase not generating online Local $atotPric = "Payable Total Price " Local $oTds = _IETagNameGetCollection($oIE, "td") For $oTd In $oTds If $oTd.Innertext = $atotPric Then $iatotPric = $oTd.NextElementSibling.innertext MsgBox (0, "2", $iatotPric) EndIf Next $irCtotal = StringFormat("%.2f", $sitem1 + $sitem2 + $lcharge) $crTotp = StringReplace(_IEBodyReadHTML($oIE), $iatotPric, $irCtotal) _IEBodyWriteHTML ($oIE, $crTotp) But, It was also changing Total price, I want to change only Payable Total Price.

    • By nacerbaaziz
      hello sirs
      i've some questions about StringRegExpReplace i hope you can help me
       
      i tried to make a function that give me the host of the url and other give me the url with out host
      for example i've this link
      https://www.example.com/vb/result.php
      i need the first give me the
      example.com
      and the other give me 
      /vb/result.php
      i find that
      $s_source = "https://www.google.com/vb/index.php" Local $s_Host = StringRegExpReplace($s_Source, '.*://(.*?)/.*', '\1') Local $s_Page = StringRegExpReplace($s_source, '.*://.*?(/.*)', '\1') msgBox(64, $s_Host, $s_Page)  
      but i found some problems i need your help to correct it
      first: when i get the host if the url has www i want to remove it
      second: if the url with out host did not have other things 
      i need the result to be ""
      e.g
      https://www.example.com
      the first i want it
      example.com
      and the second i want it to be ""
      i hope that you can help me
      thanks in advance
×
×
  • Create New...