鼠标迷宫批处理文件

Mouse Maze Batch File

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

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%\System32\findstr.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%\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

该代码旨在防止代码在最终用户输入定义的行号之一之前继续执行,(除了 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:\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

…

最后说明,我使用了 %SystemRoot%\Sytem32\,并为 FindStrTimeout 实用程序包含了 .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 tooutput. outputis now defined, so theset` 将被跳过文件的剩余行。

: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.

中空格引起的问题