Error in If logic

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
venkimuck
Posts: 3
Joined: 01 Apr 2010 12:22

Error in If logic

#1 Post by venkimuck » 01 Apr 2010 12:33

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Error in If logic

#2 Post by jeb » 01 Apr 2010 13:48

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

venkimuck
Posts: 3
Joined: 01 Apr 2010 12:22

Re: Error in If logic

#3 Post by venkimuck » 01 Apr 2010 14:11

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Error in If logic

#4 Post by jeb » 02 Apr 2010 00:48

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

venkimuck
Posts: 3
Joined: 01 Apr 2010 12:22

Re: Error in If logic

#5 Post by venkimuck » 05 Apr 2010 09:25

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 :)

Post Reply