Batch file 如何在for命令-Win批处理文件中将变量(其值)用作标记

Batch file 如何在for命令-Win批处理文件中将变量(其值)用作标记,batch-file,token,Batch File,Token,所以情况是这样的。。。我有两个嵌套的if语句,然后在它们内部有一个循环(使用GoTo命令和一个递增变量-for-loop-simulation:D)。正如您可能知道的,要将新值分配给(if语句)括号内的变量,您必须使用delayedexpansion。此外,要在for命令中使用变量,您必须将百分比标记加倍,如so%%。我想将for/f命令中的标记设置为我想要的变量值。问题是把感叹号加倍没有效果。我也试过各种方法。。。就像使用引号,转义那些引号,使用引号替代品,但这一切都是徒劳的。如果你能以任何方

所以情况是这样的。。。我有两个嵌套的if语句,然后在它们内部有一个循环(使用GoTo命令和一个递增变量-for-loop-simulation:D)。正如您可能知道的,要将新值分配给(if语句)括号内的变量,您必须使用delayedexpansion。此外,要在for命令中使用变量,您必须将百分比标记加倍,如so%%。我想将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

很抱歉,我没有足够的时间让它更具可读性,但它应该让整个事情更清楚一些。

令牌属性不在括号中,它们在引号中,无论如何都是错误的。它是第n到第n个分隔的术语

类型


例如

首先:不要在
()
括号内的代码块中使用
::备注
注释或
:标签
。可以在后面提供的脚本输出中编码的
标签.bat
脚本中找到有害性证明

在下一个脚本中,特定纯文本文件的非空行(参见
set“FilePath=labels.bat”
)保存到伪数组
LineAAA
,其中index
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
输出

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
d:\bat>labels.bat

d:\bat>if "" == "" (
rem comment

)
rem comment

d:\bat>if "" == "" (@echo :: comment )
'@echo' is not recognized as an internal or external command,
operable program or batch file.

d:\bat>if "" == "" (@echo :label )
'@echo' is not recognized as an internal or external command,
operable program or batch file.

d:\bat>
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
d:\bat>labels.bat

d:\bat>if "" == "" (
rem comment

)
rem comment

d:\bat>if "" == "" (@echo :: comment )
'@echo' is not recognized as an internal or external command,
operable program or batch file.

d:\bat>if "" == "" (@echo :label )
'@echo' is not recognized as an internal or external command,
operable program or batch file.

d:\bat>

哦,这是一个输入错误:D,标记只能是一个标记-就像在第n个标记中一样,就像这里的一个示例:…无论如何,谢谢。哇!非常感谢。对我来说,它是否在主题上并不重要。这仍然是一个更容易的选择。我没有想到使用星号通配符作为标记,而不是整个循环和v可变程序。我明天早上试试……这是漫长的一天:D