【问题标题】:batch file to replace the input with * without external file [duplicate]用*替换输入的批处理文件,没有外部文件[重复]
【发布时间】:2014-04-09 09:04:19
【问题描述】:
我需要批处理文件来用 * 屏蔽输入而没有外部文件,我需要代码 ((((写信要快))))) 并且不使用 * 计算距离
((((如果有代码从批处理文件中提取外部文件使外部文件用*设置密码并快速写入密码添加注释以查看代码))))
例如:
@echo off
set char=*
set /p variable=Enter your password:
set char=%variable%
echo %char%
pause
【问题讨论】:
标签:
file
batch-file
input
replace
【解决方案1】:
这是对您在上一个问题中获得的代码的“清理”。如果这对您来说不够快,那么您应该考虑不要批量执行并使用一些额外的工具。
@echo off
setlocal enableextensions disabledelayedexpansion
rem Call the subroutine to get the password
call :getPassword password
rem Echo what the function returns
if defined password (
echo You have typed [%password%]
) else (
echo You have typed nothing
)
rem End of the process
endlocal
exit /b
rem Subroutine to get the password
:getPassword returnVar
setlocal enableextensions disabledelayedexpansion
set "_password="
rem We need a backspace to handle character removal
for /f %%a in ('"prompt;$H&for %%b in (0) do rem"') do set "BS=%%a"
rem Prompt the user
set /p "=password ?:" <nul
:keyLoop
rem retrieve a keypress
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%"
rem handle the keypress
rem if No keypress (enter), then exit
rem if backspace, remove character from password and console
rem else add character to password and go ask for next one
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(
rem return password to caller
if defined _password ( set "exitCode=0" ) else ( set "exitCode=1" )
endlocal & set "%~1=%_password%" & exit /b %exitCode%