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?
first batch file:
@ECHO OFF
setlocal
call secondbatchfile.bat xyz
echo. [%val1%]
second batch file:
@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
)
)
this results in:
c:\test>firstbatchfile.bat
123456789 <== this works
[] <== this not
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%]
and in secon file changed:
rem ENDLOCAL & SET %2=!val1!
does not work either?
SOLUTION:
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%]
many thanks to rojo for fantastic code