david1337

String replace in all files

8 posts in this topic

#1 ·  Posted (edited)

Hi guys

With this script I can replace the word "oldtext" with "newtext" for the file test.txt.

$szFile = "test.txt"

$szText = FileRead($szFile,FileGetSize($szFile))


$szText = StringReplace($szText, "oldtext", "newtext")

FileDelete($szFile)
FileWrite($szFile,$szText)

 

But how can I do this for ALL *.txt files in the folder?
I tried with *.txt instead of test.txt, but this just deletes all the txt files.

Edited by david1337

Share this post


Link to post
Share on other sites



All *.txt files in the same directory?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-02-03 - Version 1.4.7.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

hi water

Yeah, how can I change oldtext to newtext for all txt files in the same directory?

Share this post


Link to post
Share on other sites

_FileListToArray returns an array with all *.txt files. Put your current code into a function and call it for each element of the array.

BTW:
To enhance performance use

$szText = FileRead($szFile)

because as default the function reads the whole file.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-02-03 - Version 1.4.7.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

How do I call it for each element in my array? :>

 

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



; List all the files and folders in the desktop directory using the default parameters.
Local $szFile = _FileListToArray(@ScriptDir, "*.txt")


Call("Replace")


Func Replace()
$szText = FileRead($szFile)


$szText = StringReplace($szText, "old text", "new text")

FileDelete($szFile)
FileWrite($szFile,$szText)
EndFunc

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Something like this.
But you definitely need to learn how Autoit works. Especially arrays. There are very good tutorials in the wiki :)

#include <File.au3>
; List all the files in the directory
Local $aFileList = _FileListToArray(@ScriptDir, "*.txt")
For $i = 1 To $aFileList[0]
    Replace($aFileList[$i])
Next

Func Replace($sFile)
    $sText = FileRead($sFile)
    $sText = StringReplace($sText, "old text", "new text")
    FileDelete($sFile)
    FileWrite($sFile, $sText)
EndFunc   ;==>Replace

 

Edited by water
1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-02-03 - Version 1.4.7.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks water!

That worked perfectly.
Yes you are absolutely right. I really am a newb when it comes to arrays :)

I'll start with understanding the one you just wrote for me here.

Once again, thanks for the help. It's much appreciated.

David
 

Share this post


Link to post
Share on other sites

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-02-03 - Version 1.4.7.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

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