Page 1 of 1

Error in If logic

Posted: 01 Apr 2010 12:33
by venkimuck
The below piece of code keeps erroring out saying "8 was unexpected at this time." anb its poiting to this line : "if %var1:~1,8% NEQ Disabled". Any thoughts? Very strange thing is it works sometimes... I have seen it work many times and it has given error also many times... Its the same piece of code no change was done during both success and error.


@Echo off

if %date:~0,3% EQU Sat if %time:~0,2% EQU 10 (
schtasks /query | find /i "pdh_load_n_start_new_batches" > E:\PDH_Archive\SchStat.txt

for /f "tokens=2-3" %%# in ('type E:\PDH_Archive\SchStat.txt') do set var1= %%#
echo %var1%
if %var1:~1,8% NEQ Disabled (
echo Schedled task is not disabled. Will be disabling task now
)
)

Re: Error in If logic

Posted: 01 Apr 2010 13:48
by jeb
Hello venkimuck,

you got this error if var1 is empty/not exist.

The batch logic expands

Code: Select all

if %var1:~1,8% NEQ Disabled (

to

Code: Select all

if ~1,8 NEQ Disabled (


hope it helps
jeb

Re: Error in If logic

Posted: 01 Apr 2010 14:11
by venkimuck
1) The first if condition itself is not true, so it should not even go into the ( section under the if condition.
2) The file SchStat.txt does have value and when do the echo %var1%, it works fine (after removing the first if logic).

Re: Error in If logic

Posted: 02 Apr 2010 00:48
by jeb
Hello venkimuck,

1) The first if condition itself is not true, so it should not even go into the ( section under the if condition.


That's is the problem, it's not important if the first if condition is true.
The whole section is expanded, and the syntax must be correct.

Try

Code: Select all

if "%var1:~1,8%" NEQ "Disabled" (


jeb

Re: Error in If logic

Posted: 05 Apr 2010 09:25
by venkimuck
That worked... Thanks. But, given the nature of issue I have been facing, I would wait for more executions before I can confidently say this corrected the issue :)