Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
sincos2007
- Posts: 44
- Joined: 05 Apr 2019 05:52
#1
Post
by sincos2007 » 30 Apr 2019 14:26
In my situation, command string in function used to make return value from function is necessary.
Code: Select all
:test2
setlocal EnableDelayedExpansion
set "_ret="
for /f "delims=" %%i in (temp.txt) do (
setlocal DisableDelayedExpansion
rem echo %%i
set "_ret=set %~1=%%i"
endlocal
)
(
endlocal
%_ret%
)
goto :eof
Code: Select all
set "var1="
call :test2 var1
echo %var1%
content of temp.txt, only one line in my case
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#2
Post
by aGerman » 30 Apr 2019 15:28
1) You still do it the wrong way. You mess up EnableDelayedExpansion and DisableDelayedExpansion
2) Please provide an exeample that shows why you even need to toggle these subenvironments.
If the only reason for your subroutine is that you want to read the first line of a file then you don't even need it.
Code: Select all
set "var1=" & <"temp.txt" set /p "var1="
Steffen
-
sincos2007
- Posts: 44
- Joined: 05 Apr 2019 05:52
#3
Post
by sincos2007 » 01 May 2019 04:34
Hi Steffen,
Look following code:
Code: Select all
:test6
setlocal EnableDelayedExpansion
set "lines_cnt=2"
set "in_file=temp.txt"
set "line="
set /a "n=0"
<"!in_file!" (
for /l %%j in (1 1 !lines_cnt!) do (
set "line=" &set /p "line="
if defined _ret (
set "_ret=!_ret!^&"
)
set "_ret=!_ret!set %~1[!n!]=!line!
set /a "n+=1"
)
)
rem debug
echo debug 1
echo !_ret!
(
endlocal
%_ret%
)
goto :eof
Code: Select all
set "var1="
call :test6 var1
echo %var1[0]%
echo %var1[1]%
content of temp.txt
When I run this batch, I get error information.
If I change content of temp.txt to
It runs well.
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#4
Post
by aGerman » 01 May 2019 06:23
No need to toggle delayed expansion here.
Code: Select all
@echo off
setlocal DisableDelayedExpansion
call :test6 foo
set "line_count=%errorlevel%"
setlocal EnableDelayedExpansion
for /l %%i in (1 1 %line_count%) do echo(!foo[%%i]!
endlocal
pause
goto :eof
:test6
set "in_file=temp.txt"
set "n=0"
<"%in_file%" (
for /f %%i in ('type "%in_file%"^|find /c /v ""') do set "n=%%i" &for /l %%j in (1 1 %%i) do (
set "%~1[%%j]=" &set /p "%~1[%%j]="
)
)
exit /b %n%
temp.txt
As a side note: Defining hundreds of variables for all of the lines is most of the time unnecessary and an indicator for a fundamental design problem in your code.
Steffen
-
sincos2007
- Posts: 44
- Joined: 05 Apr 2019 05:52
#5
Post
by sincos2007 » 01 May 2019 09:27
Hi Steffen,
In your code, if add “setlocal EnableDelayedExpansion” to beginning of function test6 and “endlocal” at end of test6, how could you make it work?
Thanks
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#6
Post
by aGerman » 01 May 2019 10:34
sincos2007 wrote: ↑01 May 2019 09:27
if add “setlocal EnableDelayedExpansion” to beginning of function test6 and “endlocal” at end of test6
There is NO REASON to do that and it wouldn't work anymore.
Steffen