how to access argument passed in a function

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchnewbie
Posts: 2
Joined: 08 Nov 2021 19:35

how to access argument passed in a function

#1 Post by batchnewbie » 08 Nov 2021 19:51

Dear all,

based on
https://stackoverflow.com/questions/583 ... batch-file


below is my code to find out string length of a variable called

Code: Select all


SET trace_log_script_name_prefix=%until_number%_%scn_or_sequence%_%source_host_db%_%log_datetime%_%rman_script_prefix%

call :strlen result trace_log_script_name_prefix

REM ********* function *****************************
:strlen <resultVar> <stringVar>
(   
    setlocal EnableDelayedExpansion
	echo argument %~2
	exit /b
    (set^ tmp=!%~2!)
    if defined tmp (
        set "len=1"
        for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
            if "!tmp:~%%P,1!" NEQ "" ( 
                set /a "len+=%%P"
                set "tmp=!tmp:~%%P!"
            )
        )
    ) ELSE (
        set len=0
    )
)
( 
    endlocal
    set "%~1=%len%"
    exit /b
)
output is as follow;

Code: Select all

setlocal EnableDelayedExpansion
 echo argument C:\PROGRA~1\avs\bin
 exit /b
 (set tmp=!C:\PROGRA~1\avs\bin! )
 if defined tmp (
set "len=1"
 for %P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (if "!tmp:~%P,1!" NEQ "" (
set /a "len+=%P"
 set "tmp=!tmp:~%P!"
) )
)  ELSE (set len=0 )
)
argument C:\PROGRA~1\avs\bin
unfortunately argument should not be C:\:\PROGRA~1\avs\bin it should be 149055640992_scn_lap63-sin_wcdb_20211109_094802_set_until_scn_no_newname_datafile_no_newname_tempfile_no_rename_redo_restore_db_preview_validate


so how do I access the argument passed in a function? It seems that no matter how I setlocal, arguments seems to equate the the glocal value.

many thanks in advance!

Squashman
Expert
Posts: 4360
Joined: 23 Dec 2011 13:59

Re: how to access argument passed in a function

#2 Post by Squashman » 09 Nov 2021 08:23

You have obviously changed the function from what is on StackOverFlow and you are not using it correctly either. I have no idea why you chose to put that extra code at the top of the function.
Plain and simple if you want to use that code from SO then this is all you should need.

Code: Select all

@echo off
REM Assuming all variables referenced in SET command are indeed defined in previous commands.
SET trace_log_script_name_prefix=%until_number%_%scn_or_sequence%_%source_host_db%_%log_datetime%_%rman_script_prefix%

call :strlen result trace_log_script_name_prefix
echo %result%

goto :eof

REM ********* function *****************************
:strlen <resultVar> <stringVar>
(   
    setlocal EnableDelayedExpansion
    (set^ tmp=!%~2!)
    if defined tmp (
        set "len=1"
        for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
            if "!tmp:~%%P,1!" NEQ "" ( 
                set /a "len+=%%P"
                set "tmp=!tmp:~%%P!"
            )
        )
    ) ELSE (
        set len=0
    )
)
( 
    endlocal
    set "%~1=%len%"
    exit /b
)

Post Reply