JSI Tip 5535. How can I cause a called batch file to return a call directed environment variable?

You can pass a string to a called routine and have it return an environment variable whose name is the string.


If you wish to determine how many parameters your batch file was passed, and return the results in a environment variable named answer, include the following statement in your batch:

call cntargs answer %*

NOTE: The variable answer need not be defined.

The cntargs batch file could contain:

@echo off
if \{%1\}

\{\} @echo Syntax: call cntargs string \[arg1 arg2 ... argn\]&goto :EOF setlocal set /a numargs=0 :numloop if \{%2\}

\{\} endlocal&set /a %1=%numargs%&goto :EOF set /a numargs=%numargs% + 1 shift /2 goto numloop
After the call cntargs answer %* statement, you can use the answer environment variable, as in @echo My batch was passed %answer% parameters.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.