I'm trying to get a String of an users input and then let the Text scroll.
What I have so far:
@echo off
setlocal
set /p myString=String:
cls
call :StringLenght result myString
:ECHO
set /a result=%result%-1
set myString=%mystring:~%result%%
echo %myString%
ping -n 2 localhost>nul
cls
:StringLenght <resultVar> <stringVar>
(
setlocal EnableDelayedExpansion
set "s=!%~2!#"
set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" (
set /a "len+=%%P"
set "s=!s:~%%P!"
)
)
)
(
endlocal
set "%~1=%len%"
goto ECHO
)
Now at this Point it doesn't works:
set myString=%mystring:~%result%%
I can't get the String out of a variable when I'm in a variable %% (Sounds weird but I think you can imagine what I mean).
You need delayed expansion for this to work.
Here is the concept:
Should be easy to implement into your code.
An alternative without delayed expansion is to use the
call
command like this:What happens is that the expansion of
Result
is done immediately but the expansion ofmyString
is carried out to the newcmd
instance opened bycall
:%%
afterecho
is replaced by a single%
;%result%
is expanded, that is, replaced by its value4
;%%
is replaced by a single%
;echo %myString:~4%
is passed over to the newcmd
instance opened bycall
, thusthing
is displayed;Notice, that this only works in a batch file but not in command prompt (
cmd
) directly.In case you need delayed expansion for both nested variables, you could use a
for
loop:This works because the
for
variable%%I
is expanded before delayed expansion is applied, so the loop body receives the command lineecho !myString:~4!
.