Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 批处理文件-值中的循环递增计数未正确显示_Loops_Batch File_Syntax_Increment - Fatal编程技术网

Loops 批处理文件-值中的循环递增计数未正确显示

Loops 批处理文件-值中的循环递增计数未正确显示,loops,batch-file,syntax,increment,Loops,Batch File,Syntax,Increment,我试图读取一个文件并将数据行输出到注册表项中。数据收集工作正常,但我不理解在最后一个循环中增加字符串值所需的语法 @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:*:=!" Thi

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

@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

在批处理文件中搜索错误时,在第一行中使用
@echo On
或删除
@echo off
或使用
rem
注释此行,以查看真正执行的
cmd.exe

命令行解释器在
set VariableName=%line\的行上失败!计数器!%
但是,在第二个循环中,可以更轻松地直接实现您想要实现的目标,如下例所示:

@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

此解决方案比您尝试过的要简单得多,而且可能会更加简化。

请显示示例数据,可能会出现在
数组中,更重要的是,您希望从该数据派生的值为
valuename
type
data
。你说的“继续上涨1”到底是什么意思?非常感谢Mofi!很好用!天才:)
@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