Jump to content

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)

 

 

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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.

Link to comment
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.

 

Link to comment
Share on other sites

@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

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

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

$strSourceFileName

Please :) ?

My video tutorials : ( 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

 

Link to comment
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

Link to comment
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

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

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 : ( 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

 

Link to comment
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

Link to comment
Share on other sites

😱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 : ( 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

 

Link to comment
Share on other sites

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

×
×
  • Create New...