speedtrooper Posted October 23, 2007 Share Posted October 23, 2007 How's it going? I'm a newby when it comes to AutoIt and I thought that I could get some help from the experts here at AutoIt forum. Now my issue is that I can't get the process below to go through both of those text files. Here's what I want it to do: ;Check if files exist ;Check if "123456789" is in those files ;if it is then notify the user with msg box and close the process ;if it's not then notify the user with msg box and then change the "123456789" to "56123" and close the process ;If files don't exist, notify the user and close the processoÝ÷ Ø ÝêÞßÛ-ç(uë(}ªÚºÚ"µÍÌÍÕ^[S[YLHH ][ÝÌK ][ÝÂÌÍÕ^[S[YLH ][ÝÌ ][ÝÂÌÍÑ[^H ][ÝÌLÍ MÎI][ÝÂÌÍÔXÙU^H ][ÝÍMLÉ][ÝÂY[Q^ÝÊ ÌÍÕ^[S[YLJH[ÙÐÞ M ][ÝÑ[I][ÝË ][ÝÑ[H^ÝË][ÝÊBIÌÍÑ[PÛÛ[ÈH[TXY ÌÍÕ^[S[YLJBRYÝ[Ò[Ý ÌÍÑ[PÛÛ[Ë ÌÍÑ[^ U[BSÙÐÞ M ][ÝÑ[I][ÝË ][ÝÒ]YYÈÈHÚ[ÙYÛXÚÈÒÈÈÛÛ[YK][ÝÊBBBBIÌÍÑ[PÛÛ[ÈHÝ[ÔXÙJ ÌÍÑ[PÛÛ[Ë ÌÍÑ[^ ÌÍÔXÙU^ BBQ[Q[]J ÌÍÕ^[S[YLJBBQ[UÜ]J ÌÍÕ^[S[YLK ÌÍÑ[PÛÛ[ÊBQ[ÙBBSÙÐÞ M ][ÝÑ[I][ÝË ][ÝÑÙÛÌÎNÝYYÈHÚ[ÙY ][ÝÊBBQ^]Q[Y[ÙBÙÐÞ M ][ÝÑ[I][ÝË ][ÝÑÙÈÕ^Ý][ÝÊBQ^][Y Any suggestions? Note: These files are in different directories. Also, I've only used two files for an example since I'm going to be working with probably three or four. Thanks in advance. Link to comment Share on other sites More sharing options...
PsaltyDS Posted October 23, 2007 Share Posted October 23, 2007 How's it going? I'm a newby when it comes to AutoIt and I thought that I could get some help from the experts here at AutoIt forum. Now my issue is that I can't get the process below to go through both of those text files. Here's what I want it to do: Any suggestions? Note: These files are in different directories. Also, I've only used two files for an example since I'm going to be working with probably three or four. Thanks in advance. Try opening the file for overwrite: If StringInStr($FileContents, $FindText)Then MsgBox(4096,"File", "It needs to be changed. Click OK to continue.") $FileContents = StringReplace($FileContents,$FindText,$ReplaceText) $hFile = FileOpen($TextFileName1, 2) If $hFile <> -1 Then FileWrite($hFile,$FileContents) FileClose($hFile) Else MsgBox(16, "Error", "Failed to open file for write: " & $TestFilename1) EndIf Else MsgBox(4096,"File", "Doesn't need to be changed") Exit EndIf Else MsgBox(4096,"File", "Does NOT exist.") Exit EndIf Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
speedtrooper Posted October 23, 2007 Author Share Posted October 23, 2007 PsaltyDS, thanks for the quick reply. I should have explained better. The process works fine and it does what I want except I don't know how to apply it for all the files. I know only how to do it for one file. I was wondering if I can include $TextFileName2 to go through the same process, but without writing the same code over again. Thanks. Link to comment Share on other sites More sharing options...
Kurtferro Posted October 23, 2007 Share Posted October 23, 2007 (edited) You can create Array with file name list $Array = _ArrayCreate("file1.txt", "file2.txt", "file3.txt") for $i = 0 to UBound($Array) $TextFileName = $Array[$i] $FindText = "123456789" $ReplaceText = "56123" If FileExists($TextFileName) Then MsgBox(4096, $Array[$i] , "File exists.") $FileContents = FileRead($TextFileName) If StringInStr($FileContents, $FindText)Then MsgBox(4096, $Array[$i] , "It needs to be changed. Click OK to continue.") $FileContents = StringReplace($FileContents,$FindText,$ReplaceText) FileDelete($TextFileName) FileWrite($TextFileName,$FileContents) Else MsgBox(4096, $Array[$i] , "Doesn't need to be changed") Exit EndIf Else MsgBox(4096,"File", "Does NOT exist.") Exit EndIf next try^^ i havent compiled but the concept is easy. Edited October 23, 2007 by Kurtferro Link to comment Share on other sites More sharing options...
speedtrooper Posted October 23, 2007 Author Share Posted October 23, 2007 Kurtferro, thanks a lot. That's a much better script than mine, haha. Everything works fine except one tiny glitch: Array variable has incorrect number of subscripts or subscript dimension range exceeded.: $TextFileName = $Array[$i] $TextFileName = ^ ERROR And here's the script now: #include <Array.au3> $Array = _ArrayCreate("1.txt", "2.txt", "3.txt") for $i = 0 to UBound($Array) $TextFileName = $Array[$i] $FindText = "123456789" $ReplaceText = "56123" If FileExists($TextFileName) Then MsgBox(4096, $Array[$i] , "File exists.") $FileContents = FileRead($TextFileName) If StringInStr($FileContents, $FindText)Then MsgBox(4096, $Array[$i] , "It needs to be changed. Click OK to continue.") $FileContents = StringReplace($FileContents,$FindText,$ReplaceText) FileDelete($TextFileName) FileWrite($TextFileName,$FileContents) Else MsgBox(4096, $Array[$i] , "Doesn't need to be changed") EndIf Else MsgBox(4096,"File", "Does NOT exist.") EndIf Next Link to comment Share on other sites More sharing options...
DW1 Posted October 23, 2007 Share Posted October 23, 2007 Replacefor $i = 0 to UBound($Array)oÝ÷ Ùh«¢+Ù½ÈÀÌØí¤ôÀѼU ½Õ¹ ÀÌØíÉÉ䤴 AutoIt3 Online Help Link to comment Share on other sites More sharing options...
speedtrooper Posted October 23, 2007 Author Share Posted October 23, 2007 (edited) Awesome, it worked! Thanks for all of your help. BTW, I'm interesting in knowing on what difference did that -1 make (the meaning of it I guess)? Thanks. Edited October 23, 2007 by speedtrooper Link to comment Share on other sites More sharing options...
Kurtferro Posted October 23, 2007 Share Posted October 23, 2007 yes now work at 100%^^ #include <Array.au3> $Array = _ArrayCreate("1.txt", "2.txt", "3.txt") for $i = 0 to UBound($Array) -1 $TextFileName = $Array[$i] $FindText = "123456789" $ReplaceText = "56123" If FileExists($TextFileName) Then MsgBox(4096, $TextFileName , "File exists.") $FileContents = FileRead($TextFileName) If StringInStr($FileContents, $FindText)Then MsgBox(4096, $TextFileName , "It needs to be changed. Click OK to continue.") $FileContents = StringReplace($FileContents,$FindText,$ReplaceText) FileDelete($TextFileName) FileWrite($TextFileName,$FileContents) Else MsgBox(4096, $TextFileName , "Doesn't need to be changed") EndIf Else MsgBox(4096, $TextFileName, "Does NOT exist.") EndIf Next Link to comment Share on other sites More sharing options...
speedtrooper Posted October 23, 2007 Author Share Posted October 23, 2007 yes now work at 100%^^ #include <Array.au3> $Array = _ArrayCreate("1.txt", "2.txt", "3.txt") for $i = 0 to UBound($Array) -1 $TextFileName = $Array[$i] $FindText = "123456789" $ReplaceText = "56123" If FileExists($TextFileName) Then MsgBox(4096, $TextFileName , "File exists.") $FileContents = FileRead($TextFileName) If StringInStr($FileContents, $FindText)Then MsgBox(4096, $TextFileName , "It needs to be changed. Click OK to continue.") $FileContents = StringReplace($FileContents,$FindText,$ReplaceText) FileDelete($TextFileName) FileWrite($TextFileName,$FileContents) Else MsgBox(4096, $TextFileName , "Doesn't need to be changed") EndIf Else MsgBox(4096, $TextFileName, "Does NOT exist.") EndIf Next Thanks. Link to comment Share on other sites More sharing options...
Kurtferro Posted October 23, 2007 Share Posted October 23, 2007 (edited) $Array = _ArrayCreate("1.txt", "2.txt", "3.txt") 1.txt is 0, 2.txt is 1, 3txt is 2, in the array. for uBound 1.txt is 1 2.txt is 2 3.txt is 3 so must to use -1 for have 2 and not 3, "for" start at 0 and run to 2 = 0 1 2 in the array. without -1 for run to 3 = error Edited October 23, 2007 by Kurtferro Link to comment Share on other sites More sharing options...
speedtrooper Posted October 23, 2007 Author Share Posted October 23, 2007 $Array = _ArrayCreate("1.txt", "2.txt", "3.txt")1.txt is 0, 2.txt is 1, 3txt is 2, in the array.for ubount 1.txt is 1 etc so must to use -1 for have 2 = 0 1 2 in the array.Oh, I get it now. It's pretty much what the help file says about it:...the value returned by UBound is one greater than the index of an array's last element!Thanks for clarifying. Link to comment Share on other sites More sharing options...
Kurtferro Posted October 23, 2007 Share Posted October 23, 2007 u can also create a gui with drag and drop file etc, in array u will use a variable with GUICtrlRead(..... 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