【问题标题】:Make cmd window on top of others使 cmd 窗口位于其他窗口之上
【发布时间】:2014-10-28 09:54:27
【问题描述】:

我正在编写简单的批处理脚本来隐藏用户输入的密码。解决方案是创建一个弹出窗口并更改该弹出窗口的文本颜色。以下是代码,它可以正常工作

@echo off
Echo Please enter your password in the popup window and then press enter

set tempbat="%temp%\p.cmd"

REM Create temporary batch file to make popup window for entering password 'masked'
echo mode 20,1 >%tempbat%
echo color 01 >>%tempbat%
echo Title Enter Password >>%tempbat%
echo setlocal enabledelayedexpansion >>%tempbat%
echo set /p Pass= >>%tempbat%
echo echo !pass!^>"%temp%\pass.txt" >>%tempbat%
echo exit >>%tempbat%

start /wait "" %tempbat%
del %tempbat% 2>NUL

set /p Pwd=<"%temp%\pass.txt"
del "%temp%\pass.txt" 2>NUL

echo %Pwd%

我唯一担心的是,当弹出窗口发生时,我可以将它设置在主 cmd 窗口的顶部,甚至禁用对主 cmd 窗口的访问(我希望像 Bootstrap Modal 这样的行为)?

感谢您的阅读,希望能得到您的帮助

【问题讨论】:

  • 使用弹出窗口来隐藏密码可能没有用,但如果这是你想要的,你可以使用 CMDOW。 commandline.co.uk/cmdow
  • 感谢您的建议。但我的工作只需要纯批处理脚本。我无权使用第三方脚本

标签: batch-file cmd popup disabled-control always-on-top


【解决方案1】:

可以批量混淆用户输入,而不是试图用某些东西来掩盖密码。见MC ND的代码from this answer

@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%

【讨论】:

  • 我刚刚发现如果我的密码中包含一些特殊字符(例如 ^ " ' ())它不起作用。你能帮忙吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 2023-01-27
  • 2010-12-27
  • 1970-01-01
  • 2010-12-24
  • 2021-07-15
  • 1970-01-01
相关资源
最近更新 更多