Sign in to follow this  
Followers 0
Scinner

ElseIf and more than, less than?

42 posts in this topic

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!

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 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.

Share this post


Link to post
Share on other sites

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Use FileReadLine instead of FileRead.


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

 

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Do you want to read the last 4 characters in a file?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#13 ·  Posted (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 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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

 

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

$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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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._______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
$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!

Share this post


Link to post
Share on other sites

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!

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