本文介绍了将var返回到调用批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我启动一个批处理文件来处理一些代码,一次调用另一个批处理文件,并且应该返回一个变量,但是关于本地/ endlocal的手册页中解释的方法似乎没有在这里工作,我做错了什么?

i start a batch file to process some code, at a time it calls another batchfile and should get returned a variable, but the methods explained in man pages about local/endlocal seem not to work here, what am i doin wrong please?

第一批文件:

@ECHO OFF
setlocal
call secondbatchfile.bat xyz
echo. [%val1%]

第二批文件:

@if (@a==@b) @end /*                        <== btw what does this code do ???
@echo off
setlocal enabledelayedexpansion
set "URL=%~1"
set bla bla ...
do bla bla ...

for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%URL%"') do (

rem trim whitespace from beginning and end of line
for /f "tokens=*" %%x in ("%%~I") do set "line=%%x"

rem test that trimmed line matches "variable=number"
echo !line! | findstr /i "^to[a-z]*=[0-9]*" >NUL && (

rem test was successful.  Scrape number.
for /f "tokens=2 delims==" %%x in ("%%I") do set "val1=%%x"

echo !val1!                            <== this works
ENDLOCAL & SET top=%val1%              <== this not

)
)

这样做:

c:\test>firstbatchfile.bat
123456789                                   <== this works
 []                                         <== this not

我尝试了不同的return var的语法,如!val1! %% val1 - 没有工作。我缺少什么?

i tried different syntax of return var like !val1! or %%val1 - none worked. what am i missing?

更新:
我试过:

UPDATE:regarding to other example here on site i tried:

call seconbatchfile.bat xyz ret1
echo. [%2%] [%ret1%]

并在secon文件中更改:

and in secon file changed:

rem ENDLOCAL & SET %2=!val1!

不工作?

解决方案:

第二批文件可以从rojo读出整个网站的起始脚本,我没有删除修剪和匹配语法行,只返回相关匹配:

second batch file can be origin script from rojo reading out the whole website, i did leave the trim and match syntax lines to only return relevant matches:

for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%URL%"') do (

rem trim whitespace from beginning and end of line
for /f "tokens=*" %%x in ("%%~I") do set "line=%%x"

rem test that trimmed line matches "variable=number"
echo !line! | findstr /i "^[a-z]*=[0-9]*" >NUL && (

echo(%%I

)
)

,第一个批处理文件调用它将搜索两个所需的参数,如下所示:

and the first batch file calling it will do the search for two needed parameters like this:

@ECHO OFF
setlocal
for /f "delims=" %%I in ('secondbatchfile.bat "http://xyz"') do (

echo %%I | findstr /i "top" >NUL && (

for /f "tokens=2 delims==" %%x in ("%%I") do (
set "updir=%%x"
)
)
echo %%I | findstr /i "low" >NUL && (

for /f "tokens=2 delims==" %%x in ("%%I") do (
set "lowdir=%%x"
)
)
)

echo.[%updir%]
echo.[%lowdir%]

非常感谢rojo的梦幻般的代码

many thanks to rojo for fantastic code

推荐答案

最简单的解决方案是要让您的第二个批处理文件回显其结果,并在第一批文件中使用循环捕获它。取消您的 setlocal 将环境变量传递回调用脚本是一个凌乱的事情。

The easiest solution is to have your second batch file echo its result, and capture it using a for loop in the first batch file. Cancelling your setlocal to pass an environment variable back to the calling script is a messy affair.

first.bat:

first.bat:

@echo off
setlocal

for %%x in (
    "http://10.0.0.1/foo/vars.txt"
    "http://10.0.0.1/bar/vars.txt"
    "http://10.0.0.1/baz/vars.txt"
    "http://10.0.0.1/qux/vars.txt"
    "http://10.0.0.1/corge/vars.txt"
) do (
    for /f "delims=" %%I in ('fetchvalue.bat "%%~x"') do (
        set "val1=%%I"
    )
    echo.[%val1%]
)

fetchvalue.bat:

fetchvalue.bat:

@if (@a==@b) @end /*

:: fetchvalue.bat <url>
:: output the "value" part of variable=value from a text file served by http

@echo off
setlocal
if "%~1"=="" goto usage
echo "%~1" | findstr /i "https*://" >NUL || goto usage

set "URL=%~1"

for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%URL%"') do (

    rem trim whitespace from beginning and end of line
    for /f "tokens=*" %%x in ("%%~I") do set "line=%%x"

    rem test that trimmed line matches "variable=number"
    echo !line! | findstr /i "^to[a-z]*=[0-9]*" >NUL && (

        rem test was successful.  Scrape number.
        for /f "tokens=2 delims==" %%x in ("%%I") do echo(%%x
    )
)

goto :EOF

:usage
echo Usage: %~nx0 URL
echo     for example: %~nx0 http://www.google.com/
echo;
echo The URL must be fully qualified, including the http:// or https://
goto :EOF

JScript */
var x=new ActiveXObject("Microsoft.XMLHTTP");
x.open("GET",WSH.Arguments(0),true);
x.setRequestHeader('User-Agent','XMLHTTP/1.0');
x.send('');
while (x.readyState!=4) {WSH.Sleep(50)};
WSH.Echo(x.responseText);






这是一个例子first.bat,将排序获取的值,将 low 设置为最低值,并将设置为最高。


Here's an example first.bat that will sort the fetched values, set low to the lowest value, and set high to the highest.

@echo off
setlocal enabledelayedexpansion

for %%x in (
    "http://10.0.0.1/foo/vars.txt"
    "http://10.0.0.1/bar/vars.txt"
    "http://10.0.0.1/baz/vars.txt"
    "http://10.0.0.1/qux/vars.txt"
    "http://10.0.0.1/corge/vars.txt"
) do (
    set low=
    for /f "delims=" %%I in ('fetchvalue.bat "%%~x" ^| sort') do (
        if not defined low set "low=%%I"
        set "high=%%I"
    )
    echo low: !low!
    echo high: !high!
)

这篇关于将var返回到调用批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:31