【问题标题】:Batch File - loop incrementing count in value not displaying correctly批处理文件 - 循环递增计数值未正确显示
【发布时间】:2014-07-24 14:25:20
【问题描述】:

我正在尝试读取文件并将数据行输出到注册表项中。数据收集工作,但我不明白在最后一个循环中增加字符串值所需的语法。

@echo OFF


SETLOCAL DisableDelayedExpansion
FOR /F "usebackq skip=1 delims=" %%a in (`"findstr /n ^^ C:\GetSID.txt"`) do (
set "var=%%a"
SETLOCAL EnableDelayedExpansion
set "var=!var:*:=!" This removes the prefix
echo(!var:~76,63!>>C:\SIDoutput.txt
goto :EndLoop
)
:EndLoop
set /p SID= <C:\users\paintic\SIDoutput.txt

set KEY_NAME="HKEY_USERS\!SID!\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"

set Counter=1
for /f %%x in (C:\users\paintic\Networkprinters.txt) do (
  set "Line_!Counter!=%%x"
  set /a Counter+=1
  if !Counter!==3 (Echo %line_counter%)
)

set /a counter2=!counter!-3

set counter=1

下面的部分是我无法工作的部分。我正在尝试从前一个循环中写入 LINE_1、LINE_2 和 LINE_3 值,以通过下面的循环递增。所以 VALUENAME 应该等于 LINE_1,TYPE 应该 = LINE_2 的值,DATA 应该在第一次运行时 = LINE_3 并继续上升 1 直到循环结束(文件读取结束)

`for /L %%i in (1,1,%counter2%) do (

   set ValueName=%Line_!counter!%
   set /a counter+=1
   set Type=%Line_!counter!%
   set /a Counter+=1
   set Data=%Line_!counter!%
   set /a Counter+=1
   echo !ValueName!
   echo !Type!
   echo !Data!  

REG ADD %KEY_NAME% /v !ValueName! /t !Type! /d !Data! /f

)

ENDLOCAL 

Pause`

【问题讨论】:

  • 请显示示例数据,可能会出现在LINE_ 数组中,更重要的是,您希望从该数据中获得的值是valuenametypedata .你说的“继续上升 1”到底是什么意思?

标签: loops batch-file syntax increment


【解决方案1】:

在批处理文件中搜索错误时,在第一行使用@echo on 或删除@echo off 或在此行注释rem 以查看cmd.exe 真正执行的内容总是很有帮助。

命令行解释器在带有set VariableName=%Line_!counter!% 的行上失败,因为解释器不知道首先要扩展什么。我认为不可能动态地创建环境变量的名称并接下来引用该环境变量的值。这种方法很可能永远行不通。

但是,您想要实现的目标可以直接在第二个循环中更轻松地完成,如下例所示:

@echo off
setlocal EnableDelayedExpansion

rem Create data for demo example.
set "KEY_NAME=HKEY_USERS\S-1-5-20\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"
echo TestValue>"%TEMP%\Networkprinters.txt"
echo REG_SZ>>"%TEMP%\Networkprinters.txt"
echo Sample Data>>"%TEMP%\Networkprinters.txt"
echo AnotherValue>>"%TEMP%\Networkprinters.txt"
echo REG_DWORD>>"%TEMP%\Networkprinters.txt"
echo ^1>>"%TEMP%\Networkprinters.txt"

rem Now the loop follows which reads the data from the file line
rem by line and build the line for using command "reg.exe" to
rem add the data to registry of the user with the defined SID.
set Counter=1
for /f "usebackq delims=" %%x in ("%TEMP%\Networkprinters.txt") do (
   if "!Counter!"=="1" (
      set "ValueName=%%x"
   ) else if "!Counter!"=="2" (
      set "ValueType=%%x"
   ) else (
      set "ValueData=%%x"
      rem Echo the command instead of really executing "reg.exe".
      echo reg.exe ADD %KEY_NAME% /v "!ValueName!" /t !ValueType! /d "!ValueData!" /f
      set Counter=0
   )
   set /a Counter+=1
)

rem Delete the text file created for demo example.
del "%TEMP%\Networkprinters.txt"
endlocal

此解决方案比您尝试过的要容易得多,并且可能更加简化。

【讨论】:

  • 非常感谢莫菲!完美运行!天才:)
猜你喜欢
  • 2013-01-24
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2015-08-05
  • 1970-01-01
  • 2022-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多