Batch file 鼠标迷宫批处理文件

Batch file 鼠标迷宫批处理文件,batch-file,nested-loops,subdirectory,Batch File,Nested Loops,Subdirectory,编辑: 具体问题:如何将特定变量设置为文本文件中的特定行? 到目前为止,我得到的是: 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

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

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
arked示例,显示了结构和方法,我认为您打算将其纳入实际的打印任务中:

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\
,并为
FindStr
Timeout
实用程序添加了
.exe
扩展名。这是为了避免依赖脚本来搜索
%Path%
%PATHEXT%
环境变量(其中至少有一个变量经常被用户错误地认为应该修改并错误地修改它们而损坏)

应该适合你[未经测试,但这是我将编写的代码]

“skip=0”
作为
skip
选项无效,因此如果
line
的值为0,则将
skipcmd
设置为空,否则将
skipcmd
设置为文本
skip=3
或任何值

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

因此,在跳过适当数量的行之后,应用定义的测试
为“输出”,并且在*第一次*情况下这是真的,因此将
%%A
的值分配给
输出
输出
,因此将跳过文件其余每一行的
set`

: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
循环也可能会起作用,但在
代码块
(括号内的一系列行)中不起作用,因为
代码块


提示:使用
set“var1=data”
设置值-这可以避免尾随空格引起的问题。在比较中,使用
如果“thing1”==“thing2”…
避免由
thing1/2中的空格引起的问题打开命令提示窗口,从那里可以读取每个命令的使用信息。例如,
set/?
将向您展示如何提示用户输入,以及如何使用数据值定义变量
对于/?
,将解释如何创建for循环等。是的,我知道如何使用cmd窗口。。。Thankso,如果正确使用for循环和set命令,这不是你的问题,你到底在问什么?这是一个根据您提供的代码,专门帮助您解决可复制问题的网站。由于您没有提供任何代码,而且您显然知道如何使用cmd窗口,您希望我们帮助您解决的具体问题是什么?就目前的情况而言,在您的命令没有问题的情况下,它看起来更像是请帮助我构建和规划执行整个任务的方法。这不是这个网站所做的,所以请澄清你的问题。这就是为什么我不尝试插入代码,即使我按照说明,它也不会打印right@EliWhite关于格式化代码:可以在每行前面加上四个空格,或者在块周围加上由三个反勾组成的行(不是撇号!)。非常感谢,我完全可以使用它,并将其更改为我需要的功能。如果我想根据文本字符串测试行内容,我可以使用;为(%keystring%)中的%%H设置“choiceNum[%%G]=%%H”&回显%%G:%%嗨,我不确定你到底在问什么。我最初的直觉是,如果您只想打印包含特定字符串的行,那么应该在代码的前面过滤行(在
For/F
/
findstr
循环中)。请你解释得更清楚一点好吗。(旁注:我对上面的代码做了一个更改,当我刚刚阅读时,请使用更新的版本。)
: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