Jump to content

Checking and writing to multiple files


Recommended Posts

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^][ÝÍ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[]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

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

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

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 by Kurtferro
Link to comment
Share on other sites

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

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

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

$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 by Kurtferro
Link to comment
Share on other sites

$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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...