How to avoid! at the given line of the .bat file
I have a password with !
. No matter what I tried it crashes with !
. My script is using ENABLEDELAYEDEXPANSION
, so I'm not sure which is exactly why I'm having a problem. Here is my code:
set password=cccvvv!
echo %password%
When I run the script I get cccvvv
.
I've tried the following:
set password=cccvvv!!
set password=cccvvv^^!
Nothing seems to work.
How can I get him to hold the key !
in a variable?
source to share
Check him
@echo off setlocal enableextensions enabledelayedexpansion rem Escaped exclamation set "password=this is a test^!" rem See the environment block set password rem Echo with normal expansion echo %password% rem Echo with delayed expansion echo !password!
You need to avoid exclamation in order to keep it inside a variable while delayed expansion is active, and the parser will usually remove it when you use a variable without delayed syntax while delayed expansion is active.
source to share
I offer you this solution wrapped in PowerShell;)
@echo off
Title Mask/hide password entry *****
Mode con cols=60 lines=5
:CheckPassword
cls & color 0B & echo.
set MyPassword=cccvvv!
set "psCommand=powershell -Command "$pword = read-host 'Enter your 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
if %MyPassword%==%password% (Goto:Good) else (Goto:Bad)
exit/b
::***********************************************************************************************
:Good
Cls & Color 0A
echo(
echo Good Password
TimeOut /T 1 /NoBreak>nul
Exit
::***********************************************************************************************
:Bad
Cls & Color 0C
echo(
echo Bad password
TimeOut /T 1 /NoBreak>nul
Goto:CheckPassword
::***********************************************************************************************
source to share