I cannot recall where I found this code. Sorry about that.
Code: Select all
@echo off
call :getPassword password
echo Password:%password%
pause
exit /b
rem Subroutine to get the password
:getPassword returnVar
set "_password="
for /f %%a in ('"prompt;$H&for %%b in (0) do rem"') do set "BS=%%a"
set /p "=Password:" <nul
:keyLoop
set "key="
for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined key set "key=%%a"
set "key=%key:~-1%"
if defined key (
if "%key%"=="%BS%" (
if defined _password (
set "_password=%_password:~0,-1%"
setlocal enabledelayedexpansion & set /p "=!BS! !BS!"<nul & endlocal
)
) else (
set "_password=%_password%%key%"
set /p "=*"<nul
)
goto :keyLoop
)
echo.
if defined _password ( set "exitCode=0" ) else ( set "exitCode=1" )
endlocal & set "%~1=%_password%" & exit /b %exitCode%