Windows批处理转到FOR循环内部

时间:2014-07-03 11:35:12

标签: windows batch-file for-loop goto

我有一个Windows批处理文件来读取文件,从中获取一些信息,比如机器IP,然后为该机器打印一些注册表值,如下所示:

for /f "skip=1 tokens=1,2,3,4,5,6,* delims=;" %%a in ('findstr /R "^" z:\csp\levantamento\levantamentoNOVO.csv') do (

set /A "HOME=0"

:cicle
echo HOME%HOME%
echo \\%%c\HKLM\SOFTWARE\ORACLE\HOME%HOME%

reg query \\%%c\HKLM\SOFTWARE\ORACLE\HOME%HOME% /v ORACLE_HOME

if %HOME% leq 3 (
    set /A "HOME+=1"
    goto cicle
) else (
    echo.
)

echo Conenction to %%a deleted
)

我的输出是这样的:

HOME0
\\10.13.3.27\HKLM\SOFTWARE\ORACLE\HOME0

HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME0
    ORACLE_HOME    REG_EXPAND_SZ    d:\oracle\806

HOME1
\\%c\HKLM\SOFTWARE\ORACLE\HOME1
ERROR: The network path was not found.
HOME2
\\%c\HKLM\SOFTWARE\ORACLE\HOME2
ERROR: The network path was not found.
HOME3
\\%c\HKLM\SOFTWARE\ORACLE\HOME3
ERROR: The network path was not found.
HOME4
\\%c\HKLM\SOFTWARE\ORACLE\HOME4
ERROR: The network path was not found.


Conenction to %a deleted

当我执行 goto cicle 时,似乎FOR循环不再知道%% c和%% c的帽子。我真的不知道自己做错了什么。

1 个答案:

答案 0 :(得分:4)

尝试

for /f "skip=1 tokens=1,2,3,4,5,6,* delims=;" %%a in ('findstr /R "^" z:\csp\levantamento\levantamentoNOVO.csv') do (

 set /A HOME=0
 call :cicle "%%a" "%%c"
)

:: what-to-do-after-for goes in here

goto :eof

:cicle
echo HOME%HOME%
echo \\%~2\HKLM\SOFTWARE\ORACLE\HOME%HOME%

reg query \\%~2\HKLM\SOFTWARE\ORACLE\HOME%HOME% /v ORACLE_HOME

if %HOME% leq 3 (
    set /A HOME+=1
    goto cicle
)
echo.

echo Connection to %~1 deleted
goto :eof

请注意,现在有一个名为cicle的子例程,它在"%%a"例程中提供参数"%%c"cicle%%a变为{ {1}}(第一个参数,减去引号)和%~1成为%%c(第二个参数,减去引号)。

仅在传递的参数包含分隔符时才会出现引号,这在当前情况下不太可能。

从docco的提示中查看%~2

另请注意,call /?用于字符串分配,以确保分配的值中不包含不需要的尾随空格。 set "var=variable"语法没有遇到这个问题,所以兔子的耳朵不是必需的。