Is there way to echo text after a set command? I have tried, but nothing seems to work. Here is my code:
@echo off
Echo Enter a website:
Set /p op="Https:\\" ".com"
:: The ".com" would be displayed behind the users input.
if %op%==%op% goto Show
:Show
cls
Echo Website: Http:\\%op%.com
pause
exit
How would I get the .com to be displayed after the input? I would preferably like to have the ".com" frozen in one spot, no matter how big the users input is.
I took the solution from this answer and slightly modified it in order to fulfill this request.
@echo off
setlocal EnableDelayedExpansion
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 "op="
set /P "=Https:\\.com!BS!!BS!!BS!!BS!" < NUL
:nextKey
set "key="
for /F "delims=" %%K in ('xcopy /W "%~F0" "%~F0" 2^>NUL') do if not defined key set "key=%%K" & set "key=!key:~-1!"
if "!key!" equ "!CR!" goto endInput
if "!key!" neq "!BS!" (
set "op=%op%%key%"
set /P "=.!BS!%key%.com!BS!!BS!!BS!!BS!" < NUL
) else if defined op (
set "op=%op:~0,-1%"
set /P "=.!BS!!BS!.com !BS!!BS!!BS!!BS!!BS!" < NUL
)
goto nextKey
:endInput
echo/
echo/
echo Website: Http:\\%op%.com
EDIT: New method added (requested in a comment)
@echo off
setlocal EnableDelayedExpansion
set /A spaces=10, backSpaces=spaces+4
set "spcs="
for /L %%i in (1,1,%spaces%) do set "spcs=!spcs! "
set "back="
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
for /L %%i in (1,1,%backSpaces%) do set "back=!back!!BS!"
set /P "op=Https:\\%spcs%.com!back!"
echo/
echo Website: Http:\\%op%.com
Per @Marged's comments, I suspect this is impossible (or at least extremely difficult) by batch file.
Here's a PowerShell solution should that be of use:
function Get-UserInput {
param (
[parameter(mandatory = $false)]
[string]$pre='Enter text:'
,
[parameter(mandatory = $false)]
[string]$post='_'
)
process {
[string]$text=''
while ($key.Key -ne 'Enter') {
write-host "`r$pre$text$post " -NoNewline #trailing space to hide deleted chars
#replace the above with the 3 below if you want user input to be a different colour to the defaults
#write-host "`r$pre" -NoNewline
#write-host $text -NoNewline -ForegroundColor Cyan
#write-host "$post " -NoNewline #trailing space to hide deleted chars
$key = [Console]::ReadKey($true)
switch ($key.Key)
{
'Backspace' { $text = $text.substring(0,($text.length-1)) }
default { $text = $text + $key.KeyChar }
}
}
write-host "" #undo no new line
write-output "$pre$text$post"
}
}
clear-host
$input = Get-UserInput -pre 'https://' -post '.com'
"User entered: '$input'"