【问题标题】:batch file to mask input with * without an external file批处理文件用 * 屏蔽输入而没有外部文件
【发布时间】:2014-04-08 16:06:16
【问题描述】:

我需要批处理文件来用 * 屏蔽输入而不需要外部文件,我需要代码快速写信

例如:

     @echo off
     set char=*
     set /p variable=Enter your password:
     set char=%variable%
     echo %char%
     pause

【问题讨论】:

标签: file batch-file input mask


【解决方案1】:

这是在批处理文件中使用 Powershell 的一种方法

@echo off
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
        [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
echo %password%

Powershell 默认安装在所有较新的操作系统上,因此这是一个很好的折衷方案。如果你必须支持 XP 机器,你可以用 VBScript 做类似的事情。

【讨论】:

  • 当我单击鼠标左键并移动鼠标时,它会显示我的白色东西,我不知道如何向您解释
  • “for 循环”实际上是评估它的唯一方法吗?
【解决方案2】:

是的!有一种纯批处理的方法,滥用xcopyprompt命令我们可以做到

    @echo off
setlocal enabledelayedexpansion
call:show "Enter your passwordÿ" "0a"
call:hidePass
set /p password=<_tmp&del _tmp&echo/
echo/%password%
pause>nul
exit
:hidePass
    set "str="
    set "chars="
:writing
    set "key="
    for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
        if not defined key set "key=%%L"
    )
    setlocal EnableDelayedExpansion
    set "key=!key:~-1!"
    if "%key%" == "" (echo/!str!>_tmp&exit/b)
    call:show "-" "0c"
    set "str=!str!!key!"
    goto :writing
:show
    for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
        set "DEL=%%a"
    )
    call :ColorText %~2 "%~1"
    exit/b
:ColorText
    <nul set /p ".=%DEL%" > "%~2"
    findstr /v /a:%1 /R "^$" "%~2" nul
    del "%~2" > nul 2>&1
    exit/b

唯一的问题是你不能使用 * 作为掩码,在这种情况下我使用-

【讨论】:

  • 我的立场是正确的。这是一些非常有趣的代码。我尝试使用* 只是为了看看它会做什么;) ctrl-c ctrl-c quick!
  • 如果你编辑-成为*会更好,缺点是空格显示*在框中不希望空格显示在框中*你可以编辑这些东西吗?
  • 当我写密码时,我觉得写字母很慢,你能不能给另一个代码来更快地输入字母?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-15
  • 2017-03-23
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-15
相关资源
最近更新 更多