BetaLeaf

If then compare not working

11 posts in this topic

#1 ·  Posted (edited)

So what I want to happen is when I run the script on a nonNTFS file system it should fail to run and state an error message box. Instead, it fails at the comparing at the If then statement. No matter what the output is for DriveGetFileSystem (which is working properly), it always fails to compare it even when the strings are matching.

;ProblemDemoScript
Local $DriveLetter = StringLeft(@ScriptDir, 2) ;Get Drive Letter and Store it.
MsgBox(0,"Drive Letter",$DriveLetter) ;Echo Drive Letter
MsgBox(0,"Drive Letter",DriveGetFileSystem($DriveLetter)) ;Echo File system
If not DriveGetFileSystem($DriveLetter) = 'abcd' Then;intentional set wrong to show it failing. Even with not remove it still fails to properly compare. (problem is here)
  MsgBox(0, "Safety Engaged", 'Not using NTFS FileSystem. Currently using "'&DriveGetFileSystem($DriveLetter)&'"');should be called under normal conditions.
  Exit
Else
  MsgBox(0,"No Problems","Successful");should never be called since 'abcd' isn't a real file system and not a possible output.
EndIf
Edited by BetaLeaf

False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Use:

If not (DriveGetFileSystem($DriveLetter) = 'abcd') Then

Jos

Thanks you very much for your help. I implemented this and it's working properly. Now I have another question. Why is the () around the actual command necessary? I want to be able to understand it ya know?

Edited by BetaLeaf

False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

It's because of the operators precedence

As the NOT has a higher precedence than "=" it occurs before the = operation

Share this post


Link to post
Share on other sites

Thanks you very much for your help. I implemented this and it's working properly. Now I have another question. Why is the () around the actual command necessary? I want to be able to understand it ya know?

So translated to what is state: your initial statement does this:

If (not DriveGetFileSystem($DriveLetter)) = 'abcd' Then

Jos :)


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

It's because of the operators precedence

As the NOT has a higher precedence than "=" it occurs before the = operation

I'm afraid I still don't understand. 


False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

So translated to what is state: your initial statement does this:

If (not DriveGetFileSystem($DriveLetter)) = 'abcd' Then

Jos :)

The first one you posted works fine. Should I be using this one instead?


False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

The first one you posted works fine. Should I be using this one instead?

No because this is what happened with your initial If which wasn't working!

It was comparing not DriveGetFileSystem($DriveLetter) to 'abcd' which ofcourse fails.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

No because this is what happened with your initial If which wasn't working!

It was comparing not DriveGetFileSystem($DriveLetter) to 'abcd' which ofcourse fails.

Jos

I still don't get it. Let me rephrase my original question. When should I put () around the entire statement as in your first example? I want to be able to understand this but I have autism. Could you make a few examples of what does and doesn't work and explain why. Maybe that will help me understand it if I can see examples. To me, when I look at If not drivegetfilesystem($driveletter) = 'abcd' shouldn't it just know that I mean If this statement is not this, then do this?

I really want to be able to understand this. I don't like asking for help and perfer to be self reliant.

Edited by BetaLeaf

False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

BetaLeaf,

AutoIt, like all programming languages, has an "operator precedence" list - for AutoIt this is set out in the Help file:

 

When more than one operator is used in an expression the order in which things happen is controlled by operator precedence. The precedence used in AutoIt is given below. Where two operators have the same precedence the expression is evaluated left to right.

From highest precedence to lowest:

Not

^

* /

+ -

&

< > <= >= = <> ==

And Or

So let us run through what happened in your comparison:

; Original
not DriveGetFileSystem($DriveLetter) = 'abcd'

; DriveGetFileSystem always returns a value (either string or numeric)
; NOT is a Boolean operator and AutoIt interprets a value as TRUE, an empty string (") or 0 as FALSE
; So this line reads to AutoIt as
not (TRUE) = "abcd"

; Highest operator is "NOT" and the line becomes
FALSE = "abcd"

; Which is obviously not what you wanted to happen
Happy so far?

Now let us force operator precedence by adding parentheses which AutoIt actions before external operators

; Original
not (DriveGetFileSystem($DriveLetter) = 'abcd')

; AutoIt actions the code inside the parentheses first and so we get (assuming the comparison is TRUE)
not (TRUE)

; Which is what you wanted to happen
All clear? :)

M23

1 person likes this

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

BetaLeaf,

AutoIt, like all programming languages, has an "operator precedence" list - for AutoIt this is set out in the Help file:

 

When more than one operator is used in an expression the order in which things happen is controlled by operator precedence. The precedence used in AutoIt is given below. Where two operators have the same precedence the expression is evaluated left to right.

From highest precedence to lowest:

Not

^

* /

+ -

&

< > <= >= = <> ==

And Or

So let us run through what happened in your comparison:

; Original
not DriveGetFileSystem($DriveLetter) = 'abcd'

; DriveGetFileSystem always returns a value (either string or numeric)
; NOT is a Boolean operator and AutoIt interprets a value as TRUE, an empty string (") or 0 as FALSE
; So this line reads to AutoIt as
not (TRUE) = "abcd"

; Highest operator is "NOT" and the line becomes
FALSE = "abcd"

; Which is obviously not what you wanted to happen
Happy so far?

Now let us force operator precedence by adding parentheses which AutoIt actions before external operators

; Original
not (DriveGetFileSystem($DriveLetter) = 'abcd')

; AutoIt actions the code inside the parentheses first and so we get (assuming the comparison is TRUE)
not (TRUE)

; Which is what you wanted to happen
All clear? :)

M23

 

Yes, This explained it for me. Thank you for taking the time to explain it. I sincerely appreciate it. Have a great day. /Close_thread


False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

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