chrisj90 Posted April 26, 2013 Share Posted April 26, 2013 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. expandcollapse popup#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? Link to comment Share on other sites More sharing options...
TheSaint Posted April 26, 2013 Share Posted April 26, 2013 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. 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) Link to comment Share on other sites More sharing options...
kylomas Posted April 26, 2013 Share Posted April 26, 2013 (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. expandcollapse popup#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 April 26, 2013 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 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 26, 2013 Moderators Share Posted April 26, 2013 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
chrisj90 Posted April 26, 2013 Author Share Posted April 26, 2013 (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 April 26, 2013 by Melba23 Typo Link to comment Share on other sites More sharing options...
chrisj90 Posted April 26, 2013 Author Share Posted April 26, 2013 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. expandcollapse popup#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. Link to comment Share on other sites More sharing options...
kylomas Posted April 26, 2013 Share Posted April 26, 2013 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 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