Counter in for batch doesn't count

March 25, 2011 at 02:50:03
Specs: Windows 7 (64)
I've created a batch file that should do <task> when it reaches a condition in certain lines in a txt file, but the line counter always remains in 0. Can you help me?

*************************************
@echo off
setLocal EnableDelayedExpansion
set linecount=0
for /f "tokens=* delims=" %%x in ('type \1.txt') do (
call :COUNT
echo %linecount%
set linea=%%x
if %linecount% geq 6 if %linecount% leq 11 set linea=!linea:openldap=%openldap_path%!
call :show !linea!
)
goto:eof
:show
echo %* >> temp.txt
goto:eof

:COUNT
set /a linecount+=1
*************************************

Thank you!


See More: Counter in for batch doesnt count

Report •


#1
March 25, 2011 at 03:03:24
It's a variable expansion issue:

http://www.computing.net/howtos/sho...


Here is an untested fixed version, it assumes that "%openldap_path%" is defined before the for loop begins:


@echo off
setLocal EnableDelayedExpansion
set linecount=0
for /f "tokens=* delims=" %%x in ('type 1.txt') do (
    set /a linecount+=1
    echo !linecount!
    set linea=%%x
    if !linecount! geq 6 if !linecount! leq 11 (
        set linea=!linea:openldap=%openldap_path%!
    )
    >> temp.txt echo !linea!
)


Report •

#2
March 25, 2011 at 03:37:35
Thank you a lot! It works great. And yes, you were right, "%openldap_path%" is defined before the for loop begins. I put here the whole code:

@echo off
setLocal EnableDelayedExpansion
echo Write openLDAP installation path:
set /P openldap_path=
set linecount=0
for /f "tokens=* delims=" %%x in ('type 1.txt') do (
set /a linecount+=1
echo !linecount!
set linea=%%x
if !linecount! geq 6 if !linecount! leq 11 (
set linea=!linea:"openldap=%openldap_path%!
)
>> temp.txt echo !linea!
)

Thank you again for the instant response!


Report •

#3
March 25, 2011 at 05:36:53
Now I have another question in the same line; I want to change the lines found between those number lines OR in another number line. I have written:

@echo off
setlocal enabledelayedexpansion
echo Write openLDAP installation path:
set /P openldap_path=
set linecount=0
for /f "tokens=* delims=" %%x in ('type 1.txt') do (
set /a linecount+=1
set linea=%%x
if !linecount! geq 6 if !linecount! leq 11 (
set linea=!linea:openldap=%openldap_path%!
) else (
if !linecount! equ 22(
set linea=!linea:openldap=%openldap_path%!
)
)
>> temp.txt echo !linea!
)

But it says my syntax is incorrect. How can I get this done? Is there something similar to OR? I've read that not for "IF" statements in batch,


EDIT: OK, I solved these.

@echo off
setlocal enabledelayedexpansion
echo Write openLDAP installation path:
set /P openldap_path=
set linecount=0
for /f "tokens=* delims=" %%x in ('type 1.txt') do (
set /a linecount+=1
set linea=%%x
if !linecount! geq 6 if !linecount! leq 11 (
set linea=!linea:openldap=%openldap_path%!
) else (
if !linecount!==22 (
set linea=!linea:openldap=%openldap_path%!
)
)
>> temp.txt echo !linea!
)


Report •

Related Solutions

#4
March 25, 2011 at 06:08:56
You seem to be doing the same thing in both cases, I presume you intend to change one later.

22(

There is the syntax error, you need to leave a before the parenthesis.

For "else if" I would suggest using the following format, it makes it easier to read:

if !linecount! geq 6 if !linecount! leq 11 (
    set linea=!linea:openldap=%openldap_path%!
) else if !linecount! equ 22(
    set linea=!linea:openldap=%openldap_path%!
)

The good part is that it can go on like this without heaps of closing parans at the end.


Report •

#5
March 25, 2011 at 06:16:29
Edit: messed up the else ;)

It just occurred to me that the range check makes the logic screwy, try this instead:

set inrange=
if !linecount! geq 6 if !linecount! leq 11 set inrange=true
if defined inrange (
    rem linecount geq 6 and linecount leq 11
    set linea=!linea:openldap=%openldap_path%!
) else (
    rem not in range
    set linea=!linea:openldap=%openldap_path%!
)


Report •

#6
March 25, 2011 at 06:17:49
Sorry, I edited my question before reading your answer. And you're right again, that was the syntax error.
Thank you again for replying in such a positive way!

Report •

#7
March 25, 2011 at 06:23:02
Yep, I saw your edit after I posted, there is still a logic error in what your doing though. The else matches the "leq 11" but won't execute for the "geq 6".

The now edited #5 is a way around it by using a variable.


Report •

#8
March 25, 2011 at 06:26:59
The last one really wiped me out! I worked with batch files when I started with my first PC (4mhz, 512kb RAM, no HD) when I was 12, and have been doing some batch files just now and then, mostly for fun (although now it's for work). But never nearly imagined such power in batch files!
Thanks for broadening my frontiers!

Report •

Ask Question