Jump to content

Problem with an Loop and an Array


Recommended Posts

I've got a direcory with about 250 txt files wiht information about Office Installations on my Network. What I want to do is have AutoIt read each one of those files and make a csv file out them containing (Computer Name, 1st Line of Text File, 2nd Line, 3rd Line, and so on...).

Here's my code so far:

; Creates a csv file using the files generated by FindMSOfficeInstallation.au3

Dim $line[10]

FileChangeDir("C:\MSOffInv")
$search = FileFindFirstFile("off_*.txt")
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

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

; Close the search handle
FileClose($search)

What's happening when I run the code is the $line[$x] variable always has the same value, it's like it's always reading the 1st line of the 1st text file, and it reads it more than 10 times wich causes an error in the script b/c the $line[$x] variable is only dimed for 10. Please let me know if you have any suggestions.

Thanks in advance,

Ian

"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Link to comment
Share on other sites

example i wrote yesterday

$oFile = FileOpen(@TempDir & "\dir.txt", 0);location of the name of the batch file (2345 from your example)
    $hFile = FileOpen(@TempDir & "\dir1.txt", 0);location of your file of your list file you created with tifs
    $nFile = FileOpen(@TempDir & "\dir2.txt", 1);location of your file new file with the file/date/tif info
    
; Check if file opened for reading OK
    If $hFile = -1 Or $oFile = -1 Or $nFile = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
    
; Read in lines of text until the EOF is reached
    While 1
        $sLine = FileReadLine($hFile)
        If @error = -1 Then ExitLoop
        If $sLine <> "" Then
            $result = StringTrimRight( $oFile, 3)
            $time =  FileGetTime( $sLine, 1, 1)
            FileWriteLine ( $nFile, $result & "  " & $time & "  " & $sLine)
            $Msg = GUIGetMsg()
            If $Msg = $GUI_EVENT_CLOSE Then ExitLoop
        EndIf
    WEnd
    FileClose($hFile)
    FileClose($nFile)
    FileClose($oFile)

hope it helps... no time to fix yours right now

8)

NEWHeader1.png

Link to comment
Share on other sites

depends

do you want to "find next file"?

or "open File" to read what is inside?

8)

<{POST_SNAPBACK}>

I want to read what's inside the file, but it doesn't seem to have a problem reading whats in the file with the code I have. The probem is, I think, with the loop and it reading the same line over and over again.

Thanks,

Ian

"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Link to comment
Share on other sites

I want to read what's inside the file, but it doesn't seem to have a problem reading whats in the file with the code I have.  The probem is, I think, with the loop and it reading the same line over and over again.

Thanks,

Ian

<{POST_SNAPBACK}>

this is why

While 1

$file = FileFindNextFile($search)

this finds the next file over and over

NEWHeader1.png

Link to comment
Share on other sites

I didn't think the FileOpen() was necessary with the FileFindNextFile().  Is it?

Thanks,

Ian

<{POST_SNAPBACK}>

To my knowledge, and after checking the helpfile it doesnt say anywhere that you have to use the FileOpen() in conjunction with FileFindFirst/NextFile() functions.

FileOpen does have to be used in conjunction with FileReadLine() from the helpfile, but the $file parameter you are passing I dont believe to be the file handle that you can read a line with.

May want to try something more like this...

; Creates a csv file using the files generated by FindMSOfficeInstallation.au3

Dim $line[10]

FileChangeDir("C:\MSOffInv")
$search = FileFindFirstFile("off_*.txt")
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    
 ; Read in lines of text until the EOF is reached
    $fileO = FileOpen(Eval($file), 0);Open the found file
    For $x = 0 To 9 Step 1
        $line[$x] = FileReadLine($fileO)
        If @error = -1 Then ExitLoop
        MsgBox(0, "Line read:", $line[$x])
    Next
    FileClose($fileO);Close the File that was found and opened.
WEnd

; Close the search handle
FileClose($search)

Edit: Wow lota comments while I was cooking this one up :) I hope I have posted something useful.

I am unable to test the above example so let me know how it goes.

JS

Edited by JSThePatriot

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

Thanks JS.

I tired your code mods... And this is the error msg that I still get.

>Running: (3.1.1.0):c:\Program Files\AutoIt3\autoit3.exe "C:\Scripts (that I've written)\CFCU\ProcessOfficeInventory.au3" 

C:\Scripts (that I've written)\CFCU\ProcessOfficeInventory.au3 (26) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$line[$x]= FileReadLine($file1)

^ ERROR

>AutoIT3.exe ended.

Here's my code.

; Creates a csv file using the files generated by FindMSOfficeInstallations.au3

Dim $line[10]

FileChangeDir("C:\MSOffInv")
$search = FileFindFirstFile("off_*.txt")
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
   ; Read in lines of text until the EOF is reached
    $x = 0
    While 1
        $file1 = FileOpen(Eval($file), 0); Open the found file
        $x = $x + 1
        $line[$x] = FileReadLine($file1)
        If @error = -1 Then ExitLoop
        MsgBox(0, "Line read:", $line[$x])
    Wend
    FileClose($file1); Close the File that was found and opened.
    
WEnd

; Close the search handle
FileClose($search)

It still looks like the problem is that it doesn't know when to get out of the 2nd While...Wend loop, even though I have 'If @error = -1 Then ExitLoop', which is supposed to tell the code to exit the loop at the (EOF) End Of File.

Thanks again to everyone,

Ian

"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Link to comment
Share on other sites

You need to use my code exactly as I have it, because if the file has more than 10 lines. You are running an Array. You have to specify how many dimensions it will have. You may be able to UBound() the array to keep it going if it needs to read more than the first 10 lines.

Use my code as I have it, or figure out how to get yours to exit as it gets to the 10th line. Also you are storing your line in the second dimension of the array not the first. Arrays in AutoIt start at 0... so your array is 0-9 not 1-10.

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

Ok JS.

I've got you code running. And I added a line to make it display the filename that it's reding from, so that I can follow it.

; Creates a csv file using the files generated by FindMSOfficeInstallations.au3

Dim $line[10]

FileChangeDir("C:\MSOffInv")
$search = FileFindFirstFile("off_*.txt")
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
   ; Read in lines of text until the EOF is reached
;$x = 0
    For $x = 0 To 9 Step 1
        $file1 = FileOpen(Eval($file), 0); Open the found file
    ;$x = $x + 1
        $line[$x] = FileReadLine($file1)
        If @error = -1 Then ExitLoop
        MsgBox(0, "File: " & $file & " Line read:", $line[$x])
    Next
    FileClose($file1); Close the File that was found and opened.
    
WEnd

; Close the search handle
FileClose($search)

Here's the added part: MsgBox(0, "File: " & $file & " Line read:", $line[$x])

It steps through each file 10 times, but it doen't read the text in the files. Any ideas?

Thanks again,

Ian

"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Link to comment
Share on other sites

Ok, I think I've got it figured out... It looks like some of the commands were in the wrong place... Here's the updated code.

; Creates a csv file using the files generated by FindMSOfficeInstallations.au3

Dim $line[10]

FileChangeDir("C:\MSOffInv")
$search = FileFindFirstFile("off_*.txt")
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    $file1 = FileOpen($file, 0); Open the found file
    
; Read in lines of text of each file until the EOF is reached
    $x = 0
    While 1
        $x = $x + 1
        $line[$x] = FileReadLine($file1)
        If @error = -1 Then ExitLoop
        MsgBox(0, "File: " & $file & " Line read:", $line[$x])
    WEnd
    FileClose($file1); Close the File that was found and opened.
    
WEnd

; Close the search handle
FileClose($search)

This line $file1 = FileOpen($file, 0) ; Open the found file didn't need the Eval(), and was inside the 2nd While ... WEnd loop, so it kept opening the same file.

Thanks to everyone who helped out on this.

Ian

"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Link to comment
Share on other sites

Ah, okay. I checked AutoIt documentation and thought that it had to be a "filename" not a variable/file handle. That is why I used the Eval.

If you notice in my script above... I did put the file open at the top above the second loop :)

Glad you got it all working,

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

Ah, okay. I checked AutoIt documentation and thought that it had to be a "filename" not a variable/file handle. That is why I used the Eval.

If you notice in my script above... I did put the file open at the top above the second loop :)

Glad you got it all working,

JS

<{POST_SNAPBACK}>

I guess I should have paid more attention, would have saved myself a lot of time. Thanks for you help.

Ian

"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Link to comment
Share on other sites

lol... it help at times :evil::)

Not a problem man. I enjoy helping.

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

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