david1337 Posted October 12, 2015 Share Posted October 12, 2015 (edited) Hi guysWith 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 October 12, 2015 by david1337 Link to comment Share on other sites More sharing options...
water Posted October 12, 2015 Share Posted October 12, 2015 All *.txt files in the same directory? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
david1337 Posted October 12, 2015 Author Share Posted October 12, 2015 hi waterYeah, how can I change oldtext to newtext for all txt files in the same directory? Link to comment Share on other sites More sharing options...
water Posted October 12, 2015 Share Posted October 12, 2015 _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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
david1337 Posted October 12, 2015 Author Share Posted October 12, 2015 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 Link to comment Share on other sites More sharing options...
water Posted October 12, 2015 Share Posted October 12, 2015 (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 October 12, 2015 by water david1337 1 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
david1337 Posted October 12, 2015 Author Share Posted October 12, 2015 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 Link to comment Share on other sites More sharing options...
water Posted October 12, 2015 Share Posted October 12, 2015 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now