lolz Posted June 8, 2017 Share Posted June 8, 2017 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 More sharing options...
benners Posted June 8, 2017 Share Posted June 8, 2017 (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 June 8, 2017 by benners abberration and lolz 1 1 Link to comment Share on other sites More sharing options...
InunoTaishou Posted June 8, 2017 Share Posted June 8, 2017 Even shorter! Func CreatePath1() Return FileExists("C:\test") ? DirMove("C:\test", "C:\testnew", 1) : DirCreate("C:\testnew") EndFunc ;==>CreatePath lolz, benners and abberration 2 1 Link to comment Share on other sites More sharing options...
lolz Posted June 9, 2017 Author Share Posted June 9, 2017 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 More sharing options...
InunoTaishou Posted June 9, 2017 Share Posted June 9, 2017 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 More sharing options...
abdulrahmanok Posted June 9, 2017 Share Posted June 9, 2017 "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. Link to comment Share on other sites More sharing options...
InunoTaishou Posted June 9, 2017 Share Posted June 9, 2017 @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 Link to comment Share on other sites More sharing options...
abdulrahmanok Posted June 9, 2017 Share Posted June 9, 2017 @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 More sharing options...
InunoTaishou Posted June 9, 2017 Share Posted June 9, 2017 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. Link to comment Share on other sites More sharing options...
abdulrahmanok Posted June 9, 2017 Share Posted June 9, 2017 oh it's my fault to Igonre Func got it now thanks for explaining Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now