-2

I am having a problem with the if statements in my program. For example:

if %variable% GEQ 20 (
    Do Something
)

whenever I run that program I get an error which says that the number 20 unexpected at this time.

I've checked on google and under the command if /? but I still can't find an answer. Could anyone explain why this is happening and how to fix it?

Edit

This is where the variable is set:

for /f "Tokens=7" %%a IN (AimiMood.txt) DO (
    set Loneliness=%%a
)

And this is the file from which the value is extracted:

0 0 0 50 0 0 30 0 0 0 26/12/2015 23:50:30.5
halfer
  • 19,824
  • 17
  • 99
  • 186
5H4D0W
  • 19
  • 7
  • 1
    Can you post your full script or atleast part above or below. I guess you didn't close your parantheses well. – SomeDude Dec 27 '15 at 17:58
  • Sorry New To the site and im having some problems – 5H4D0W Dec 27 '15 at 18:03
  • I just tried your code above- it also works. To debug your code, remove @echo off in your batch script and run it, you might be able to figure out. – SomeDude Dec 27 '15 at 18:06
  • The code itself its pretty big (around 400 lines of code) but here: if %Loneliness% GEQ 20 ( if %BondS% GEQ 25 ( color 0d echo Aimi: Hey Shadow! I Missed You! set /a BondS+=3 if %BondS% GEQ 40 ( timeout 1 /nobreak >nul echo Aimi: *giggle* set /a BondS+=2 ) ) ) timeout 2 /nobreak >nul – 5H4D0W Dec 27 '15 at 18:06
  • I just did that and when I looked at it, it showed this if GEQ 20 ( but I have clearly decleared the variable %Loneliness% in here for /f "Tokens=7" %%a IN (AimiMood.txt) DO ( set Loneliness=%%a ) – 5H4D0W Dec 27 '15 at 18:10
  • I have run this code too. Reading and setting Lonliness from a text file, it works no problem. It is almost likely that you have some mismatch in your parantheses. Try to comment out code that is downstream and see if you are able to resolve "unexpected at this moment" and work your way down the script. – SomeDude Dec 27 '15 at 18:15
  • I'm sure that %variable% is empty... – aschipfl Dec 27 '15 at 18:29
  • given the symptoms, I'd bet, this piece of code is inside a block and needs [delayed expansion](http://stackoverflow.com/a/30284028/2152082) – Stephan Dec 27 '15 at 18:33
  • @aschipfl That is the problem but I have clearly set it using the code that I've shown in the edit of the question – 5H4D0W Dec 27 '15 at 18:35
  • Quick Update I have just put a pause in the code right after the variable is set and it shows that it cannot find the Text Document!!! – 5H4D0W Dec 27 '15 at 18:37
  • Your added code in the edit has nothing to do with the `if` statement of your original question (check out the variable names!); anyway, if the text file does not exist, the `for /F` loop does not iterate and the `set` command is therefore never executed... – aschipfl Dec 27 '15 at 19:59
  • @aschipfl I known that and I already have a solution. But in my case the program changed its own path because it was on a removable disk therefore I couldn't find the path to the text document. And I added the extra code in the question in case someone was wondering about it. – 5H4D0W Dec 28 '15 at 02:17

2 Answers2

0

I have not managed to Recreate this.

Check all your variables, make sure any = signs have no spaces between the label and data.

@echo off
set /p test=


if %test% GEQ 20 (
echo hi
)
pause

This example should show you about it. If 'Loneliness' is set as a pre defined integer (which can't be directly changed by the user), make sure that you're using set /a for that.

adsfg
  • 42
  • 1
  • 7
  • I tried using the set /a when setting the variable but I still get the same error. And when I am setting it there is no spaces between the label and the data. – 5H4D0W Dec 27 '15 at 18:21
  • @Shadow Alright, try my example, on a clean batch file, tell me if it works. – adsfg Dec 27 '15 at 18:23
  • When I run it the nothing is outputted The Only Explanation I am able to come up with is that when I am extracting the value from the text document it also includes the space before the value itself. – 5H4D0W Dec 27 '15 at 18:27
  • @Shadow Type a number, like 25 in, press enter, and it should say 'hi' – adsfg Dec 27 '15 at 18:28
  • I does echo 'hi' but I dont see how that will help me – 5H4D0W Dec 27 '15 at 18:30
  • It's a 'Template', you could say, for what you should be doing, other than the set being for a different input. – adsfg Dec 27 '15 at 18:33
  • You're extracting a value from a text document, I see. Please edit your main question, and show that code. – adsfg Dec 27 '15 at 18:34
  • @Shadow I've seen the edit. type 'set Loneliness' somewhere, and it should give you the value – adsfg Dec 27 '15 at 18:39
  • I found the answer already, thanks for trying and sorry for wasting your time – 5H4D0W Dec 27 '15 at 18:45
  • @Shadow It's alright matey, I was bored, so I wanted to answer this – adsfg Dec 27 '15 at 18:49
0

Okay Everyone I have found the solution that is bound to work!! the reason why it wasnt working in the first place was because the text document couldn't been found.

And the reason for that was because I though that just adding the name and the extension of the file would work in the program itself and the file are in the same directory which would work if the program was on the hard drive but in my case the program is on a Removable Disk, and because the removable disk doesn't have cmd on it it has the change the directory therefore the program and the document aren't in the same directory anymore!!

facepalm to myself

5H4D0W
  • 19
  • 7