所以情况是这样的...我有两个嵌套的if语句,然后在它们内部循环(使用GoTo命令和递增的变量 - 用于循环模拟:D)。您可能知道将新值分配给括号内的变量(if语句),您必须使用delayedexpansion。另外要在for命令中使用变量,你必须加倍百分比标记,如%%。我想将for / f命令中的标记设置为我想要的变量的值。问题是加倍感叹号没有效果。我也试过各种各样的...比如使用引号,转义那些引号,使用引用替代品,但这一切都无济于事。如果你能以任何方式提供帮助,那将是非常好的,因为我根本无法想到任何事情:(。提前谢谢你们!
如果这没有意义,那么代码就是:
@echo off
set FilePath=test.bat
set RefreshRate=3
setlocal enabledelayedexpansion enableextensions
:GetData
if defined FilePath (
if exist "%FilePath%" (
:GetLines
cls
:: This is how I find out how many lines there is in the file
set "cmd=findstr /R /N "^^" "%FilePath%" | find /C ":""
for /f %%a in ('!cmd!') do set Lines=%%a
:ShowCode
cls
set LineNum+=1
if ""!LineNum!"" GTR ""!Lines!"" GoTo Refresh
::THIS IS THE MAIN PROBLEM
for /f "tokens=%%LineNum%% delims=$" %%b in ("%FilePath%") do (
set Line%LineNum%=%%b
echo !LineNum!. | !Line%LineNum%!
GoTo ShowCode
)
)
)
:Refresh
ping localhost -n %RefreshRate% >nul
GoTo GetData
对不起,我没有足够的时间让它更具可读性,但它应该让整个事情变得更加清晰。
答案 0 :(得分:0)
令牌属性不会放在引号中的括号中,无论如何都是错误的。它是第n个分隔词的第n个。
类型
for /?
示例
答案 1 :(得分:0)
首先:不要在:: remark
括号中的代码块中同时使用:label
条评论和()
。您可以在以下提供的脚本输出中编码的labels.bat
脚本中找到有害的证据; an explanation here: Comments within bracketed code blocks
在下一个脚本中,特定纯文本文件(参见set "FilePath=labels.bat"
)的非空行被保存到伪数组LineAAA
,其中 index {{ 1}} =行号。根据你的问题,我不知道这是不是主题,但可以给出一些有用的线索......
AAA
<强>输出强>:
@echo off
setlocal enabledelayedexpansion enableextensions
cls
set line
set "FilePath=labels.bat"
set /A "RefreshRate=3"
:GetData
if not defined FilePath (
echo %%FilePath%% not defined
goto :eof
)
if not exist "%FilePath%" (
echo %FilePath% does not exist
goto :eof
rem following 'else' (sub)statement seems to be superabundant
) else (
rem GetLines
rem This is how I find out how many lines there is in the file
set "cmd=findstr /R /N "^^" "%FilePath%" | find /C ":""
for /f %%a in ('!cmd!') do set /A "Lines=%%a"
set /A "LineNum=0"
set "Line000=@rem %FilePath%"
for /f "tokens=*" %%b in (%FilePath%) do (
set /A "LineNum+=1"
set "LineX=000000000!LineNum!"
set "LineX=!LineX:~-3!"
set "Line!LineX!=%%b"
)
call :Refresh
)
set line
rem pause
endlocal
goto :eof
:Refresh
ping localhost -n %RefreshRate% | findstr /I "Packets: statistics"
rem >nul
GoTo :eof
labels.bat输出:
Environment variable line not defined
Ping statistics for ::1:
Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Line000=@rem labels.bat
Line001=@SETLOCAL enableextensions enabledelayedexpansion
Line002=@ECHO ON >NUL
Line003=if ""=="" (
Line004=rem comment
Line005=@echo rem comment
Line006=)
Line007=if ""=="" (
Line008=:: comment
Line009=@echo :: comment
Line010=)
Line011=if ""=="" (
Line012=:label
Line013=@echo :label
Line014=)
Line015=@ENDLOCAL
Line016=@goto :eof
LineNum=16
Lines=16
LineX=016