Mads Gadeberg
Mads Gadeberg

Reputation: 1480

How to assign call argument to var and echo it in Windows batch script

I want to do this:

set kommune

FOR /F "tokens=* delims=" %%x in (DBLib.txt) DO (
    CALL :decryptLine "%%x"
)
GOTO:eof

:decryptLine
for /f "tokens=1,* delims==" %%a in ("%~1") do set argument=%%a & set value=%%b
set "argument=%argument:~0,-2%"
set "value=%value:~1%"
call:updateVar "%argument%" "%value%"
GOTO:EOF

:updateVar
IF "%~1" == "KommuneNavn" (
    ECHO "%~2"
    ECHO "KommuneNavn"
    set kommune=%~2
    ECHO kommune = "%kommune%" testhest
)

What it outputs:

"ABC Test Kommune"
"KommuneNavn"
"kommune = "" testhest"

How do i copy the value of the secont argument to the Variable "kommune"? And Echo it?

Edit 1: updated to exact code. "inside IF"

Upvotes: 0

Views: 88

Answers (1)

Magoo
Magoo

Reputation: 79982

@ECHO OFF
SETLOCAL

set kommune

FOR /F "tokens=* delims=" %%x in (q27922463.txt) DO (
    CALL :decryptLine "%%x"
)
GOTO:eof

:decryptLine
for /f "tokens=1,* delims==" %%a in ("%~1") do set "argument=%%a" & set "value=%%b"
set "argument=%argument:~0,-2%"
set "value=%value:~1%"
call:updateVar "%argument%" "%value%"
GOTO:EOF

:updateVar
IF "%~1" == "KommuneNavn" (
    ECHO "%~2"
    ECHO "KommuneNavn"
    set kommune=%~2
    CALL ECHO kommune = "%%kommune%%" testhest
)

GOTO :eof

Critical point: You haven't shown us the content of your file, so we have to construct it: and I've changed the filename to suit my system (q27922463.txt)

contents of q27922463.txt

KommuneNavnxy=yourvalue

output generated:

"ourvalue"
"KommuneNavn"
kommune = "ourvalue" testhest

Note the positioning of the quotes in the set assignments. Batch is sensitive to spaces in a SET statement. SET FLAG = N sets a variable named "FLAGSpace" to a value of "SpaceN"

So, %%a becomes KommuneNavnxy, is assigned to argument, and the last 2 characters are removed, making KommuneNavn

Similarly, %%b gets yourvalue, you remove the first and make ourvalue

Since the string kommune is set within the code block of the if statement, you need to use call echo %%var%% to display it (one of several ways).

Upvotes: 2

Related Questions