add counter to variable

January 7, 2010 at 05:21:23
Specs: Windows XP, 3mhz/2 gig
Hi all. I have done this in the past but cant figure it out now. I need to add a counter to a variable. then the batch will work through each line until the '[' character is found, then it will display the line before that. (ie the counter minus 1)
This is what I have so far. (NOT WORKING FOR SOME REASON)
==============================

@echo off & cls
setlocal enabledelayedexpansion
set c=1

for /f "tokens=* delims= " %%a in ('find "e" ^< c:\temp\U.Txt') do (
set line=%%a
set "line!c!"=!line!
cls
echo "!line!c!!"
pause
set parse=!line:~0,1!
call :FUNCTION
)

:FUNCTION
if '!parse!' == '[' (
set /a c = !c! - 1
cls
echo "!line!c!!"
pause
exit
) else (
set /a c = !c! + 1
goto :EOF
)


See More: add counter to variable

Report •


#1
January 7, 2010 at 06:45:51
Instead of this:

set /a z= !z! + 2

try:

set /a z= z + 2


=====================================
Helping others achieve escape felicity

M2


Report •

#2
January 7, 2010 at 08:52:19
I haven't got time to test your script, but just noticed this:

set "line!c!"=!line!

Does that actually work? I've never seen anything like that before, i.e. with the variable name in quotes.


Report •

#3
January 7, 2010 at 12:06:56
Yeah, get rid of the quotes, and i don't think you can do this: !line!c!!. I have always had to use percents, like:
!line%c%! which means you have to go outside the for-loop
and/or the If-block in order to have it right. I would re-org the
code like this:
for /f "tokens=* delims= " %%a in (list) do (
set line=%%a
set line!c!=!line!
set parse=!line:~0,1!
if '!parse!' == '[' (
call :function
goto :eof
)
set /a c = !c! + 1
)
echo no bracket found
goto :eof

:FUNCTION
set /a c = c - 1
echo !line%c%!


Report •

Related Solutions

#4
January 7, 2010 at 15:11:37
By the way, do you really need to create a unique variable for every single line in the file? What if your file is gigabytes in size, then you will run out of main memory.

All you need is two variables: current_line and previous_line, and copy one to the other each time through the loop.


Report •

#5
January 8, 2010 at 00:53:50
Guys, thank you for your comments.
nbrane: Thank you for rewriting the code, it looks like it should work but it doesn't.
Klint, you are right in asking that question and I should really tell you all the file I am working with. (I only need the current line and previous line)
C:\WINDOWS\SchedLgU.Txt
if you scroll down about 70% of the file you will see a line like this "[ ***** Most recent entry is above this line ***** ]"
I need to only display the line above that. This was the only way I could think of doing this.

Thanks again.


Report •

#6
January 8, 2010 at 00:57:30
Thanks to Klint for pointing that out, I now have a working solution:
@echo off & cls
setlocal enabledelayedexpansion

for /f "tokens=* delims= " %%a in ('find "e" ^< c:\temp\U.Txt') do (
set previous=!line!
set line=%%a
set parse=!line:~0,1!
call :FUNCTION
)

:FUNCTION
if '!parse!' == '[' (
cls
echo !previous!
pause
exit
)


Report •

#7
January 8, 2010 at 02:24:11
If you have a copy of GNU's grep utility, then you can give the grep -B switch to print a few lines before the line that matches "[". I don't know if Windows findstr or find utilities have anything similar.

Report •

#8
January 8, 2010 at 03:55:57
Thanks though Klint. It is working now and does exactly what I need it to. I will keep that in mind though.

Ciao


Report •

#9
January 8, 2010 at 12:23:10
If you just need to get the line above a known line:

myfile.txt contains:

mary
suzi
marv
augi

=============================
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=1 delims=[]" %%a in ('find /n "marv" ^< myfile.txt') do (
set /a L=%%a
)

set /a L-=1
set /a N=0

for /f "tokens=* delims= " %%a in (myfile.txt) do (
set /a N+=1
if !N! equ !L! echo %%a
)


=====================================
Helping others achieve escape felicity

M2


Report •

#10
January 8, 2010 at 14:46:48
@M2: Instead of:

set /a N=0
for /f "tokens=* delims= " %%a in (myfile.txt) do (
set /a N+=1
if !N! equ !L! echo %%a
)

why not:

find /n /v "" myfile.txt | find "[%L%]"


Report •


Ask Question