鼠标迷宫批处理文件



编辑:具体问题:如何将特定变量设置为文本文件中的特定行?到目前为止,我所拥有的是:

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

因此,如果我在文本文档testCode.txt中有三行内容如下:

Red
Green
Blue

我想让它们像这样打印到我的批处理文件中:

1: Red
2: Green
3: Blue

到目前为止,它是这样打印的:

delims=" was unexpected at this time.
1: 
2: Blue
3: Blue

在我看来,你实际上想要做的不仅仅是打印每一行源文件,并以其行号为前缀,否则你只需要:

%SystemRoot%System32findstr.exe /N "^" "testCode.txt"

哪个会输出:

1:Red
2:Green
3:Blue

因此,下面是一个rem标记的示例,显示了结构和方法,我认为您打算将其纳入您的实际任务中进行打印:

1: Red
2: Green
3: Blue

然后要求最终用户从该列表中进行选择,只需输入行号,并定义一个变量,以便在脚本中进一步使用,其中包含该行的内容:

@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%System32findstr.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%System32findstr.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%System32timeout.exe /T 5 /NoBreak 1>NUL
Exit /B

该代码旨在防止代码继续运行,直到最终用户输入一个定义的行号(CTRL+C除外)

您会注意到,5行的源文件位置使用%~dp0来引用包含正在运行的批处理文件的目录的路径。如果源文件不在那里,请使用它的绝对路径。

或者,您应该从一开始就在批处理文件中定义当前目录,然后可以选择在整个过程中使用相对路径:

示例:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Rem Define the current working directory, and exit if it does not exist.
CD /D "W:orkingdirectory" 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
…

最后要注意的是,对于FindStrTimeout实用程序,我使用了%SystemRoot%Sytem32,并包含了.exe扩展。这是为了防止依赖于必须搜索%Path%%PATHEXT%环境变量的脚本(至少其中一个环境变量经常被用户错误地认为应该修改它们而损坏)

:a
if %line%==0 (SET "skipcmd=") else (set :skipcmd=skip=%line%")
set "output="
for /f "skip=%skipcmd% delims=" %%A in (testCode.txt) do if not defined output set "output=%%A"

应该对你有效[未经测试,但我会这么编码]

"skip=0"作为skip选项无效,因此如果line的值为0,则将skipcmd设置为空,否则将skipcmd设置为文字skip=3或其他任何值。

output设置为nothing将意味着output未定义。

因此,在跳过适当数量的行之后,对于文件的剩余行中的每一行,将跳过测试is "output" defined is applied, and on the *first* occasion this is true, so the value of%%Ais assigned to输出.输出is now defined, so the集合"。

:a
if %line%==0 (SET "skipcmd=") else (set :skipcmd=skip=%line%")
for /f "skip=%skipcmd% delims=" %%A in (testCode.txt) do set "output=%%A"&goto outputset
:outputset

也可能起作用,当遇到第一个选定的行时退出for循环,但在code block(带括号的一系列行)中不起作用,因为在code-block中不允许使用标签

提示:使用set "var1=data"设置值-这样可以避免尾部空格引起的问题。在比较中,使用if "thing1" == "thing2" ...可以避免thing1/2中的空格引起的问题。

最新更新