glasglow Posted May 5, 2008 Posted May 5, 2008 Hi this isn't my code but it works to find and replace things in a single file. I would like to find and replace in multiple files and multiple directories. Any suggestions? #include <File.au3> ;Define starting folder $root = "D:\" ;Create array of txt files $theArray = _FileListToArray ($root, "*.txt",1) ;String to find $find = "that" ;String to replace $replace = "this" $resultString = "Replacing all occurences of " & $find & " with " & $replace & @CRLF ;Loop through array elements For $X = 1 to $theArray[0] ;Replace all occurences $count = _ReplaceStringInFile($root & "\" & $theArray[$X],$find,$replace) If NOT @ERROR Then $resultString &= $count & " occurences replaced in file: " & $root & "\" & $theArray[$X] EndIf Next MsgBox(0,"",$resultString) I have tried \*.* and StringRegExpReplace($startDir, "(\\.[^\\]*)\z" But I am quite unable to get it going.
d4rk Posted May 5, 2008 Posted May 5, 2008 is there any error message from console ? [quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys
glasglow Posted May 5, 2008 Author Posted May 5, 2008 is there any error message from console ?No errors if there is a found file... But when I try to move it into that directory and all directories under it.. I get errors.. because I'm coding it wrong.
FreeRider Posted May 5, 2008 Posted May 5, 2008 Hi Glasglow,Instead of _FileListToArray() try using _FileSearch() where recursive mode is possible... This would avoid errors when files are in sub dirs.Here is the link to the function : http://www.autoitscript.com/forum/index.php?showtopic=42049READ GEOSoft's instructions, it's clear and simple (as always whit him)Of course as you will for sure retreive the fullpath to file you will have to modify the script in your loop ($root & "\" & is now not necessary)...Hope this will help you,Bye FreeRiderHonour & Fidelity
glasglow Posted May 5, 2008 Author Posted May 5, 2008 Here is what I have: expandcollapse popup#include <File.au3> #include <array.au3> ;Define starting folder $theArray = _FileSearch("D:\") ;String to find $find = "that" ;String to replace $replace = "this" $resultString = "Replacing all occurences of " & $find & " with " & $replace & @CRLF ;Loop through array elements For $X = 1 to $theArray[0] $count = _ReplaceStringInFile($theArray[$X],$find,$replace) Next MsgBox(0,"",$resultString) Func _FileSearch($szRoot, $Szmask = "*.html", $nFlag = 3, $nOcc = 0) Local $hArray = $Szmask, $iRec, $iPath, $iSort Switch $nFlag Case 1 $iRec = 1 $iPath = 0 $iSort = 0 Case 2 $iRec = 0 $iPath = 1 $iSort = 0 Case 3 $iRec = 1 $iPath = 1 $iSort = 0 Case 4 $iRec = 0 $iPath = 0 $iSort = 1 Case 5 $iRec = 1 $iPath = 0 $iSort = 1 Case 6 $iRec = 0 $iPath = 1 $iSort = 1 Case Else $iRec = 1 $iPath = 1 $iSort = 1 EndSwitch If NOT IsArray($hArray) Then $hArray = StringSplit($hArray, '|') Local $Hfile = 0, $F_List = '' $szBuffer = "" $szReturn = "" $szPathlist = "*" For $I = 1 To Ubound($hArray)-1 $szMask = $hArray[$I] If NOT StringInStr ($Szmask, "\") Then $szRoot &= '' Else $iTrim = StringInStr ($Szmask, "\",0,-1) $szRoot &= StringLeft ($Szmask, $iTrim) $Szmask = StringTrimLeft ($Szmask, $iTrim) EndIf If $iRec = 0 Then $Hfile = FileFindFirstFile ($szRoot & $szMask) If $Hfile >= 0 Then $szBuffer = FileFindNextFile ($Hfile) While NOT @Error If $iPath = 1 Then $szReturn &= $szRoot If $szBuffer <> "." AND $szBuffer <> ".." Then $szReturn &= $szBuffer & "*" $szBuffer = FileFindNextFile ($Hfile) Wend FileClose ($Hfile) EndIf Else While 1 $Hfile = FileFindFirstFile ($szRoot & "*.*") If $Hfile >= 0 Then $szBuffer = FileFindNextFile ($Hfile) While NOT @Error If $szBuffer <> "." AND $szBuffer <> ".." AND StringInStr (FileGetAttrib ($szRoot & $szBuffer), "D") Then _ $szPathlist &= $szRoot & $szBuffer & "*" $szBuffer = FileFindNextFile ($Hfile) Wend FileClose ($Hfile) EndIf If StringInStr ($szReturn, $Szmask) > 0 AND $nOcc = 1 Then $szRoot = '' ExitLoop EndIf $Hfile = FileFindFirstFile ($szRoot & $szMask) If $Hfile >= 0 Then $szBuffer = FileFindNextFile ($Hfile) While NOT @Error If $iPath = 1 Then $szReturn &= $szRoot If $szBuffer <> "." AND $szBuffer <> ".." Then $szReturn &= $szBuffer & "*" $szBuffer = FileFindNextFile ($Hfile) Wend FileClose ($Hfile) EndIf If $szPathlist == "*" Then ExitLoop $szPathlist = StringTrimLeft ($szPathlist, 1) $szRoot = StringLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1) & "\" $szPathlist = StringTrimLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1) Wend EndIf Next If $szReturn = "" Then Return 0 Else $szReturn = StringReplace($szReturn,'\', '') $F_List = StringSplit (StringTrimRight ($szReturn, 1), "*") If $iSort = 1 Then _ArraySort($F_List) Return $F_List EndIf EndFunc ;<===> _FileSearch() But I'm missing something. It is getting a null when there are files.
FreeRider Posted May 5, 2008 Posted May 5, 2008 Try this modified function... some of the line at bottom have been unactivated. I personaly use it and it work perfectly (ready to use with you own default parameters) expandcollapse popupFunc _FileSearch($szRoot, $Szmask = "*.html", $nFlag = 3, $nOcc = 0) Local $hArray = $Szmask, $iRec, $iPath, $iSort Switch $nFlag Case 1 $iRec = 1 $iPath = 0 $iSort = 0 Case 2 $iRec = 0 $iPath = 1 $iSort = 0 Case 3 $iRec = 1 $iPath = 1 $iSort = 0 Case 4 $iRec = 0 $iPath = 0 $iSort = 1 Case 5 $iRec = 1 $iPath = 0 $iSort = 1 Case 6 $iRec = 0 $iPath = 1 $iSort = 1 Case Else $iRec = 1 $iPath = 1 $iSort = 1 EndSwitch If NOT IsArray($hArray) Then $hArray = StringSplit($hArray, '|') Local $Hfile = 0, $F_List = '' $szBuffer = "" $szReturn = "" $szPathlist = "*" For $I = 1 To Ubound($hArray)-1 $szMask = $hArray[$I] If NOT StringInStr ($Szmask, "\") Then $szRoot &= '' Else $iTrim = StringInStr ($Szmask, "\",0,-1) $szRoot &= StringLeft ($Szmask, $iTrim) $Szmask = StringTrimLeft ($Szmask, $iTrim) EndIf If $iRec = 0 Then $Hfile = FileFindFirstFile ($szRoot & $szMask) If $Hfile >= 0 Then $szBuffer = FileFindNextFile ($Hfile) While NOT @Error If $iPath = 1 Then $szReturn &= $szRoot If $szBuffer <> "." AND $szBuffer <> ".." Then $szReturn &= $szBuffer & "*" $szBuffer = FileFindNextFile ($Hfile) Wend FileClose ($Hfile) EndIf Else While 1 $Hfile = FileFindFirstFile ($szRoot & "*.*") If $Hfile >= 0 Then $szBuffer = FileFindNextFile ($Hfile) While NOT @Error If $szBuffer <> "." AND $szBuffer <> ".." AND StringInStr (FileGetAttrib ($szRoot & $szBuffer), "D") Then _ $szPathlist &= $szRoot & $szBuffer & "*" $szBuffer = FileFindNextFile ($Hfile) Wend FileClose ($Hfile) EndIf If StringInStr ($szReturn, $Szmask) > 0 AND $nOcc = 1 Then $szRoot = '' ExitLoop EndIf $Hfile = FileFindFirstFile ($szRoot & $szMask) If $Hfile >= 0 Then $szBuffer = FileFindNextFile ($Hfile) While NOT @Error If $iPath = 1 Then $szReturn &= $szRoot If $szBuffer <> "." AND $szBuffer <> ".." Then $szReturn &= $szBuffer & "*" $szBuffer = FileFindNextFile ($Hfile) Wend FileClose ($Hfile) EndIf If $szPathlist == "*" Then ExitLoop $szPathlist = StringTrimLeft ($szPathlist, 1) $szRoot = StringLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1) & "\" $szPathlist = StringTrimLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1) Wend EndIf Next If $szReturn = "" Then Return 0 Else ;$szReturn = StringReplace($szReturn,'\', '') $F_List = StringSplit (StringTrimRight ($szReturn, 1), "*") ;If $iSort = 1 Then _ArraySort($F_List) Return $F_List EndIf EndFunc;<===> _FileSearch() FreeRiderHonour & Fidelity
glasglow Posted May 5, 2008 Author Posted May 5, 2008 Nice.. you trimmed off the path? trimright looks more like you trimmed off after the extension.. Anyway thank you very much for that .. certainly gives me something study.
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