Let me start off by saying I am very new to this, and what little code I have cobbled together I found on this site.
In the end I need a batch that when ran will grab each folder name in a parent dir. and copy it to a text file named label1, label2, ect.
I started with pulling the lines from a directory list in a text file. I can get it to echo the last line to a file using Seth's code from this post Windows Batch file to echo a specific line number
I made some modifications to try to put it in a loop and now I get nothing out.
If anyone can help me it would be much appreciated. Here is my code so far.
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 (
set "FullLine=%%a"
for /f "tokens=1* delims=:" %%b in ("%%a") do (
setlocal enabledelayedexpansion
set "LineData=!FullLine:*:=!"
if "%%b" equ "%1" echo(!LineData!
echo title=!linedata! > Lable%x%.dat
set /a "x= x+1"
endlocal
goto :while1
)
)
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...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.