Eazy-P Posted February 28, 2023 Share Posted February 28, 2023 Hey Guys, this is my first time asking for help so sorry if this is basic stuff. Anyway I have 5 folders each having 5 folders in each of them (total of 25 folders all togther). In all of the folders i have a .txt file wich has the same name in all 25 folders. What i want to do is overwrite all 25 .txt files with the new .txt file i made. Is this possible? Link to comment Share on other sites More sharing options...
ioa747 Posted February 28, 2023 Share Posted February 28, 2023 take a look to FileFindFirstFile and to _FileListToArray examples in your help file SOLVE-SMART 1 I know that I know nothing Link to comment Share on other sites More sharing options...
Eazy-P Posted February 28, 2023 Author Share Posted February 28, 2023 13 minutes ago, ioa747 said: take a look to FileFindFirstFile and to _FileListToArray examples in your help file Hey, thanks for the quick response! I tried using FileFindFirstFile to see if i can atleast show me the files in the folder, I tried using the example it gave me and adjust abit. When i try running the code it only gives me back the folder name and not files in the folder. What am i doing wrong? Func MuliReplaceTest() Local $hSearch = FileFindFirstFile("C:\Users\Patrick\Desktop\KFC") If $hSearch = -1 Then MsgBox($MB_SYSTEMMODAL, "", "Error: No files/directories matched the search pattern.") Return False EndIf Local $sFileName = "", $iResult = 0 While 1 $sFileName = FileFindNextFile($hSearch) If @error Then ExitLoop $iResult = MsgBox(($MB_OKCANCEL + $MB_SYSTEMMODAL), "", "File: " & $sFileName) If $iResult <> $IDOK Then ExitLoop WEnd FileClose($hSearch) EndFunc Link to comment Share on other sites More sharing options...
ioa747 Posted February 28, 2023 Share Posted February 28, 2023 I'm not a fortune teller because I don't see the names of the folders nor do I know what file you're looking for here I don't see a file extension What is the name of the file you are looking for I know that I know nothing Link to comment Share on other sites More sharing options...
Eazy-P Posted February 28, 2023 Author Share Posted February 28, 2023 1 minute ago, ioa747 said: I'm not a fortune teller because I don't see the names of the folders nor do I know what file you're looking for here I don't see a file extension What is the name of the file you are looking for Hey sorry, my first time posting. I made a folder called KFC on my desktop with 5 txt. files called Test.txt Test2.txt etc.. I wanted to display all the filenames in the KFC folder. The paths for the files are : C:\Users\Patrick\Desktop\KFC\Test.txt C:\Users\Patrick\Desktop\KFC\Test2.txt etc.. Again sorry if I didnt tell you what you needed to know im still kinda new to this, but im trying! Link to comment Share on other sites More sharing options...
SOLVE-SMART Posted February 28, 2023 Share Posted February 28, 2023 No worries @Eazy-P, we are trying to help and you doing well in comparison to other newcomers here 😀 . I will also give you a example in few minutes. Best regards Sven Stay innovative! Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon) Link to comment Share on other sites More sharing options...
ioa747 Posted February 28, 2023 Share Posted February 28, 2023 (edited) try with _FileListToArray #include <File.au3> _FindAllFile() ;---------------------------------------------------------------------------------------- Func _FindAllFile() Local $dir = @ScriptDir & "\kfc\" ; <------------ $ArraySrtfiles = _FileListToArrayRec($dir, "test*.txt", $FLTAR_FILES, $FLTAR_RECUR ) If Not IsArray($ArraySrtfiles) Then ConsoleWrite($dir & " = Invalid input path" & @CRLF) Return Else For $x = 1 To $ArraySrtfiles[0] ConsoleWrite($dir & $ArraySrtfiles[$x] & @CRLF) Next EndIf EndFunc ;==>_FindAllFile ;---------------------------------------------------------------------------------------- this finds all the test*.txt in the KFC folder and all its subfolders Edited February 28, 2023 by ioa747 I know that I know nothing Link to comment Share on other sites More sharing options...
ioa747 Posted February 28, 2023 Share Posted February 28, 2023 this finds all the test*.txt in the KFC folder but not in all its subfolders #include <MsgBoxConstants.au3> MuliReplaceTest() Func MuliReplaceTest() Local $hSearch = FileFindFirstFile(@ScriptDir & "\KFC\test*.txt") If $hSearch = -1 Then MsgBox($MB_SYSTEMMODAL, "", "Error: No files/directories matched the search pattern.") Return False EndIf Local $sFileName = "", $iResult = 0 While 1 $sFileName = FileFindNextFile($hSearch) If @error Then ExitLoop $iResult = MsgBox(($MB_OKCANCEL + $MB_SYSTEMMODAL), "", "File: " & $sFileName) If $iResult <> $IDOK Then ExitLoop WEnd FileClose($hSearch) EndFunc I know that I know nothing Link to comment Share on other sites More sharing options...
Eazy-P Posted February 28, 2023 Author Share Posted February 28, 2023 37 minutes ago, ioa747 said: try with _FileListToArray #include <File.au3> _FindAllFile() ;---------------------------------------------------------------------------------------- Func _FindAllFile() Local $dir = @ScriptDir & "\kfc\" ; <------------ $ArraySrtfiles = _FileListToArrayRec($dir, "test*.txt", $FLTAR_FILES, $FLTAR_RECUR ) If Not IsArray($ArraySrtfiles) Then ConsoleWrite($dir & " = Invalid input path" & @CRLF) Return Else For $x = 1 To $ArraySrtfiles[0] ConsoleWrite($dir & $ArraySrtfiles[$x] & @CRLF) Next EndIf EndFunc ;==>_FindAllFile ;---------------------------------------------------------------------------------------- this finds all the test*.txt in the KFC folder and all its subfolders Thanks this helped alot! I dont understand some of the things in the script but it works. Ill try to adjust it that it will copy the new file i made and replace all the old files with the same name. Link to comment Share on other sites More sharing options...
SOLVE-SMART Posted February 28, 2023 Share Posted February 28, 2023 (edited) Hi @Eazy-P, (1) I created the folder and file structure, for testing by this script: expandcollapse popup#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #AutoIt3Wrapper_AU3Check_Stop_OnWarning=y #AutoIt3Wrapper_Run_Au3Stripper=y #AutoIt3Wrapper_UseUpx=n #Au3Stripper_Parameters=/sf /sv /mo /rm /rsln _CreateStructure() MsgBox('', 'DEMO', 'Folder and file structure created.') Func _CreateStructure() Local Const $sRootFolder = _AddTrailingBackslash(@DesktopDir & '\DEMO') Local Const $sOldTxtFile = 'myTextFile.txt' Local Const $sNewTxtFile = 'newTextFile.txt' Local Const $sNewTxtFilePath = _AddTrailingBackslash(@DesktopDir) & $sNewTxtFile Local Const $aFolderList[] = _ [ _ 'Adam\Blake', _ 'Adam\Cedrick', _ 'Adam\Chaz', _ 'Adam\Christopher', _ 'Adam\Cierra', _ 'Clyde\Cristina', _ 'Clyde\Cynthia', _ 'Clyde\Dakota', _ 'Clyde\Damian', _ 'Clyde\Darwin', _ 'Emely\Florence', _ 'Emely\Frank', _ 'Emely\Grayce', _ 'Emely\Gregory', _ 'Emely\Hazle', _ 'Jacky\Jade', _ 'Jacky\Jewell', _ 'Jacky\Joannie', _ 'Jacky\Johathan', _ 'Jacky\Lavina', _ 'Mossie\Myrtle', _ 'Mossie\Octavia', _ 'Mossie\Oleta', _ 'Mossie\Rae', _ 'Mossie\Rafael' _ ] Local Const $sFileContent = 'This is a demo.' ; Can also be '' if you don't need any content. For $sFolder In $aFolderList _WriteFile($sRootFolder & _AddTrailingBackslash($sFolder) & $sOldTxtFile, $sFileContent) Next _WriteFile($sNewTxtFilePath, $sFileContent) EndFunc Func _WriteFile($sFile, $sText) Local Const $iUtf8WithoutBomAndOverwriteCreationMode = 256 + 2 + 8 Local $hFile = FileOpen($sFile, $iUtf8WithoutBomAndOverwriteCreationMode) FileWrite($hFile, $sText) FileClose($hFile) EndFunc Func _AddTrailingBackslash($sPath) Return (StringRight($sPath, 1) == '\') ? $sPath : $sPath & '\' EndFunc You @Eazy-P do have your own folder structure (so only the second script should be interesting for you), but this can help others to recreate your scenario easily 😀 . (2) As far as I understood you correct, one way to replace all your old *.txt files within the folders by a copy of one other file (the new one) can be this: expandcollapse popup#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #AutoIt3Wrapper_AU3Check_Stop_OnWarning=y #AutoIt3Wrapper_Run_Au3Stripper=y #AutoIt3Wrapper_UseUpx=n #Au3Stripper_Parameters=/sf /sv /mo /rm /rsln #include-once #include <File.au3> _ReplaceFiles() MsgBox('', 'DEMO', 'New files are in the folders.') Func _ReplaceFiles() Local Const $sRootFolder = _AddTrailingBackslash(@DesktopDir & '\DEMO') Local Const $sNewTxtFile = 'newTextFile.txt' Local Const $sNewTxtFilePath = _AddTrailingBackslash(@DesktopDir) & $sNewTxtFile Local $aFileList = _GetFiles($sRootFolder) For $sFile In $aFileList FileDelete($sFile) FileCopy($sNewTxtFilePath, _AddTrailingBackslash(_GetPath($sFile)) & $sNewTxtFile) Next EndFunc Func _GetFiles($sRootFolder) Local Const $iFilesOnlyFlag = 1 Local Const $iRecursiveFlag = 1 Local Const $iNoSortFlag = 0 Local Const $iFullPathFlag = 2 Return _FileListToArrayRec($sRootFolder, '*.txt', $iFilesOnlyFlag, $iRecursiveFlag, $iNoSortFlag, $iFullPathFlag) EndFunc Func _AddTrailingBackslash($sPath) Return (StringRight($sPath, 1) == '\') ? $sPath : $sPath & '\' EndFunc Func _GetPath($sFile) Return StringRegExpReplace($sFile, '(^.*\\)(.*)', '\1') EndFunc You have to adjust the folder paths and $sNewTxtFile name, then you should be done 🤞 . Best regards Sven Edited February 28, 2023 by SOLVE-SMART Stay innovative! Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon) Link to comment Share on other sites More sharing options...
ioa747 Posted February 28, 2023 Share Posted February 28, 2023 21 minutes ago, Eazy-P said: I dont understand some of the things in the script but it works. What is it that you do not understand everything is in the help _FileListToArrayRec if you do not understand something ask Here is the replace part #include <File.au3> _FindAllFile() ;---------------------------------------------------------------------------------------- Func _FindAllFile() Local $dir = @ScriptDir & "\kfc\" ; <------------ $ArraySrtfiles = _FileListToArrayRec($dir, "test*.txt", $FLTAR_FILES, $FLTAR_RECUR) If Not IsArray($ArraySrtfiles) Then ConsoleWrite($dir & " = Invalid input path" & @CRLF) Return Else For $x = 1 To $ArraySrtfiles[0] ;ConsoleWrite($dir & $ArraySrtfiles[$x]& @CRLF) _ReplaceTxt($dir & $ArraySrtfiles[$x]) Next EndIf EndFunc ;==>_FindAllFile ;---------------------------------------------------------------------------------------- Func _ReplaceTxt($TxtFile) Local $NewTxtFile = @ScriptDir & "\Test.txt" If FileCopy($NewTxtFile, $TxtFile, $FC_OVERWRITE + $FC_CREATEPATH) Then ConsoleWrite("ReplaceTxt --> " & $TxtFile & @CRLF) EndIf EndFunc ;==>_ReplaceTxt I know that I know nothing Link to comment Share on other sites More sharing options...
SOLVE-SMART Posted February 28, 2023 Share Posted February 28, 2023 Unfortunately this does not work this way @ioa747 😔 . Either I did my tests wrong which could be possible or you cannot use FileCopy() for replacement like that. Please double check in case I am wrong. Best regards Sven Stay innovative! Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon) Link to comment Share on other sites More sharing options...
ioa747 Posted February 28, 2023 Share Posted February 28, 2023 @SOLVE-SMART make a txt file in @ScriptDir name Test.txt (Local $NewTxtFile = @ScriptDir & "\Test.txt") put some text in the file e.g. "hello world", make your DEMO structure , run the script, and check #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <File.au3> _FindAllFile() ;---------------------------------------------------------------------------------------- Func _FindAllFile() Local $dir = @DesktopDir & "\DEMO\" ; <------------ Local $ArraySrtfiles = _FileListToArrayRec($dir, "*.txt", $FLTAR_FILES, $FLTAR_RECUR) If Not IsArray($ArraySrtfiles) Then ConsoleWrite($dir & " = Invalid input path" & @CRLF) Return Else For $x = 1 To $ArraySrtfiles[0] ;ConsoleWrite($dir & $ArraySrtfiles[$x]& @CRLF) _ReplaceTxt($dir & $ArraySrtfiles[$x]) Next EndIf EndFunc ;==>_FindAllFile ;---------------------------------------------------------------------------------------- Func _ReplaceTxt($TxtFile) Local $NewTxtFile = @ScriptDir & "\Test.txt" If FileCopy($NewTxtFile, $TxtFile, $FC_OVERWRITE + $FC_CREATEPATH) Then ConsoleWrite("ReplaceTxt --> " & $TxtFile & @CRLF) EndIf EndFunc ;==>_ReplaceTxt I know that I know nothing Link to comment Share on other sites More sharing options...
SOLVE-SMART Posted February 28, 2023 Share Posted February 28, 2023 (edited) That's exactly what I did. Please try this on your own: FileCopy('c:\Users\sven.seyfert\Desktop\au3\Test.txt','C:\Users\sven.seyfert\Desktop\DEMO\Mossie\Rafael\myTextFile.txt', 1 + 8) Of course adjust the paths and please choose different path locations. If this works for you, then I am wrong probably with my assesment. Best regards Sven Update: I also will restart my computer, just in case 😅 . It's windows 😂 . Edited February 28, 2023 by SOLVE-SMART Stay innovative! Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon) Link to comment Share on other sites More sharing options...
ioa747 Posted February 28, 2023 Share Posted February 28, 2023 have you check inside the text file? I know that I know nothing Link to comment Share on other sites More sharing options...
SOLVE-SMART Posted February 28, 2023 Share Posted February 28, 2023 15 minutes ago, ioa747 said: have you check inside the text file? What do you mean? I restarted the system (Windows 10) to ensure there is no file lock, if you mean this? I use 3.3.16.0 on this specific computer. I will double check on another computer with AutoIt version 3.3.16.1 (newest one). Did you test the single line copy on your own? Does it work @ioa747? Best regards Sven Stay innovative! Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon) Link to comment Share on other sites More sharing options...
ioa747 Posted February 28, 2023 Share Posted February 28, 2023 (edited) For me, the text inside the text file has changed in all the files. the name is the same, of course FileCopy ( "source", "dest" [, flag = 0] ) Edited February 28, 2023 by ioa747 SOLVE-SMART 1 I know that I know nothing Link to comment Share on other sites More sharing options...
SOLVE-SMART Posted February 28, 2023 Share Posted February 28, 2023 (edited) 17 minutes ago, ioa747 said: For me, the text inside the text has changed in all the files with the same name, of course Aaaaah, now I understand what you mean, okay 😀 , thanks 👍 . The copy action works. But only the file content changes, not the file name as I would expect it. I thought, the author of the question (@Eazy-P) wanted to replace the files by a new file and also replace the file names. Like I did it in my example from "myTextFile.txt" to "newTextFile.txt". In case this isn't the requirement, your approach fits pretty well @ioa747. In the other case I would suggest my example (demo) approach. Anyway, thanks for the clarification @ioa747 👍 . Best regards Sven Edited February 28, 2023 by SOLVE-SMART Stay innovative! Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon) Link to comment Share on other sites More sharing options...
Eazy-P Posted March 1, 2023 Author Share Posted March 1, 2023 21 hours ago, SOLVE-SMART said: Aaaaah, now I understand what you mean, okay 😀 , thanks 👍 . The copy action works. But only the file content changes, not the file name as I would expect it. I thought, the author of the question (@Eazy-P) wanted to replace the files by a new file and also replace the file names. Like I did it in my example from "myTextFile.txt" to "newTextFile.txt". In case this isn't the requirement, your approach fits pretty well @ioa747. In the other case I would suggest my example (demo) approach. Anyway, thanks for the clarification @ioa747 👍 . Best regards Sven Im sorry it took a while to respond. what i meant was that the new file i created replaces/overwrites the files with the same name(as the new file) in all the folders. Link to comment Share on other sites More sharing options...
ioa747 Posted March 1, 2023 Share Posted March 1, 2023 (edited) this find all test*.txt (test1.txt , test2.txt etc) in folder @ScriptDir & "\kfc\ and its subfolder and replaced with @ScriptDir & "\Test.txt" but hold the initial name (test1.txt , test2.txt etc) because FileCopy ( "source", "dest" ) means source copy as dest e.g. (test1.txt , test2.txt etc) I hope I managed to explain it correctly #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <File.au3> _FindAllFile() ;---------------------------------------------------------------------------------------- Func _FindAllFile() Local $dir = @ScriptDir & "\kfc\" ; <------------ Local $ArraySrtfiles = _FileListToArrayRec($dir, "test*.txt", $FLTAR_FILES, $FLTAR_RECUR) If Not IsArray($ArraySrtfiles) Then ConsoleWrite($dir & " = Invalid input path" & @CRLF) Return Else For $x = 1 To $ArraySrtfiles[0] ;ConsoleWrite($dir & $ArraySrtfiles[$x]& @CRLF) _ReplaceTxt($dir & $ArraySrtfiles[$x]) Next EndIf EndFunc ;==>_FindAllFile ;---------------------------------------------------------------------------------------- Func _ReplaceTxt($TxtFile) Local $NewTxtFile = @ScriptDir & "\Test.txt" If FileCopy($NewTxtFile, $TxtFile, $FC_OVERWRITE + $FC_CREATEPATH) Then ConsoleWrite("ReplaceTxt --> " & $TxtFile & @CRLF) EndIf EndFunc ;==>_ReplaceTxt Edited March 1, 2023 by ioa747 I know that I know nothing 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