Sign in to follow this  
Followers 0
chrisj90

Unknown Errors with FileRead()

7 posts in this topic

Hello,

A little background on what I'm trying to do. We have a webapp that runs off of XML docs. These XML docs are kicked out of a system to a network location so that they can be rendered into PDF docs for shipping purposes. Sometimes these XML docs contain special ASCII characters that cause the rending to not be possible. When this happens, we have to manually go into the haystack and find that needle and replace it with a similar character that is renderable.

What I'm trying to do is open selected XML files in a specific location, Read them to an array and convert them back to string so that I can remove all the "good" characters from the string and return the "bad" character as a result. Below is my code with associated error. Keep in mind that I'm only to the FileRead() part of this process, so the code is incomplete for the concept.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

Global $readFile[100]

getXMLFiles()
readXMLFiles()

Func getXMLFiles()
   Global $file = FileOpenDialog( "Select AutoBOL File", @WorkingDir & "\", "XML Files (*.XML)", 1 + 2 + 4)
   Global $stringArray = StringSplit( $file, '|', 1)
   Global $size = UBound($stringArray)
EndFunc

Func readXMLFiles()
   If $size = 2 Then
      Global $fileToRead = FileOpen( $stringArray[1])
      If $fileToRead = -1 Then
         MsgBox(0, "Error", "Unable to open file ")
      EndIf

      Global $readFile[1] = FileRead( $stringArray[1])
      FileClose( $fileToOpen)
   Else
      For $i = 1 To $size Step 1
         Global $fileToOpen = $stringArray[1] & "\" & $stringArray[$i + 1]
         Global $fileToRead = FileOpen( $fileToOpen)
         If $fileToRead = -1 Then
            MsgBox(0, "Error", "Unable to open file ")
         EndIf

         Global $readFile[$i] = FileRead( $fileToOpen)
      Next
      FileClose( $fileToOpen)
   EndIf
EndFunc

Error Message: One file selected

H:\!Projects\Incomplete\A~.au3 (23) : ==> Missing subscript dimensions in "Dim" statement.:
Global $readFile[1] = FileRead( $stringArray[1])
Global $readFile[1] = ^ ERROR
>Exit code: 1 Time: 7.316

Error Message: More than one file selected

H:\!Projects\Incomplete\A~.au3 (34) : ==> Missing subscript dimensions in "Dim" statement.:
Global $readFile[$i] = FileRead( $fileToOpen)
Global $readFile[$i] = ^ ERROR
>Exit code: 1 Time: 7.321

I've confirmed that the variable being passed to FileRead() contains a valid file path. Any ideas on why I'm getting these errors?

Share this post


Link to post
Share on other sites



I don't write/declare code in the same manner as you, but usually an array doesn't go beyond a function, without being assigned to another variable(s) first. That's what my tired brain is telling me anyway.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

chrisj90,

I moved some of your variable declarations to the top and changed the file type so I could run it. It works fine till I select multiple file where it errors on the stmt I commented on in the code.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

Global $readFile[100], $fileToRead, $fileToOpen                        ; defined vars before use

getXMLFiles()
readXMLFiles()

_arraydisplay($readFile)

Func getXMLFiles()
    Global $file = FileOpenDialog("Select AutoBOL File", @desktopdir & "\", "XML Files (*.txt)", 1 + 2 + 4) ; changes for testing
    Global $stringArray = StringSplit($file, '|', 1)
    Global $size = UBound($stringArray)
EndFunc   ;==>getXMLFiles

Func readXMLFiles()
    If $size = 2 Then
        $fileToRead = FileOpen($stringArray[1])
        If $fileToRead = -1 Then
            MsgBox(0, "Error", "Unable to open file ")
        EndIf

        $readFile[1] = FileRead($stringArray[1])
        FileClose($fileToOpen)
    Else
        For $i = 1 To $size Step 1
            $fileToOpen = $stringArray[1] & "\" & $stringArray[$i + 1]   ; this stmt does'nt make sense...what are you trying to do?
            $fileToRead = FileOpen($fileToOpen)
            If $fileToRead = -1 Then
                MsgBox(0, "Error", "Unable to open file ")
            EndIf

            $readFile[$i] = FileRead($fileToOpen)
        Next
        FileClose($fileToOpen)
    EndIf
EndFunc   ;==>readXMLFiles

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

chrisj90,

Quite a lot not quite right with that script - take a look at this one which works fine for me: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

Global $readFile, $stringArray ; Declare Globals here - not in functions

getXMLFiles()
readXMLFiles()

Func getXMLFiles()
    Local $file = FileOpenDialog("Select AutoBOL File", @WorkingDir & "\", "XML Files (*.au3)", 1 + 2 + 4) ; $file is Local
    $stringArray = StringSplit($file, '|') ; No need for the flag - you have only 1 delimiter character
    ; Local $size = UBound($stringArray) - 1 ; Just use the variable where needed
    ; Now redeclare the array to the correct size
    Global $readFile[UBound($stringArray) - 1]
EndFunc   ;==>getXMLFiles

Func readXMLFiles()
    ; Get the number of files to read directly from the array which we set to the correct size
    Switch UBound($readfile)
        Case 1
            $readFile[0] = FileRead($stringArray[1])
        Case Else
            For $i = 0 To UBound($readFile) - 1
                Local $fileToOpen = $stringArray[1] & "\" & $stringArray[$i + 1] ; Again a Local variable
                ;Global $fileToRead = FileOpen( $fileToOpen) ; No need to open to read
                ;If $fileToRead = -1 Then
                ;    MsgBox(0, "Error", "Unable to open file ")
                ;EndIf
                $readFile[$i] = FileRead($fileToOpen)
                ; FileClose( $fileToOpen) not needed
            Next
    EndSwitch
EndFunc   ;==>readXMLFiles

_ArrayDisplay($readFile)

Please ask if you have any questions. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

chrisj90,

Quite a lot not quite right with that script - take a look at this one which works fine for me: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

Global $readFile, $stringArray ; Declare Globals here - not in functions

getXMLFiles()
readXMLFiles()

Func getXMLFiles()
Local $file = FileOpenDialog("Select AutoBOL File", @WorkingDir & "\", "XML Files (*.XML)", 1 + 2 + 4) ; $file is Local
$stringArray = StringSplit($file, '|') ; No need for the flag - you have only 1 delimiter character
; Local $size = UBound($stringArray) - 1 ; Just use the variable where needed
; Now redeclare the array to the correct size
Global $readFile[UBound($stringArray) - 1]
EndFunc ;==>getXMLFiles

Func readXMLFiles()
; Get the number of files to read directly from the array which we set to the correct size
Switch UBound($readfile)
Case 1
$readFile[0] = FileRead($stringArray[1])
Case Else
For $i = 0 To UBound($readFile) - 1
Local $fileToOpen = $stringArray[1] & "\" & $stringArray[$i + 1] ; Again a Local variable
;Global $fileToRead = FileOpen( $fileToOpen) ; No need to open to read
;If $fileToRead = -1 Then
; MsgBox(0, "Error", "Unable to open file ")
;EndIf
$readFile[$i] = FileRead($fileToOpen)
; FileClose( $fileToOpen) not needed
Next
EndSwitch
EndFunc ;==>readXMLFiles

_ArrayDisplay($readFile)

Please ask if you have any questions. :)

M23

I guess a lot of my confusing code come from not being familiar with the File functions. Also, it's been probably 3 years since I've done any real scripting outside of Excel. I'll see how this works on Monday when work rolls back around. Thanks for the cleanup! Edited by Melba23
Typo

Share this post


Link to post
Share on other sites

chrisj90,

I moved some of your variable declarations to the top and changed the file type so I could run it. It works fine till I select multiple file where it errors on the stmt I commented on in the code.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

Global $readFile[100], $fileToRead, $fileToOpen ; defined vars before use

getXMLFiles()
readXMLFiles()

_arraydisplay($readFile)

Func getXMLFiles()
Global $file = FileOpenDialog("Select AutoBOL File", @desktopdir & "\", "XML Files (*.txt)", 1 + 2 + 4) ; changes for testing
Global $stringArray = StringSplit($file, '|', 1)
Global $size = UBound($stringArray)
EndFunc ;==>getXMLFiles

Func readXMLFiles()
If $size = 2 Then
$fileToRead = FileOpen($stringArray[1])
If $fileToRead = -1 Then
MsgBox(0, "Error", "Unable to open file ")
EndIf

$readFile[1] = FileRead($stringArray[1])
FileClose($fileToOpen)
Else
For $i = 1 To $size Step 1
$fileToOpen = $stringArray[1] & "\" & $stringArray[$i + 1] ; this stmt does'nt make sense...what are you trying to do?
$fileToRead = FileOpen($fileToOpen)
If $fileToRead = -1 Then
MsgBox(0, "Error", "Unable to open file ")
EndIf

$readFile[$i] = FileRead($fileToOpen)
Next
FileClose($fileToOpen)
EndIf
EndFunc ;==>readXMLFiles

kylomas

When you select multiple files it returns them to a single string delimited by a |. Doing a StringSplit() on the | breaks it into an array where each element is separated where the | was. You have to concatenate the array elements together to get the full file path for the files. I hope that makes more sense. If you do _ArrayDisplay($stringArray) you'll see what I'm talking about.

Share this post


Link to post
Share on other sites

chrisj90,

Thanks,

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
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
Sign in to follow this  
Followers 0