Jump to content
david1337

String replace in all files

Recommended Posts

david1337

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
water

All *.txt files in the same directory?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.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
david1337

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
water

_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 2018-12-03 - Version 1.4.11.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
david1337

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
water

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
  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.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
david1337

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
water

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.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

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

×