Timeout with GUI adjustments breaks its design after first second passes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Timeout with GUI adjustments breaks its design after first second passes

#1 Post by DOSadnie » 31 Jul 2023 16:09

I want to use timeout as a fail-safe so I should not make it silent thus I cannot hide it. But I also do not want to see the default
"Waiting for X seconds, press a key to continue...
message, where X is the digit informing about how many seconds are left


I managed to procure such script

Code: Select all

@echo off

echo. 
echo.   Info about what the script will do
echo.

setlocal

set "countdown=3"

echo.   Executing in:
echo.

for /L %%i in (%countdown%,-1,1) do (
    <nul set /p "=%%i "
    ping 127.0.0.1 -n 2 > nul
    cls
    echo.   Executing in:
    echo.

)

:: Here be dragons

echo.   The script has been executed
echo.
echo.   [Press any key to close this CMD window]
echo.

pause > nul
and it works half way

[Due to how QUOTE area works on this forum I am using >>_<< sign to represent >> << i.e. every underscore stands in for one white space]

Because it shows at step

#1
_
___Info about what the script will do

___Executing in:

3
[thus at the very beginning it works almost as intended by me as it only lacks three spaces before the digit]


#2
___Executing in:

2
[here it starts to break its envisioned visual design]


#3
___Executing in:

1

#4
___Executing in:

___The script has been executed

___[Press any key to close this CMD window]
_



I want it to show at step

#1
_
___Info about what the script will do

___Executing in:

___3

#2
_
___Info about what the script will do

___Executing in:

___2

#3
_
___Info about what the script will do

___Executing in:

___1

#4
_
___The script has been executed

___[Press any key to close this CMD window]
_


Does anyone know how to do that?
Last edited by DOSadnie on 05 Aug 2023 03:25, edited 1 time in total.

OJBakker
Expert
Posts: 91
Joined: 12 Aug 2011 13:57

Re: Timeout with GUI adjustments breaks its design after first second passes

#2 Post by OJBakker » 31 Jul 2023 17:12

Use a for/L loop to do the countdown, and in the loop do the echoing for your message followed by an invisible timeout /T 1. And with invisible I mean suppress the output of timeout with >nul.

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Timeout with GUI adjustments breaks its design after first second passes

#3 Post by DOSadnie » 01 Aug 2023 12:07

Thank you for the idea

I managed to come up with this version

Code: Select all

@echo off

echo.
echo.   Info about what the script will do
echo.

setlocal

set "countdown=3"

echo.   Executing in:
echo.

for /l %%i in (%countdown%,-1,1) do (
    cls
    echo.
    echo.   Info about what the script will do
    echo.
    echo.   Executing in:
    echo.
    echo.   %%i
    timeout /t 1 >nul
)

cls

:: Here be dragons
::
:: Use >nul to hide them

echo.
echo.   The script has been executed
echo.
echo.   [Press any key to close this CMD window]
echo.

pause > nul
which does exactly what I need it to do - with the caveat of the CMD window being reloaded each second thus annoyingly flashing

And this version does not flash

Code: Select all

@echo off

setlocal enabledelayedexpansion

echo.
echo.   Info about what the script will do
echo.

set "countdown=3"

echo.   Executing in:
echo.

for /l %%i in (%countdown%,-1,1) do (
    <nul set /p "=   %%i   "
    timeout /t 1 >nul
)

cls

:: Here be dragons
::
:: Use >nul to hide them

echo.
echo.   The script has been executed
echo.
echo.   [Press any key to close this CMD window]
echo.

pause > nul
but keeps adding each following digit on the right side of the previous one and I was not able to add three white spaces before the first one i.e. resulting with
___Info about what the script will do

___Executing in:

3___2___1
before
___The script has been executed

___ [Press any key to close this CMD window]
is shown

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: Timeout with GUI adjustments breaks its design after first second passes

#4 Post by Batcher » 01 Aug 2023 22:00

test-1.bat

Code: Select all

@echo off
for /f %%i in ('echo prompt $H ^| cmd') do (
    set "KeyBS=%%i"
)
echo   Executing in:
for /l %%i in (3,-1,1) do (
    set /p "= %KeyBS%  %%i"<nul
    set /p "=%KeyBS%%KeyBS%%KeyBS%"<nul
    timeout /t 1 >nul
)
echo,
echo   The script has been executed
pause

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Timeout with GUI adjustments breaks its design after first second passes

#5 Post by Aacini » 01 Aug 2023 23:18

Mmm... Perhaps I don't understand your request...

However, if you want to move the cursor to a previous line, you can do it via this trick...

Antonio

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Timeout with GUI adjustments breaks its design after first second passes

#6 Post by DOSadnie » 05 Aug 2023 04:15

Batcher wrote:
01 Aug 2023 22:00
test-1.bat
[...]
This works, thank you

It shows
Executing in:
__X
__The script has been executed


I tried to improve it visually by making it to show
Executing in:

______X

__The script has been executed
but this version

Code: Select all

@echo off
setlocal enabledelayedexpansion

for /f %%i in ('echo prompt $H ^| cmd') do (
    set "KeyBS=%%i"
)

echo   Executing in:
for /l %%i in (3,-1,1) do (
    set "spaces="
    for /l %%j in (1,1,%%i) do (
        set "spaces=!spaces!     "
    )
    echo !spaces!!KeyBS!  %%i
    echo %KeyBS%%KeyBS%%KeyBS%
    timeout /t 1 >nul
)
echo.
echo   The script has been executed
pause
shows in the end
Executing in:

__________________3

____________2

______1

The script has been executed
while this version

Code: Select all

@echo off
setlocal enabledelayedexpansion

for /f %%i in ('echo prompt $H ^| cmd') do (
    set "KeyBS=%%i"
)

echo   Executing in:
echo.
for /l %%i in (3,-1,1) do (
    set "spaces="
    for /l %%j in (1,1,%%i) do (
        set "spaces=!spaces!     "
    )
    set /a "numspaces=(3-%%i)*5"
    set "sp=!spaces:~0,!numspaces!"
    echo !sp!!KeyBS!  %%i
    echo %KeyBS%%KeyBS%%KeyBS%
    timeout /t 1 >nul
)
echo.
echo   The script has been executed
pause
produces in the end
__Executing in:

numspace 3

numspace 2

numspace 1


__The script has been executed

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Timeout with GUI adjustments breaks its design after first second passes

#7 Post by DOSadnie » 13 Oct 2023 10:18

Due to not being able to come up with a properly working BAT, I decided to use this PS1 script instead

Code: Select all

Write-Host "Line of text at the very top"
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host "Line of text in the middle"
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host "Last line of text"
Write-Host

$SHOWING_OF_CURSOR_ON_TOP_PREVENTION = [System.Console]::CursorTop

# Number of seconds
3..0 | `

ForEach-Object `
 {
        [System.Console]::SetCursorPosition(0, $SHOWING_OF_CURSOR_ON_TOP_PREVENTION)
        
        if ($_ -eq 3)   # Showcase of the first digit / second
    {
    Write-Host
    Write-Host
    Write-Host "   Executing in"
    Write-Host
    Write-Host -ForegroundColor Red "    ► $_ ◄"
    Write-Host " "
    Write-Host "   seconds"
    Write-Host
    }
    
        else   # Showcase of the remaining digits / seconds
    {
    Write-Host
    Write-Host
    Write-Host "   Executing in"
    Write-Host
    Write-Host -ForegroundColor Red "    ► $_ ◄"
    Write-Host " "
    Write-Host "   seconds"
    Write-Host
    }

    Start-Sleep -Seconds 1
    
    Write-Host " `b"

}

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Timeout with GUI adjustments breaks its design after first second passes

#8 Post by Aacini » 13 Oct 2023 18:33

What about this?

Code: Select all

@echo off

cls
echo/ 
echo    Info about what the script will do
echo/

setlocal EnableDelayedExpansion

rem Get an ASCII CR (13) character
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
rem Create a file with *just* 3 spaces
call :CreateSpacesFile 3

set "countdown=3"

echo    Executing in:
echo/

for /L %%i in (%countdown%,-1,1) do (
    type SpacesFile.txt
    <nul set /p "=%%i!CR!"
    ping 127.0.0.1 -n 2 > nul
)

:: Here be dragons

echo    The script has been executed
echo/
echo    [Press any key to close this CMD window]
echo/

pause > nul
goto :EOF



:CreateSpacesFile numOfSpaces
setlocal EnableDelayedExpansion
set "spcs="
for /L %%i in (1,1,%1) do set "spcs=!spcs! "
set /P ^"=X^
% Do not remove this line %
%spcs%^" > spacesFile.tmp < nul
findstr /V "X" spacesFile.tmp > spacesFile.txt
del spacesFile.tmp
exit /B
Antonio

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Timeout with GUI adjustments breaks its design after first second passes

#9 Post by DOSadnie » 14 Oct 2023 07:05

It produces

Code: Select all

   Info about what the script will do

   Info Executing in:

   Info 3The   2The   1The   The script has been executed

   Info [Press any key to close this CMD window]
and leaves that spacesFile.txt file behnd

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Timeout with GUI adjustments breaks its design after first second passes

#10 Post by DOSadnie » 14 Oct 2023 08:08

I also just now tried going back [viewtopic.php?f=3&t=10871&p=69159#p68888] and after all settling on seeing

Code: Select all

    3     2     1
But I get

Code: Select all

3  2  1
with this script utilizing the >>set<< trick

Code: Select all

@echo off

setlocal enabledelayedexpansion

echo.
echo.   Info about what the script will do
echo.

set "countdown=3"

echo.   Executing in:
echo.

chcp 65001 >nul

set "PAUSES_A=   "
set "PAUSES_B="

for /l %%i in (%countdown%,-1,1) do (
    <nul set /p=!PAUSES_A! %%i !PAUSES_B!
    timeout /t 1 >nul
    )

:: Here be dragons
::
:: Use >nul to hide them

echo.
echo.   The script has been executed
echo.
echo.   [Press any key to close this CMD window]
echo.

pause > nul
despite adding to it

Code: Select all

chcp 65001 >nul
before using various white spaces from the list available at https://en.wikipedia.org/wiki/Whitespace_character [although I have not tested out all of them]


But the minute I make a change like for example this one

Code: Select all

set "PAUSES_A=.   "
set "PAUSES_B=; "
what I will get is this

Code: Select all

   Info about what the script will do

   Executing in:

.    3 ; .    2 ; .    1 ;
   The script has been executed

   [Press any key to close this CMD window]

So the problem, with this version, is that any number of leading white spaces are ignored i.e. not displayed; and adding back quotes for that crucial line does not help either

But as I said I already have went with using PS1, as apparently [as the version from this post also shows] BAT scripting is rather too basic to achieve such GUI elementrs

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Timeout with GUI adjustments breaks its design after first second passes

#11 Post by Aacini » 14 Oct 2023 12:07

DOSadnie wrote:
14 Oct 2023 07:05
It produces

Code: Select all

   Info about what the script will do

   Info Executing in:

   Info 3The   2The   1The   The script has been executed

   Info [Press any key to close this CMD window]
and leaves that spacesFile.txt file behnd

It works perfectly here:

#1

Code: Select all


   Info about what the script will do

   Executing in:

   3
#2

Code: Select all


   Info about what the script will do

   Executing in:

   2
#3

Code: Select all


   Info about what the script will do

   Executing in:

   1
#4

Code: Select all


   Info about what the script will do

   Executing in:

   The script has been executed

   [Press any key to close this CMD window]
Windows 10 here

Are you sure you copied the Batch file as it is?

Just add a del spacesFile.txt at end...

Antonio

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Timeout with GUI adjustments breaks its design after first second passes

#12 Post by DOSadnie » 16 Oct 2023 10:56

Yes, I am sure; this is the code cut out from UTF-8 BAT file saved on my volume

Code: Select all

@echo off

cls
echo/
echo    Info about what the script will do
echo/

setlocal EnableDelayedExpansion

rem Get an ASCII CR (13) character
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
rem Create a file with *just* 3 spaces
call :CreateSpacesFile 3

set "countdown=3"

echo    Executing in:
echo/

for /L %%i in (%countdown%,-1,1) do (
    type SpacesFile.txt
    <nul set /p "=%%i!CR!"
    ping 127.0.0.1 -n 2 > nul
)

:: Here be dragons

echo    The script has been executed
echo/
echo    [Press any key to close this CMD window]
echo/

pause > nul
goto :EOF

:CreateSpacesFile numOfSpaces
setlocal EnableDelayedExpansion
set "spcs="
for /L %%i in (1,1,%1) do set "spcs=!spcs! "
set /P ^"=X^
% Do not remove this line %
%spcs%^" > spacesFile.tmp < nul
findstr /V "X" spacesFile.tmp > spacesFile.txt
:: del spacesFile.tmp
@echo off

cls
echo/
echo    Info about what the script will do
echo/

setlocal EnableDelayedExpansion

rem Get an ASCII CR (13) character
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
rem Create a file with *just* 3 spaces
call :CreateSpacesFile 3

set "countdown=3"

echo    Executing in:
echo/

for /L %%i in (%countdown%,-1,1) do (
    type SpacesFile.txt
    <nul set /p "=%%i!CR!"
    ping 127.0.0.1 -n 2 > nul
)

:: Here be dragons

echo    The script has been executed
echo/
echo    [Press any key to close this CMD window]
echo/

pause > nul
goto :EOF

:CreateSpacesFile numOfSpaces
setlocal EnableDelayedExpansion
set "spcs="
for /L %%i in (1,1,%1) do set "spcs=!spcs! "
set /P ^"=X^
% Do not remove this line %
%spcs%^" > spacesFile.tmp < nul
findstr /V "X" spacesFile.tmp > spacesFile.txt
del spacesFile.tmp
del spacesFile.txt
exit /B
And now, after adjusting its end, I get

Code: Select all

   Info about what the script will do

   Executing in:

The system cannot find the file specified.
3TheThe system cannot find the file specified.
2TheThe system cannot find the file specified.
1The   The script has been executed

   [Press any key to close this CMD window]

   Executing in:

The system cannot find the file specified.
3TheThe system cannot find the file specified.
2TheThe system cannot find the file specified.
1The   The script has been executed

   [Press any key to close this CMD window]
i.e. doubled run, because when after seeing [the first] "Press any key to close this CMD window" I do press any key, the whole shebang is executed once more in the same CMD window

I am using Windows 10 Enterprise 20H2 x64

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Timeout with GUI adjustments breaks its design after first second passes

#13 Post by Aacini » 16 Oct 2023 12:59

Aacini wrote:
14 Oct 2023 12:07

Are you sure you copied the Batch file as it is?

Antonio
DOSadnie wrote:
16 Oct 2023 10:56
Yes, I am sure...

Mmmm... The script I posted have 47 lines. These are the last ones:

Code: Select all

38::CreateSpacesFile numOfSpaces
39:setlocal EnableDelayedExpansion
40:set "spcs="
41:for /L %%i in (1,1,%1) do set "spcs=!spcs! "
42:set /P ^"=X^
43:% Do not remove this line %
44:%spcs%^" > spacesFile.tmp < nul
45:findstr /V "X" spacesFile.tmp > spacesFile.txt
46:del spacesFile.tmp
47:exit /B
The script you posted have 90 lines! :shock: Apparently, it have my code repeated two times. This is the same section in your code:

Code: Select all

36::CreateSpacesFile numOfSpaces
37:setlocal EnableDelayedExpansion
38:set "spcs="
39:for /L %%i in (1,1,%1) do set "spcs=!spcs! "
40:set /P ^"=X^
41:% Do not remove this line %
42:%spcs%^" > spacesFile.tmp < nul
43:findstr /V "X" spacesFile.tmp > spacesFile.txt
44::: del spacesFile.tmp
45:@echo off
46:
47:cls
If you remove the exit /B command that appear in my script at line 47, you are modifying the code in a way that it don't correctly works anymore... :cry:

Antonio

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Timeout with GUI adjustments breaks its design after first second passes

#14 Post by DOSadnie » 22 Oct 2023 08:57

My fingers slipped and I performed a double pastng of what I had in the clipboard at that time

This is the code I have

Code: Select all

@echo off

cls
echo/
echo    Info about what the script will do
echo/

setlocal EnableDelayedExpansion

rem Get an ASCII CR (13) character
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
rem Create a file with *just* 3 spaces
call :CreateSpacesFile 3

set "countdown=3"

echo    Executing in:
echo/

for /L %%i in (%countdown%,-1,1) do (
    type SpacesFile.txt
    <nul set /p "=%%i!CR!"
    ping 127.0.0.1 -n 2 > nul
)

:: Here be dragons

echo    The script has been executed
echo/
echo    [Press any key to close this CMD window]
echo/

pause > nul
goto :EOF



:CreateSpacesFile numOfSpaces
setlocal EnableDelayedExpansion
set "spcs="
for /L %%i in (1,1,%1) do set "spcs=!spcs! "
set /P ^"=X^
% Do not remove this line %
%spcs%^" > spacesFile.tmp < nul
findstr /V "X" spacesFile.tmp > spacesFile.txt
findstr /V "X" spacesFile.tmp > spacesFile.txt
del spacesFile.tmp
del spacesFile.txt
exit /B
and it produces

Code: Select all

   Info about what the script will do

   Executing in:

The system cannot find the file specified.
3TheThe system cannot find the file specified.
2TheThe system cannot find the file specified.
1The   The script has been executed

   [Press any key to close this CMD window] 

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Timeout with GUI adjustments breaks its design after first second passes

#15 Post by DOSadnie » 18 Nov 2023 11:12

I have managed to produce

Code: Select all

   Info about what the script will do

   Executing in:

     3
    2
   1
   The script has been executed

   [Press any key to close this CMD window]
with this version

Code: Select all

@echo off

cls
echo/
echo    Info about what the script will do
echo/

setlocal EnableDelayedExpansion

set "countdown=3"

echo    Executing in:
echo/

for /L %%i in (%countdown%,-1,1) do (
    set "spcs="
    for /L %%j in (1,1,%%i) do set "spcs=!spcs! "
    echo   !spcs!%%i
    ping 127.0.0.1 -n 2 > nul
)

:: Here be dragons

echo    The script has been executed
echo/
echo    [Press any key to close this CMD window]
echo/

pause > nul
goto :EOF

Post Reply