Jump to content

Statement cannot be just an expression


Recommended Posts

I figured this would be an easy one, but apparently not.  I've re-checked syntax and several threads on if/else statements.  Why is the else statement and the EndIf statement giving this error after them?  Im just creating a file path.  If the path c:\test exists, I want to rename it to c:\testnew.  If it doesnt exist, I want to create it.  Maybe there's an easier way of doing this for you gurus...

 

Func CreatePath()
    If FileExists("C:\test") Then DirMove("C:\test", "C:\testnew", 1)
    Else DirCreate("C\testnew")
EndIf
EndFunc

The error after the Else and EndIf:

error:  syntax error

error:  Statement cannot be just an expression

 

Link to comment
Share on other sites

The layout of the  statement is wrong, it should be 

Func CreatePath()
    If FileExists("C:\test") Then
        DirMove("C:\test", "C:\testnew", 1)
    Else
        DirCreate("C:\testnew")
    EndIf
EndFunc   ;==>CreatePath

or shorter if you want

Func CreatePath1()
    If FileExists("C:\test") Then Return DirMove("C:\test", "C:\testnew", 1)
    DirCreate("C:\testnew")
EndFunc   ;==>CreatePath

There's also a colon missing from the file path in DirCreate in your post

Edited by benners
Link to comment
Share on other sites

1 hour ago, benners said:

The layout of the  statement is wrong, it should be 

Func CreatePath()
    If FileExists("C:\test") Then
        DirMove("C:\test", "C:\testnew", 1)
    Else
        DirCreate("C:\testnew")
    EndIf
EndFunc   ;==>CreatePath

or shorter if you want

Func CreatePath1()
    If FileExists("C:\test") Then Return DirMove("C:\test", "C:\testnew", 1)
    DirCreate("C:\testnew")
EndFunc   ;==>CreatePath

There's also a colon missing from the file path in DirCreate in your post

So the layout, meaning it has to be tabbed correctly, or dircreate has to be below 'else' and tabbed out further such as in your first solution? I didn't think that would (should?) be an issue, is that just an autoit thing?

Link to comment
Share on other sites

It's just the syntax of langauges, some are the some, some are a lot different. In languages like C++, Java, Javascript your if/else statement would be valid since it uses curly braces ({}) to encase statements

if (FileExists("Path")) { DirMove("Path", "NewPath"); }
else { DirCreate("Path"); }

In languages like AutoIt and Visual Basic it relies on new lines to start the encapsulated statement (the tabs are just to prettify it and make it easier to read. When you compile the script to an exe all of the excess tabs/spaces are removed to reduce file size)

Python on the other hand has be to tabbed correctly, there is no encapsulating words/symbols

if FileExists('Path'):
    DirMove('Path', 'NewPath')
else:
DirCreate('Path')
# this DirCreate would always execute no matter if the FileExists or not and the compiler would raise an error expecting a statement for the else block since there's nothing tabbed below else:

There's other examples, these are the only languages I know though lol

Link to comment
Share on other sites

@InunoTaishou

I understand you and this example shows what I mean :
Short Code:

   If FileExists("C:\test") Then MsgBox(0,"File","Exist")
    MsgBox(0,"","I will Create it anyway")
   DirCreate("C:\testnew")

If Else Code:

If FileExists("C:\test") Then
     MsgBox(0,"File","Exist")
Else
    MsgBox(0,"","file not found")
   DirCreate("C:\testnew")
EndIf

So This short code not do the whole If Else Job.

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