You are not logged in.

#1 12 Jul 2007 20:03

avery_larry
Member
Registered: 11 Jul 2007
Posts: 266

Why doesn't this work . . .

setlocal enabledelayedexpansion
set nfiles=35
set lthread=5
if /i %nfiles% lss %dfiles% (
      if 1%nfiles:~0,-1%==1 set nfiles=10
      for /l %%a in (%lthread%,-1,!nfiles:~0,-1!) do echo %%a
)


It should execute the for loop 3 times--echo'ing 5 then 4 then 3.  Instead, it echo's 5 then 4 then 3 then 2 then 1 then 0.  If I add an intermediate variable like the following it works:

setlocal enabledelayedexpansion
set nfiles=35
set lthread=5
if /i %nfiles% lss %dfiles% (
      if 1%nfiles:~0,-1%==1 set nfiles=10
      set l2thread=!nfiles:~0,-1!
      for /l %%a in (%lthread%,-1,!l2thread!) do echo %%a
)

Offline

#2 13 Jul 2007 10:09

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Why doesn't this work . . .

I presume you've missed something off that snippet, cause I don't see where "dfiles" is defined.

But, assuming it's some figure higher than "nfiles", a quick run of your script (without using "echo off") reveals the problem (the following being what's displayed on screen for me):

if /I 35 LSS 100 (
if 13 == 1 set nfiles=10
 for /L %a in (5 -1 !nfiles:~0 -1!) do echo %a
)

As you can see, the "for" command has taken it upon itself to replace the commas with spaces, thus breaking !nfiles:~0,-1! ... presumably due to the fact that you've used delayed expansion.  Well done for finding an idiosyncrasy of "cmd" that I hadn't yet encountered!

Useful cmd tip #47263: Using "echo on" at problematic parts of a script can help you debug.

Last edited by bluesxman (13 Jul 2007 10:17)


cmd | *sh | ruby | chef

Offline

Board footer

Powered by