Jump to content

ElseIf and more than, less than?


Go to solution Solved by Melba23,

Recommended Posts

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

  • Replies 41
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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.
Link to comment
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.

Link to comment
Share on other sites

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.
Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
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.
Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
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!

Link to comment
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!

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