Jump to content

help processing multiple .txt files one at a time.


Recommended Posts

Ive written a simple script that reads a .txt file and inputs the hands into a 3rd party programme for me. it has been a learning experience for me as prior to this project I had no coding experience. the script works to process 1 file no problem, but I've been stuck for hours trying to figure out how to get it to open a new file once the loop is finished. I've been playig around with FileFindFirstFile, FileFindNextFile, _FileListtoArray but I just can't figure it out.

My working script does the following......this is cut down of course.

Dim  $file = FileOpen("1.txt",0)
           
         ;mainloop
          While $file = 1   
      $line = FileReadLine($file) 
          If @error = -1 Then ExitLoop
          code
          Wend

          ;Functions

How can i get it to open a new file once $line reaches @error -1 and repeat the main loop till all files in a specific directory have been processed?

Edited by kaibraine
Link to comment
Share on other sites

This should do the trick for you...

Global $g_Dir = @MyDocumentsDir

Func _ParseFilesDir($folName, $fileType = "*.*")
    FileChangeDir($g_Dir & "\" & $folName & "\")
    $search = FileFindFirstFile($fileType)

    If $search = -1 Then
        _ErrorMsg("No files/directories matched the search pattern")
        Return
    EndIf

    While 1
        $found = FileFindNextFile($search)
        If @error Then ExitLoop
        $fileO = FileOpen($found, 0)
        While 1
            $line = FileReadLine($fileO)
            If @error = -1 Then ExitLoop
            ;>>>>>>>>>>>>>>>>>
            ;YOUR CODE HERE!!!
            ;<<<<<<<<<<<<<<<<<
        WEnd
        FileClose($fileO)
    WEnd
    
    FileClose($search)
EndFunc

I hope it helps solve your issues. Please keep in mind that by default it calls "*.*" all files. You seem to need "*.txt" :lmao:

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

This should do the trick for you...

Global $g_Dir = @MyDocumentsDir

Func _ParseFilesDir($folName, $fileType = "*.*")
    FileChangeDir($g_Dir & "\" & $folName & "\")
    $search = FileFindFirstFile($fileType)

    If $search = -1 Then
        _ErrorMsg("No files/directories matched the search pattern")
        Return
    EndIf

    While 1
        $found = FileFindNextFile($search)
        If @error Then ExitLoop
        $fileO = FileOpen($found, 0)
        While 1
            $line = FileReadLine($fileO)
            If @error = -1 Then ExitLoop
            ;>>>>>>>>>>>>>>>>>
            ;YOUR CODE HERE!!!
            ;<<<<<<<<<<<<<<<<<
        WEnd
        FileClose($fileO)
    WEnd
    
    FileClose($search)
EndFunc

I hope it helps solve your issues. Please keep in mind that by default it calls "*.*" all files. You seem to need "*.txt" :lmao:

JS

Looks like just what I need. Thanks so much. I have learned alot just by looking at the code. I'm gonna test it now.

Link to comment
Share on other sites

Directly from the helpfile:

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Line read:", $line)
Wend

FileClose($file)oÝ÷ Ø    趫~º&¶¡zZ_WºÚ"µÍÈÚÝÜÈH[[[YÈÙ[[È[HÝ[XÝÜKÌÍÜÙXÚH[Q[Ý[J    ][ÝÊ][ÝÊHÈÚXÚÈYHÙXÚØÈÝXØÙÜÙ[Y  ÌÍÜÙXÚHLH[ÙÐÞ
    ][ÝÑÜ][ÝË  ][ÝÓÈ[ËÙXÝÜYÈX]ÚYHÙXÚ]][ÝÊB^][YÚ[HB   ÌÍÙ[HH[Q[^[J ÌÍÜÙXÚ
HYÜ[^]ÛÜÙÐÞ
M   ][ÝÑ[N][ÝË  ÌÍÙ[JBÑ[ÈÛÜÙHHÙXÚ[B[PÛÜÙJ  ÌÍÜÙXÚ

Therefore, you need to look up FileReadLine and FileFindFirstFile in the help documentation and combine the two scripts together.

Also, please take the time to read Support Forum FAQ Q1 regarding how to make your code readable during scripting.

Who else would I be?
Link to comment
Share on other sites

Directly from the helpfile:

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Line read:", $line)
Wend

FileClose($file)oÝ÷ Ø    趫~º&¶¡zZ_WºÚ"µÍÈÚÝÜÈH[[[YÈÙ[[È[HÝ[XÝÜKÌÍÜÙXÚH[Q[Ý[J    ][ÝÊ][ÝÊHÈÚXÚÈYHÙXÚØÈÝXØÙÜÙ[Y  ÌÍÜÙXÚHLH[ÙÐÞ
    ][ÝÑÜ][ÝË  ][ÝÓÈ[ËÙXÝÜYÈX]ÚYHÙXÚ]][ÝÊB^][YÚ[HB   ÌÍÙ[HH[Q[^[J ÌÍÜÙXÚ
HYÜ[^]ÛÜÙÐÞ
M   ][ÝÑ[N][ÝË  ÌÍÙ[JBÑ[ÈÛÜÙHHÙXÚ[B[PÛÜÙJ  ÌÍÜÙXÚ

Therefore, you need to look up FileReadLine and FileFindFirstFile in the help documentation and combine the two scripts together.

Also, please take the time to read Support Forum FAQ Q1 regarding how to make your code readable during scripting.

Thx This is me.

yes I have spent A LOOOONG time trying to combine these two things. FilereadLine I am comfortable with as it was the basis of the original script. please have patience with this noob :lmao:

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...