Page 1 of 1

how to read line in text file and return it by function

Posted: 30 Apr 2019 14:26
by sincos2007
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

Code: Select all

) el

Re: how to read line in text file and return it by function

Posted: 30 Apr 2019 15:28
by aGerman
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

Re: how to read line in text file and return it by function

Posted: 01 May 2019 04:34
by sincos2007
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

Code: Select all

) el
kkk ww
When I run this batch, I get error information.

If I change content of temp.txt to

Code: Select all

el
kkk ww
It runs well.

Re: how to read line in text file and return it by function

Posted: 01 May 2019 06:23
by aGerman
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

Code: Select all

) el
kkk ww

! % < > | & "
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

Re: how to read line in text file and return it by function

Posted: 01 May 2019 09:27
by sincos2007
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

Re: how to read line in text file and return it by function

Posted: 01 May 2019 10:34
by aGerman
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