Make cmd window on top of others - batch-file

I am working on simple batch script to hide the password inputted by user. The solution is to create a popup and change text-color for that popup. Following is the code and it works properly
#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%
My only concern is that when the popup occurs, can I set it always on top of main cmd window, and even disable access to main cmd window (I expect the behavior like Bootstrap Modal)?
Thank for reading and hope to receive helps from you

Instead of trying to cover the password with something, it is possible to obfuscate user input in batch. See MC ND's code 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%

Related

Making a batch file do something once

I'm making a game and I want a personalized username option that only appears once.
Here is an example:
#echo off
:onetime
echo please enter a username
echo.
set /p newuser=%newuser%:
echo %newuser%> cfg.txt
goto menu
:menu
for /f "tokens=* delims=" %%x in (cfg.txt) do echo %%x
cls
...
I'm trying to figure out how to make :onetime happen once, so that it sends the username to cfg.txt
Anyone know how?
I have cleaned up your code and provided a completed working script, and noted where you will put your game code.
Although Delayed expansion is not needed for this code, I have placed it in the script as you had it in your original script.
Please be mindful of the variable names I have chosen as they are used in several locations.
I have tried to comment the code with info on what it is doing, and I would be happen to explain further if needed.
I'd like to actually play your game when you are completed with it, if I may. :)
Hope that helps.
Ben
BPG.cmd
REM Script: BPG.cmd
REM Version: 1.0
REM Description: Game to play about Monsters.
Rem Notes: Currently Implementing Menu System.
Rem Sets up Variables and checks if the script needs to be re-called.
#(
SETLOCAL ENABLEDelayedExpansion
echo off
SET "eLvl=0"
SET "ScriptFolder=%~dp0"
SET "Log=%~dpn0.log"
SET "ConfigFile=%~dpn0_cfg.txt"
IF /I "%~1" NEQ "MAX" (
ENDLOCAL
ECHO.Game not started Maximized, Opening in a New Window by Running: Start "BPG 1 A Batch of Monsters" /MAX "%~dpnx0" MAX
Start "BPG 1 A Batch of Monsters" /MAX "%~dpnx0" MAX
EXIT /b %eLvl%
)
COLOR 2
)
REM Calls Main Function.
CALL :Main
REM Ends the script.
(
ENDLOCAL
EXIT /b %eLvl%
)
REM Main Function, most of your coding goes here, and this function calls sub functions.
:Main
Rem Check if Config file exists, if it does not, then call the New User Function.
IF NOT EXIST "%ConfigFile%" (
CALL :NewUser
)
Rem Load Username from Config file.
FOR /F "Tokens=*" %%A IN ('Type "%ConfigFile%"') DO (
SET "_UserName=%%~A"
)
Rem Call Menu System
CALL :Menu
REM Based off the option chosen Either Start a new Game or Skip to the End.
REM ECHO.CALL %_Menu_Choice%
CALL %_Menu_Choice%
GOTO :EOF
:NewUser
SETLOCAL
REM Get the Username Input
SET /P "_User=Please Enter a Username: "
REM Output the Username to the config file, overwriting all the file contents:
echo.%_User%>"%ConfigFile%"
ENDLOCAL
GOTO :EOF
:Menu
SETLOCAL
REM Output the Config file contents:
Type "%ConfigFile%"
REM Clear the screen
cls
echo ______ _______ _______
echo I ___ \ I ____ I I ____ \
echo I I I II I II I I \/
echo I I__/ / I I____II I I
echo I __ I I _____I I I ____
echo I I \ \ I I I I \_ I
echo I I___I II I I I___I I
echo I______/ I_/ I_______I A BATCH OF MONSTERS
echo.
echo.
echo 1) Begin
echo.
echo 2) Exit
echo.
set /p "_Choice=%_UserName%, Enter a Number: "
(
REM End the local variable Space, and Set return variables based on the choice, or re-draw the menu.
ENDLOCAL
IF /I "%_Choice%" EQU "1" (
REM ECHO.%_Choice% EQU 1
SET "_Menu_Choice=:Begin_Game"
) ELSE (
IF /I "%_Choice%" EQU "2" (
REM ECHO.%_Choice% EQU 2
SET "_Menu_Choice=GOTO :EOF"
) ELSE (
ECHO.%_Choice% Not Valid!
PAUSE
GOTO :Menu
)
)
)
goto :EOF
:Begin_Game
REM All the remaining code for your game should probably go here.
GOTO :EOF
try checking file existence :
#echo off
:onetime
setlocal enableDelayedExpansion
if not exist "cfg.txt" (
echo please enter a username
echo.
set /p newuser=%newuser%:
echo !newuser!> cfg.txt
goto menu
)
:menu
for /f "tokens=* delims=" %%x in (cfg.txt) do echo %%x
cls

Batch File Command Hide Password [duplicate]

This question already has answers here:
Can I mask an input text in a bat file?
(19 answers)
Closed 6 years ago.
I have this batch file I wrote to open putty and want to make it a universal script for others. The script is as follows
#echo off
::Written by Mark Gulick::
::Today's Date 20150316::
set /p U="Enter Username: "
set /p P="Enter Password: "
set /p DC="Enter DC Number: "
start /d "C:\Program Files (x86)\putty\" PUTTY.EXE %U%#b0%DC%db -pw %P%
pause
I would like to make the password not show up and have tried some areas on here and haven't found one that will work. I might be doing it wrong too. I'm a little rusty on my scripting. Am I missing something or should I use something else other then the set command?
You can do something like this :
#echo off & setlocal DisableDelayedExpansion
Title %~n0
Mode 50,5 & Color 0E
set /p U="Enter Username : "
Call:InputPassword "Enter Password" P
set /p DC="Enter DC Number: "
setlocal EnableDelayedExpansion
start /d "C:\Program Files (x86)\putty\" PUTTY.EXE !U!#b0!DC!db -pw !P!
pause
::***********************************
:InputPassword
Cls
echo.
echo.
set "psCommand=powershell -Command "$pword = read-host '%1' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set %2=%%p
)
goto :eof
::***********************************
This post on DOSTips references a post here by MC ND, but I can't find the original, so here it is again. Whenever you want to get a password and mask the input, simply call :getPassword target_variable input_prompt where target_variable is the name of the variable you store the password in and input_prompt is whatever you show the user to prompt them to enter their password.
#echo off
setlocal enabledelayedexpansion
set /p "user_name=Enter username here:"
call :getPassword user_password "Enter password here: "
:: The user's password has been stored in the variable %user_password%
exit /b
::------------------------------------------------------------------------------
:: Masks user input and returns the input as a variable.
:: Password-masking code based on http://www.dostips.com/forum/viewtopic.php?p=33538#p33538
::
:: Arguments: %1 - the variable to store the password in
:: %2 - the prompt to display when receiving input
::------------------------------------------------------------------------------
:getPassword
set "_password="
:: We need a backspace to handle character removal
for /f %%a in ('"prompt;$H&for %%b in (0) do rem"') do set "BS=%%a"
:: Prompt the user
set /p "=%~2" <nul
:keyLoop
:: 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%"
:: If No keypress (enter), then exit
:: If backspace, remove character from password and console
:: Otherwise, add a character to password and go ask for next one
if defined key (
if "%key%"=="%BS%" (
if defined _password (
set "_password=%_password:~0,-1%"
set /p "=!BS! !BS!"<nul
)
) else (
set "_password=%_password%%key%"
set /p "="<nul
)
goto :keyLoop
)
echo/
:: Return password to caller
set "%~1=%_password%"
goto :eof
batch-file's cannot do this alone, but if you're running batch that means you're likely on windows, and automatically have VBScript installed. You can use this to get a password with masking:
randomThing.bat
echo Blah blah blah...
:: Call a vbscript file with the outputs being set as a variable
for /f "usebackq tokens=*" %%r in (`wscript "password.vbs"`) do set retPass=%%r
:: Write back the input for example's sake.
echo What you typed in: %retPass%
password.vbs
'Call this script from a batch file to input a masked password
set objPassword = createObject("scriptPW.password")
wScript.stdOut.write "Input Password:"
strPassword = objPassword.getPassword()
wScript.echo strPassword
Note I'm answering this on mobile, and as such cannot test if this method still works. However it gets the main idea across.

How to make a batch SET /P statememt not require an enter

I'm making my own console based off batch because I want more then what cmd offers but have the key features that batch has.
One of the most important things is to prompt the user for the command:
:prompt
#echo off
title JDOS command line
echo.
echo.
SET /P command="%FDIR%>"
rem SECTIONS/COMMANDS
rem /I means the if statement is not case-sensitive. USE IT AT ALL TIMES!
if /I "%command%"=="add" goto add
if /I "%command%"=="subtract" goto subtract
goto prompt
(This is just a small portion of all the options but you get the idea)
:add
title Calculator/Add
SET /P Add_A=Please enter the first number:
SET /P Add_B=Please enter the second number:
SET /A sum=%Add_A% + %Add_B%
echo The sum is %sum%
timeout /t 10 >nul
goto prompt
But this is timewasting and sort of idiotic (at least in my opinion).
So, can I execute goto add without the user pressing enter ?
Edit:
I'm thinking an approach similar to CHOICE but with the option to have more than 1 key be pressed (so for example instead of 1/2/3/4/ it would be restart/shutdown/lock/logoff/
I modified my accepted answer at this post and adjusted it for this request. Here it is:
#echo off
setlocal EnableDelayedExpansion
set "commands=add subtract"
for %%a in (%commands%) do set "com[%%a]=1"
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
set "command="
:nextKey
set "key="
for /F "delims=" %%K in ('xcopy /W "%~F0" "%~F0" 2^>NUL') do if not defined key set "key=%%K"
if "!key:~-1!" equ "!CR!" goto endCommand
if "!key:~-1!" equ "!BS!" (
if defined command (
set "command=%command:~0,-1%"
set /P "=.!BS!!BS! !BS!!BS!" < NUL
)
) else if "!key:~-1!" neq " " (
set "command=%command%!key:~-1!"
set /P "=!key:~-1!" < NUL
if defined com[!command!] goto endCommand
)
goto nextKey
:endCommand
echo/
echo/
echo command read: "%command%"
Note: The mechanism used in this method does not allow to insert spaces! This code should be modified in large parts in order to read spaces.

Can only write one word on chat batch file

The code below is the part of my program. My problem is that when i enter my message it works perfectly if its 1 word. but if it is more it says goto was expected. the Q and R options are needed to refresh the chat and pop up a quit prompt.
set /p M=Enter Your Message:
if %M%==Q goto B
if %M%==q goto B
if %M%==R goto A
if %M%==r goto A
echo %U%: %M% >>%S%.txt
What should i do?
ps. this is my first quistion on here sorry if formating is wrong)
If the input has more than one word, the parser will see, after variable replacement
if this is a test==Q goto B
which is an invalid command.
Simplest solution is to use quotes
if "%M%"=="Q" goto B
....
will be interpreted as
if "this is a test"=="Q" goto B
edited to adapt to comments
This is a skeleton of a chat script with "auto update" in the terms indicated in the comments.
#echo off
setlocal enableextensions disabledelayedexpansion
rem Configuration
set "chatFile=c:\temp\chat.txt"
set "lastSize=-1"
:selectOption
rem Update screen
call :refresh
rem retrieve user selection
>nul choice /c rqw /n /t 3 /d r /m ""
rem Check what has been selected
if errorlevel 3 call :write & goto :selectOption
if errorlevel 2 goto :quit
rem Select another option
goto :selectOption
:refresh [bForceRefresh]
rem Check if refresh is being forced
if not "%~1"=="" set "lastSize=-1"
rem Ensure we have a file
if not exist "%chatFile%" >"%chatFile%" echo(
rem Check if we can skip the refresh.
rem If file has no changed its size there are no changes in content
for %%a in ("%chatFile%") do if %%~za leq %lastSize% (
goto :skipRefresh
) else (
set "lastSize=%%~za"
)
rem Paint the screen
cls
type "%chatFile%"
rem Show the options
echo(
<nul set /p ".=[R]efresh [Q]uit [W]rite ?"
:skipRefresh
goto :eof
:write
setlocal
rem Ask message
echo(
echo(
set "message="
set /p "message=What do you want to say? >"
rem If there is a message, write to output file,
rem else force screen refresh to remove the message question
if defined message (
setlocal enabledelayedexpansion
>>"%chatFile%" echo(%time% :[ %username% ]: !message!
endlocal
) else (
call :refresh forceRefresh
)
endlocal
goto :eof
:quit
cls
exit /b

batch file to replace the input with * without external file [duplicate]

This question already has answers here:
batch file to mask input with * without an external file
(2 answers)
Closed 8 years ago.
i need batch file to mask the input with * without external file and i need the code to ((((be fast in write letters))))) and does not Calculates the distance with *
((((if there is code extract the external file from the batch file which make the external file make password with * and fast to write password add comment to see the code ))))
FOR EXAMPLE:
#echo off
set char=*
set /p variable=Enter your password:
set char=%variable%
echo %char%
pause
This is a "cleanup" of the code you get in your previous question. If this is not fast enough for you, then you should consider not doing it in batch and use some aditional tool.
#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%

Resources