Jump to content

If then compare not working


 Share

Go to solution Solved by Jos,

Recommended Posts

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

 

 

Link to comment
Share on other sites

  • Developers
  • Solution

Use:

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

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

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 :)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

  • Moderators

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

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

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

 

 

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