setlocal enabledelayedexpansion
set /a x=1
set /a lines=91
:while1
if %x% leq %lines% (
for /f "tokens=*" %%a in ('findstr /n .* "Y:\Test\foldernametest.txt"') do (
for /f "tokens=1* delims=:" %%b in ("%%a") do (
if "%%b" equ "%x%" (
echo(%%c
echo title=%%c > Lable%x%.dat
set /a x= x+1
goto while1
)
)
)
endlocal
I'm reasonably sure this will work, as would
setlocal enabledelayedexpansion
set /a x=1
set /a lines=91
:while1
if %x% leq %lines% (
for /f "tokens=1* delims=:" %%a in ('findstr /n .* "Y:\Test\foldernametest.txt"') do if %%a==%x% (
echo(%%b
echo title=%%b > Lable%x%.dat
set /a x= x+1
goto while1
)
)
endlocal
The issue with your code is that endlocal
terminates a setlocal
and all of the environment changes that have taken place since the setlocal
are backed out - the environment is restored to what it was when the setlocal
was executed.
The consequence is that with your code, you are incrementing x
(a grand name for a variable) and then the increment is backed out when the endlocal
is executed.
So - put the entire routine in a setlocal/endlocal
bracket. This has other advanteages - like if you execute a setlocal
immediately after @echo off
, then when the routine terminates, the environment is returned to its original state - it does not accumulate changes (normally additions of variables) as more and more batches are run.
Some of the other changes I've made are cosmetic. the quotes in a set /a
are superfluous and so is the colon in a goto
(with the sole exception of goto :eof
)
Another problem you have was %1
(meaning "the first parameter to the routine") where you probably meant "%x%".
In the first code fragment, the output of the findstr
is assigned to %%a
and the inner for
assigns that part of the findstr
before the delimiter to %%b
and that after to %%c
. You evidently want to pick the line %%b
equal to %x%
so the code makes the comparison and if equal, outputs %%c
(rest of line) and title=%%c
to the file made from Lable
and the line number. (You've spelled label
incorrectly); then increments x
and tries again.
The second piece of code is a simplification of the first. The line is read from the file and numbered, then split directly on the colon; %%a
gets the number, %%b
the rest of the line, so if %%a
is the same as the number %x%
then we want to do something (no quotes required, since %%a
is a simple numeric string and x
will also be numeric because it's never assigned to a string containing separators or empty).
The thing-to-be-done is to echo the line from the file (in %%b
, bump the line number and start again...