Scinner Posted June 24, 2013 Share Posted June 24, 2013 Can someone tell me what Im doing wrong in this script? Local $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 4 character at a time until the EOF is reached While 1 Local $chars = FileRead($file, 4) If @error = -1 Then ExitLoop WEnd FileClose($file) If $chars > 10 Then MsgBox(0, "Char read:", "Yes") ElseIf $chars < 10 Then MsgBox(0, "Char read:", "No") EndIf Exit The number in the .txt is 22 but I get MsgBox with "No" Thanks guys! Link to comment Share on other sites More sharing options...
jdelaney Posted June 24, 2013 Share Posted June 24, 2013 (edited) Local $file = FileOpen(@DesktopDir & "\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 4 character at a time until the EOF is reached $chars = "" While 1 $chars &= FileRead($file, 4) If @error = -1 Then ExitLoop WEnd FileClose($file) If StringLen($chars)> 10 Then MsgBox(0, "Char read:", "Yes" & $chars) Else MsgBox(0, "Char read:", "No") EndIf Edited June 24, 2013 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted June 24, 2013 Share Posted June 24, 2013 Are you trying to count the characters? The amount of characters read is in @extended, not the return of FileRead .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
Scinner Posted June 24, 2013 Author Share Posted June 24, 2013 Are you trying to count the characters? The amount of characters read is in @extended, not the return of FileRead No I just want to check the test.txt and if the number is equal or higher than 10 I want it to say Yes, if not then No. Local $file = FileOpen(@DesktopDir & "\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 4 character at a time until the EOF is reached $chars = "" While 1 $chars &= FileRead($file, 4) If @error = -1 Then ExitLoop WEnd FileClose($file) If StringLen($chars)> 10 Then MsgBox(0, "Char read:", "Yes" & $chars) Else MsgBox(0, "Char read:", "No") EndIf Don't work. Don't even get a MsgBox with this. Link to comment Share on other sites More sharing options...
jdelaney Posted June 24, 2013 Share Posted June 24, 2013 (edited) Sure it does, add a file to your desktop, or change to be a valid file...also, if you want greater or equal, you need this: If StringLen($chars)>= 10 Then or, since these are all integers: If StringLen($chars)>9 Then Edited June 24, 2013 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
water Posted June 24, 2013 Share Posted June 24, 2013 What FileRead returns is a string. If you need to comapre it to a number convert the string to a number before comparing. If Number($chars) > 10 Then MsgBox(0, "Char read:", "Yes") ElseIf Number($chars) < 10 Then MsgBox(0, "Char read:", "No") EndIf My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
somdcomputerguy Posted June 24, 2013 Share Posted June 24, 2013 Use FileReadLine instead of FileRead. - Bruce /*somdcomputerguy */ If you change the way you look at things, the things you look at change. Link to comment Share on other sites More sharing options...
Scinner Posted June 24, 2013 Author Share Posted June 24, 2013 Sure it does, add a file to your desktop, or change to be a valid file You Edited the script like 10 times. The last one gives me a MsgBox but not the right answer. Link to comment Share on other sites More sharing options...
jdelaney Posted June 24, 2013 Share Posted June 24, 2013 what is the 'right' answer...do you want a count of groupings of 4, or a total count of chars IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
Scinner Posted June 24, 2013 Author Share Posted June 24, 2013 What FileRead returns is a string. If you need to comapre it to a number convert the string to a number before comparing. If Number($chars) > 10 Then MsgBox(0, "Char read:", "Yes") ElseIf Number($chars) < 10 Then MsgBox(0, "Char read:", "No") EndIf This gives me No what ever number i have in the .txt Local $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 4 character at a time until the EOF is reached While 1 Local $chars = FileRead($file, 4) If @error = -1 Then ExitLoop WEnd FileClose($file) If Number($chars) > 10 Then MsgBox(0, "Char read:", "Yes") ElseIf Number($chars) < 10 Then MsgBox(0, "Char read:", "No") EndIf Exit Link to comment Share on other sites More sharing options...
Scinner Posted June 24, 2013 Author Share Posted June 24, 2013 what is the 'right' answer...do you want a count of groupings of 4, or a total count of chars 22 is more than 10 so I want Yes. 9 is less than 10 so I want No 10 is equal to 10 so I want Yes Link to comment Share on other sites More sharing options...
water Posted June 24, 2013 Share Posted June 24, 2013 Do you want to read the last 4 characters in a file? My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
jdelaney Posted June 24, 2013 Share Posted June 24, 2013 (edited) Check my post once more, that was an older version: Local $file = FileOpen(@DesktopDir & "\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 4 character at a time until the EOF is reached $chars = "" $iCount = 0 While 1 $chars &= FileRead($file, 4) If @error = -1 Then ExitLoop $iCount += 1 WEnd FileClose($file) MsgBox(1,1,"Chrs_count=[" & StringLen($chars) & "], group_count=[" & $iCount & "]") If StringLen($chars)>= 10 Then MsgBox(0, "Char read:", "Yes") Else MsgBox(0, "Char read:", "No") EndIf Edited June 24, 2013 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
Scinner Posted June 24, 2013 Author Share Posted June 24, 2013 Do you want to read the last 4 characters in a file? Sometimes there is 1 char in the .txt but there can be up to 5 chars. So everything thats equal or above 10 I want to get Yes, anything below I want No. Link to comment Share on other sites More sharing options...
Scinner Posted June 24, 2013 Author Share Posted June 24, 2013 Check my post once more, that was an older version: Local $file = FileOpen(@DesktopDir & "\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 4 character at a time until the EOF is reached $chars = "" $iCount = 0 While 1 $chars &= FileRead($file, 4) If @error = -1 Then ExitLoop $iCount += 1 WEnd FileClose($file) MsgBox(1,1,"Chrs_count=[" & StringLen($chars) & "], group_count=[" & $iCount & "]") If StringLen($chars)>= 10 Then MsgBox(0, "Char read:", "Yes") Else MsgBox(0, "Char read:", "No") EndIf Sorry, but the comments in the code is left from the example script. Link to comment Share on other sites More sharing options...
somdcomputerguy Posted June 24, 2013 Share Posted June 24, 2013 file.txt contains 22 Local $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 1 character at a time until the EOF is reached While 1 Local $chars = FileRead($file, 1) If @error = -1 Then ExitLoop MsgBox(0, "Char read:", $chars) WEnd FileClose($file) returns 2 (# of chars in file) Local $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 1 character at a time until the EOF is reached While 1 Local $chars = FileReadLine($file, 1) If @error = -1 Then ExitLoop MsgBox(0, "Char read:", $chars) WEnd FileClose($file) returns 22 (value in file) - Bruce /*somdcomputerguy */ If you change the way you look at things, the things you look at change. Link to comment Share on other sites More sharing options...
water Posted June 24, 2013 Share Posted June 24, 2013 $sInput = FileRead("test.txt") $sInput = StringStripWS($sInput, 8) If Number($sInput) < 10 Then MsgBox(0, "Info", "Input is < 10") Else MsgBox(0, "Info", "Input is >= 10") EndIf The whole file is read in one go, spaces stripped off, converted to a number and compared. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 24, 2013 Moderators Share Posted June 24, 2013 Hi,Some here seem to be getting a bit heated so let us all calm down and go back to the beginning. Scinner,How about providing a couple of example files - and an explanation of whether they should be classed as "Yes" or "No" - so that we can actually see what we are actually dealing with. 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...
Scinner Posted June 24, 2013 Author Share Posted June 24, 2013 $sInput = FileRead("test.txt") $sInput = StringStripWS($sInput, 8) If Number($sInput) < 10 Then MsgBox(0, "Info", "Input is < 10") Else MsgBox(0, "Info", "Input is >= 10") EndIf The whole file is read in one go, spaces stripped off, converted to a number and compared. You da man! Works like a charm! Thanks for understanding my bad English and my bad explaining! Link to comment Share on other sites More sharing options...
Scinner Posted June 24, 2013 Author Share Posted June 24, 2013 Hi, Some here seem to be getting a bit heated so let us all calm down and go back to the beginning. Scinner, How about providing a couple of example files - and an explanation of whether they should be classed as "Yes" or "No" - so that we can actually see what we are actually dealing with. M23 No ones getting heated Im just bad at making myself understood in English! Sorry, and thank you guys! 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