lolz

Statement cannot be just an expression

10 posts in this topic

#1 ·  Posted

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

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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
1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted

Even shorter!

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

 

2 people like this

Share this post


Link to post
Share on other sites

#4 ·  Posted

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?

Share this post


Link to post
Share on other sites

#5 ·  Posted

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

Share this post


Link to post
Share on other sites

#6 ·  Posted

"or shorter if you want

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

I'm agree with you but in this code there is no "Else" statement so it will create Test folder even if Folder is Exists.

Share this post


Link to post
Share on other sites

#7 ·  Posted

@abdulrahmanok if the file exists it returns the return value from DirMove, so it will never create the testnew dir if the C:\test exists

Share this post


Link to post
Share on other sites

#8 ·  Posted

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

Share this post


Link to post
Share on other sites

#9 ·  Posted

Of course short code will execute DirCreate no matter what because it's not returning from the running function, which is exactly what @benners code does. Once you hit the Return statement in a function all code below it does not execute.

Share this post


Link to post
Share on other sites

#10 ·  Posted

oh it's my fault to Igonre Func got it now thanks for explaining :)

 

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