简体   繁体   English

Windows 批处理转到 FOR 循环内

[英]Windows batch goto inside FOR loop

I have a Windows batch file to read a file, get some info from it, like machine IP, and then print some registry values for that machine, something like this:我有一个 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
)

and my output is this:我的输出是这样的:

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

When I do a goto cicle , it appear that the FOR loop doesn't know anymore hat the %%c and %%c are.当我执行goto cicle 时,似乎 FOR 循环不再知道 %%c 和 %%c 是什么。 I really don't know what I'm doing wrong.我真的不知道我做错了什么。

Try尝试

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

Note that now there is a subroutine called cicle which is supplied with parameters "%%a" and "%%c" Within the cicle routine, what was %%a becomes %~1 (the first parameter, minus the quotes) and what was %%c becomes %~2 (the second parameter, minus the quotes).请注意,现在有一个名为cicle的子例程,它提供参数"%%a""%%c"cicle例程中, %%a变为%~1 (第一个参数,减去引号)以及什么是%%c变成%~2 (第二个参数,减去引号)。

The quotes are only there in case the parameter being passed contains separator characters, which is unlikely in your current case.引号仅在传递的参数包含分隔符的情况下才存在,这在您当前的情况下不太可能。

see call /?call /? from the prompt for docco.从 docco 的提示。

Note also that set "var=variable" is used in string assignments to ensure unwanted trailing spaces are not included in the value assigned.另请注意,在字符串分配中使用set "var=variable"以确保分配的值中不包含不需要的尾随空格。 The set /a syntax does not suffer from this problem, so the rabbit's ears are not necessary. set /a语法没有这个问题,所以兔子的耳朵是没有必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM