1.

Solve : CALLed Function not returning values?

Answer»

I have a FOR LOOP which calls a FUNCTION with passed parameters.  The parameters set inside the function is not being returned [see Resultant Output] when I echo var1 / var4 / var5, any ideas how to fix this?

Code: [Select]setlocal enabledelayedexpansion
echo off

for /f "tokens=*" %%X in (c:\temp\keys.txt) do (
    set "work=%%X"
    :: echo %%X
    :: fill empty fields with "#NUL#" ...
    :: but do it twice, just in case CONSECUTIVE fields are empty
    for /l %%i in (1,1,2) do set "work=!work:;;=;#NUL#;!"
    for /F "tokens=1,2,3,4* delims=;" %%i in ("!work!") do (
        echo first is:  %%i
        echo second is: %%j
        echo third is:  %%k
        echo fourth is: %%l
        echo rest is:   %%m
        echo ----------------------------------- 
        CALL :func1 %%i %%j %%k %%l whatthe
        echo var1 : %var1%
        echo var4 : %var4%
        echo var5 : %var5%
))

goto :EOF

:func1
set "var1=%~1"
set "var2=%~2"
set "var3=%~3"
set "var4=%~4"
set "var5=%~5"
echo func values:
echo 1 %var1%
echo 2 %var2%
echo 3 %var3%
echo 4 %var4%
echo 5 %var5%
goto :EOF

Contents of c:\temp\keys.txt:
Code: [Select]this;is;a;test;file           
this;is also; a;;test file   
this file;has;;;;nul;;;data 
new:datafile;with;;;null_data;;Y

Resultant output:
Code: [Select]C:\Users\Anonymous\Downloads\DOS>setlocal enabledelayedexpansion
The system cannot find the drive specified.
first is:  this
second is: is
third is:  a
fourth is: test
rest is:   file
-----------------------------------
func values:
1 this
2 is
3 a
4 test
5 whatthe
var1 :
var4 :
var5 :
The system cannot find the drive specified.
first is:  this
second is: is also
third is:   a
fourth is: #NUL#
rest is:   test file
-----------------------------------
func values:
1 this
2 is
3 also
4 a
5 #NUL#
var1 :
var4 :
var5 :
The system cannot find the drive specified.
first is:  this file
second is: has
third is:  #NUL#
fourth is: #NUL#
rest is:   #NUL#;nul;#NUL#;#NUL#;data
-----------------------------------
func values:
1 this
2 file
3 has
4 #NUL#
5 #NUL#
var1 :
var4 :
var5 :
The system cannot find the drive specified.
first is:  new:datafile
second is: with
third is:  #NUL#
fourth is: #NUL#
rest is:   null_data;#NUL#;Y
-----------------------------------
func values:
1 new:datafile
2 with
3 #NUL#
4 #NUL#
5 whatthe
var1 :
var4 :
var5 :
Replace this:

Code: [Select]        echo var1 : %var1%
        echo var4 : %var4%
        echo var5 : %var5%

with this:

Code: [Select]        echo var1 : !var1!
        echo var4 : !var4!
        echo var5 : !var5!
Thanks a million for the fix.



Discussion

No Comment Found