Home > Enterprise >  How do I pass a variable between two batch files?
How do I pass a variable between two batch files?

Time:07-24

I want to make a login-feature for one of my batch files usiang an authorization-tool, and I'm asking myself how I pass the variable of the username from the auth-tool to the initial script.

I've been doing it so far by writing the username into a .txt file and having the main program read it, like this:

echo Guest26528 >username.txt
>nul find "Guest26528" username.txt && (set usr=Guest26528)
>nul find "ResAs" username.txt && (set usr=ResAs)

But there must be a more efficient way, right?

For anyone interested, this is the authentification-tool:

@echo off
color 1F
type authart.txt
echo.
echo.
set /a errorcount = 0
:start
echo Please login to your clearance profile
set /p usr="Username:"
if %usr%==ResAs goto login0
echo Error 303: Unknown Username
goto start 

:login0
set /p pswd="Enter Password for user ResAs:"
if %pswd%==12345 goto pass
echo Error 304: Incorrect password
set /a errorcount = %errorcount%   1
set /a attempts = 3 - %errorcount%
echo %attempts% Attempts left.
if %errorcount%==3 goto fail
goto login0
    :fail 
    echo Error 305: Too many failed login attempts.
    echo Press any button to continue.
    pause >nul
    exit
    :pass
    echo Login successful.
    echo ResAs >username.txt

I'm still very new to this whole coding thing and english isn't my main language, so I hope I wrote my question in an understandable way. I'm excited for your ideas!

CodePudding user response:

The setx command writes a variable into the registry and can be used if you want to use a variable globally for all batch files.

SETX VAR_C somevalue

Alternatively you could write the variable to a file and read it back into the other batch file ie;

Batch file A:

SET VAR_C=somevalue
ECHO %VAR_C% >%TMP%\var_c

Batch file B:

ECHO %VAR_C%

CodePudding user response:

I think this can solve your problem: https://stackoverflow.com/a/48264867/14473816

Your solution is good anyway, you can also pass batch1 variables as arguments to batch2.

  • Related