Edit: Specific question: How do I set a specific variable to a specific line in a text file? So far what I have is this:
set line=0
setlocal EnableDelayedExpansion
set "cmd=findstr /r /n "^^" testCode.txt | find /C ":""
for /f %%A in ('!cmd!') do set subsets=%%A
endlocal
:a
for /f "skip=%line% delims=" %%A in (testCode.txt) do set output=%%A
set /a line+=1
echo %Line%: %output%
if %line%==%subsets% goto :b
goto:a
:b
echo.
pause
So if I've got three lines in my text document, testCode.txt, that read as such:
Red
Green
Blue
I want them to print into my batch file like so:
1: Red
2: Green
3: Blue
so far, it's printing like this:
delims=" was unexpected at this time.
1:
2: Blue
3: Blue
It appears to me as if you actually intend to do more than just print each of your source file lines, prepended with its line number, otherwise all you'd need was:
%SystemRoot%\System32\findstr.exe /N "^" "testCode.txt"
Which would output:
1:Red
2:Green
3:Blue
What follows is therefore a rem
arked example showing the structure and methodology, I think you intend to incorporate into your actual task to print:
1: Red
2: Green
3: Blue
Then ask the end user to make a selection from that list, by entering the line number only, and define a variable, for further use within the script, containing the content from that line:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Rem Define a variable for the intended source file.
Set "SourceFile=%~dp0testCode.txt"
Rem Exit script if the source file does not exist.
If Not Exist "%SourceFile%" Exit /B
Rem Undefine any existing variables with names beginning Line[
For /F "Delims==" %%G In ('Set Line[ 2^>NUL') Do Set "%%G="
Rem Define individual variables Line[n], where n is their line number,
Rem and print the line number followed by a colon, space and line content.
For /F "Tokens=1,* Delims=:" %%G In ('%SystemRoot%\System32\findstr.exe /N "^"
"testCode.txt"') Do Set "Line[%%G]=%%H" & Echo %%G: %%H
Rem Exit the script if no line existed in the source file.
If Not Defined Line[1] Exit /B
Rem Assumption that the end user will need to select an entry via menu option.
:Selection
Echo(
Set "UserSelection="
Set /P "UserSelection=Enter the number for your selection>"
Set "UserSelection=%UserSelection:"=%"
(Set Line[%UserSelection%]) 2>NUL | %SystemRoot%\System32\findstr.exe /B /L^
"Line[%UserSelection%]=" 1>NUL || GoTo Selection
Rem Re-define the user selection variable to the line content.
SetLocal EnableDelayedExpansion
For %%G In ("!Line[%UserSelection%]!") Do EndLocal & Set "UserSelection=%%~G"
Rem Print the chosen line content.
Echo(
Echo You Selected %UserSelection%
Rem For demonstration purposes only, delay the script before exiting.
%SystemRoot%\System32\timeout.exe /T 5 /NoBreak 1>NUL
Exit /B
The code has been designed to prevent the code from continuing until the end user has entered one of the defined line numbers, (except CTRL+C).
You'll note that the source file location, on line 5
uses %~dp0
to reference the path to the directory holding your running batch file. If you source file is not located there, please use its absolute path instead.
Alternatively, you should define the current directory in your batch file from the outset, and could then choose to use relative paths throughout:
Example:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Rem Define the current working directory, and exit if it does not exist.
CD /D "W:\orking\directory" 2>NUL || Exit /B
Rem Define a variable for the intended source file.
Set "SourceFile=testCode.txt"
Rem Exit script if the source file does not exist.
If Not Exist "%SourceFile%" Exit /B
…
Final note, I have used %SystemRoot%\Sytem32\
, and included the .exe
extension, for both the FindStr
and Timeout
utilities. This is deliberate to prevent reliance on the script having to search the %Path%
, and %PATHEXT%
environment variables, (one of which at least, is often corrupted by users ill advisedly thinking that they should modify them, and doing so incorrectly).