Loading progress animation like with batch scripting - batch-file

i want to make such as loading indicator just like : installation is running...
and those dots should be appear progressively and once they are three their number should be reinitialized to one dot and all happens in the same line.
for this i've tried to mix code of two small scripts : the first plays continuestly the dots
and the seconds makes a counter increase its value by echoing it in the same line.
at the end i got this code which is the closest solution for what i want to achieve but still with a problem which is that when there are already three dots, the next row which has less number of dots will overwrite its similar part from the previous one but the remaining part from the previous row will stay so at the end it looks like there are always three dots and all will looks like just a simple static echoed text..
here is the code :
#echo off
cls
setlocal EnableDelayedExpansion EnableExtensions
call :CreateCR
:spinner
set mSpinner=%mSpinner%.
if %mSpinner%'==.................................' (
set mSpinner=.
)
:: Wait one second.
ping 127.0.0.1 -n 2 >nul
set /P "=Installation Java en cours%mSpinner%!CR!" <nul
::set /p "=Installation Java en cours" <nul
::echo %mSpinner%
goto spinner
:exit
::echo(
goto :eof
:CreateCR
rem setlocal EnableDelayedExpansion EnableExtensions
set "X=."
for /L %%c in (1,1,13) DO set X=!X:~0,4094!!X:~0,4094!
echo !X! > %temp%\cr.tmp
echo\>> %temp%\cr.tmp
for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do (
endlocal
set cr=%%a
goto :eof
)
goto :eof

You can capture a backspace character to a variable like this (courtesy of dbenham):
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
Then to erase your dots, simply send a number of
<NUL set /P "=%BS%"
... one %BS% for each dot you want to erase. As an alternative, you could use a spinner that spins instead of dots that grow. See this question and answer. In the answer there, insert the for /f line above to capture the backspace to %BS% at the top of the script, and replace all occurrences of ^H with %BS% in the spinner strings at the bottom.

A more simplistic approach would be the following:
#echo off
:spinner
set mSpinner=%mSpinner%.
if %mSpinner%'==....' (set mSpinner=.)
cls
echo Installation in progress%mSpinner%
ping 127.0.0.1 -n 2 >nul
goto spinner

Related

Formatting generated numbers in a .bat file

Forgive me if there is a simple answer to this, I'm new to all of this.
The below .bat script generates a list of numbers depending on how many numbers you want.
However what I would like is to format the numbers it generate.
For example, if I input 20, instead of it coming out 1, 2, 3 etc. I would like it to come out as 001, 002... 020.
Is this possible? Am I missing something obvious?
Many Thanks.
#echo off
setlocal EnableExtensions
:start
cls
set /p loopcount= How Many Users?:
set "x="0"
:loop
set /a "x+=1"
echo %x%
set /a loopcount=loopcount-1
if %loopcount%==0 goto exitloop
goto loop
:exitloop
pause
goto start
just implementing SomethingDark's suggestion (and a minor change in the logic of the loop):
set /p "loopcount= How Many Users?: "
set "x=1000"
:loop
set /a x+=1
echo %x:~-3%
set /a loopcount-=1
if %loopcount% gtr 0 goto :loop
echo loop finished.
(btw: your set "x="0" has a quote too much (probably a typo)
Here's a quick example batch file which uses powershell for your leading zeroes.
I have used a for loop as the looping mechanism.
#Echo Off
SetLocal EnableExtensions
:AskNum
Set "num=1"
Set /P "num=how many users?"
Set num | findstr.exe /RX "^num=[123456789][0123456789]*$" 1>NUL || GoTo AskNum
For /F %%G In ('powershell.exe "1..%num% | %% ToString User000"') Do Echo %%G
Pause
This script will not continue unless the end user inputs an integer, without a leading 0, and which is a minimum value of 1. Here you can modify the Echo command in Do to an alternative, for example net.exe User %%G /Add, or if you wish, to a parenthesized sequence of commands. In each case %%G will contain the returned string with the incremented three digits.This version prepends each three digit number sequence with an additional string, (User), but that can simply be deleted or replaced if/as required.
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET /p "lastnum= How many numbers? "
SET /a lastnum+=1000
FOR /L %%e IN (1001,1,%lastnum%) DO SET /a num=%%e&ECHO !num:~-3!
GOTO :EOF
No need for complication

pulling a variable from text file in a sentence

im currently making something along the lines of a sentence generator, it uses text files which have a list of words in them, for example celebrity.txt has a list of celebrities and this script both shuffles the text file into newcelebrity.txt and takes the first one from that list so its different every time, and ive run into a problem, i want it to be one line and that you can call a variable in the sentance youre typing, not break it down as it is right now, is there a way to have it "this $celebrity is really great" as of now, it works like this: https://gyazo.com/9ae8583ed5457709bd1c1dc9cc0cc106 and outputs as this https://gyazo.com/1a5a90f1fbf80faa73d71791a8c1c761, i dont mind the quotation marks at all, its just the way you input it.
Is there any way to make it work like i want it to or is this a limitation of batch files?
set /p message=Unity:
set /p input=The variable:
set /p after=Unity:
:gen
setlocal
cd ..
cd rcs
echo doing background work, please wait a few seconds :)
for /f "delims=" %%a in (%input%.txt) do call set "$$%%random%%=%%a"
(for /f "tokens=1,* delims==" %%a in ('set $$') do echo(%%b)>new%input%.txt
endlocal
cls
set "File=C:\Users\%USERNAME%\Desktop\gg\rcs\new%input%.txt"
set /a count=0
echo background work done :)
timeout /t 1 >nul
SETLOCAL enabledelayedexpansion
for /F "tokens=* delims=" %%a in ('Type "%File%"') do (
Set /a count+=1
Set "output[!count!]=%%a"
)
For /L %%i in (1,1,%Count%) Do (
Call :Action "!output[%%i]!"
pause
)
Exit
::*******************************************************
:Action
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo %message% %1 %after%
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo if you want to go back and change your inputs type back
echo if you want to continue generating, type gen
echo.
set /p instruction=
goto %instruction%
::*******************************************************
To get rid of the quotation marks, just use %~1 instead of %1.
I'm assuming, you want to input something like 'this $celeb is great' instead of the first three set /p lines (as your output is already in one line).
I used some fixed variables to keep it short - of course you would fetch them from the user and the text file instead, as you already did.
#echo off
setlocal EnableDelayedExpansion
set "count=3"
set "output[3]=John"
set "sentence=this $celeb is great."
REM above would be your 'set /p' instead
call :action "!output[%count%]!"
goto :of
:action
echo !sentence:$celeb=%~1!
(just to be clear: $celeb is not a variable - it's just a string, that we replace later, so from user's perspective, it behaves like a variable)

Find current console size in batch

I am working on a batch file and i need to print hyphens ( - ) across the screen as a separator. Is there a fast (under two seconds) command that can do this?
I have done multiple search queries and could not find the answer on various websites.
(code to find screen size)
for /l %%a in (1,1,%screen size var%) do (set "line=%line%-")
echo %line%
The output should show a line of hyphens across the console.
This is untested, but based upon the output from Mode CON as used in gjpio's answer:
#Echo Off
For /F "Skip=4Tokens=1*Delims=:" %%A In ('Mode CON')Do (For /L %%C In (1,1,%%B)Do #Set/P "=-"<Nul)&Echo( &GoTo :Draw
:Draw
Pause
If you intend to use the separator multiple times within your script you could save it to a variable:
#Echo Off
For /F "Skip=4Tokens=1*Delims=:" %%A In ('Mode CON')Do (For /L %%C In (1,1,%%B)Do (Call Set "separator=%%separator%%-"))&GoTo :Next
:Next
Echo Welcome to %~nx0
Echo %separator%
Pause
As a final afterthought, and just in case you feel that it would perform quicker, I thought I'd better provide a version using delayed expansion too:
#Echo Off&SetLocal EnableDelayedExpansion&Set "separator="
For /F "Skip=4Tokens=1*Delims=:" %%A In ('Mode CON')Do (For /L %%C In (1,1,%%B)Do Set "separator=!separator!-")&GoTo :Next
:Next
Rem Uncomment the next line if you don't want to use delayed expansion in the rest of the script
::EndLocal&Set "separator=%separator%"
Rem Your code goes here
Echo Welcome to %~nx0
Echo %separator%
Pause
As an addition to all of the above, you could also leverage powershell to do this too:
#Echo Off
For /F %%A In ('Powershell -NoP "Write-Host('-' * $(Get-Host).UI.RawUI.WindowSize.Width)"')Do Set "separator=%%A"
Echo Welcome to %~nx0
Echo %separator%
Pause
If you can work out the batch commands retrieve the columns value you could use the output from the MODE command;
MODE CON
C:\Users\gjp>mode con
Status for device CON:
----------------------
Lines: 9001
Columns: 120
Keyboard rate: 31
Keyboard delay: 1
Code page: 850
I would suggest you to use the following code:
#echo off
setlocal EnableDelayedExpansion
for /F "skip=4 tokens=2" %%A IN ('mode CON') do (
for /L %%B IN (1 1 %%A) do set "hyphen=!hyphen!-"
echo !hyphen!
goto :subroutine
)
:subroutine
echo You may continue here
pause
which is a bit complicated, but should do what you want.
The code searches for the columns in mode CON's command output and adds to hyphen variable these accordingly.

Batch Loading screen with no Glitching/Lag

Here is a bit of my batch Loading script
Echo Loading
Echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
Echo ºÛ º
Echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
ping localhost -n 2 > nul
cls
Echo Loading
Echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
Echo ºÛÛ º
Echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
ping localhost -n 2 > nul
cls
Echo Loading
Echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
Echo ºÛÛÛ º
Echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
exit
Using the cls code to wipe the screen makes the screen look like a "Blink". Is there a better way to clear the screen for this batch file? TIA
Just far from perfect less glitching option: Instead of repaint the full screen, repaint only the loading line (but this has to be last painted line in the screen).
The basic idea is to ouput the line ending with a carriage return but without a line feed so the cursor is moved to the start of the line to write again over the same line.
#echo off
setlocal enableextensions enabledelayedexpansion
Rem Get a carriage return character
set "CR=" & for /f %%a in ('copy /Z "%~f0" nul') do if not defined CR set "CR=%%a"
rem The progress bar
set "fill=[##########]"
cls
echo(:: computers were created to give humans time to think while waiting ....
rem For each character in the fill
for /l %%a in (2 1 11) do (
rem Calculate the right part of the bar
set "spaces=!fill:~%%a!"
rem Output the left and right parts of the bar and carriage return
<nul set/p ".=:: Loading something big !fill:~0,%%a!!spaces:#= !!CR!"
rem Pause for a second
ping -n 2 "" > nul
)
echo(
echo(:: Done
If you can not change your code/design so the last line is the only one repainted, at least try to enclose the painting operation inside a block (code enclosed in parenthesis) so in each paint operation commands are only parsed once, and prepare everything before starting to paint. It will not avoid the glitches, but will be less evident
#echo off
setlocal enableextensions enabledelayedexpansion
for /l %%a in (0 10 100) do (
call :loadingScreen %%a
>nul ping -n 2 ""
)
echo(:: Done
goto :eof
:loadingScreen percent
setlocal enableextensions enabledelayedexpansion
rem Prepare everything
set "sb=+----------+"
set "fill=^|##########^|"
set "eb=+----------+"
set /a "chars=2+%~1/10"
set "spaces=!fill:~%chars%!"
set "loadBar=!fill:~0,%chars%!!spaces:#= !"
rem Time to paint
( cls
echo(:: computers were created to give humans time to think while waiting .... %time%
echo(
echo( %sb%
echo( Loading something big %loadBar%
echo( %eb%
echo(
)
goto :eof

Batch script .exe delete selection

There is a folder which contains some random files:
file1.txt
file2.exe
file3.cpp
file4.exe
How to SIMPLY display exe files connected with numbers like this:
1. file2.exe
2. file4.exe
And then I enter the number of the file, which I want to delete.. If it is even possible to do this simply..
Shortest bullet proof solution I can come up with. Like Anders, the DEL statement is disabled by the ECHO command. Remove the ECHO to make the menu functional.
#echo off
setlocal disableDelayedExpansion
for /f "delims==" %%A in ('set menu 2^>nul') do set "%%A="
for /f "tokens=1* delims=:" %%A in ('dir /b *.exe 2^>nul ^| findstr /n "^"') do (
set menu%%A=%%B
echo %%A. %%B
)
if not defined menu1 exit /b
set "delNum="
set /p "delNum=Delete which file (enter the number): "
setlocal enableDelayedExpansion
if defined menu!delNum! echo del "!menu%delNum%!"
The only thing I can think of that could go wrong is part of the menu could scroll off the screen if there are too many entries.
Additional messages can easily be incorporated. and an ELSE condition could be appended to the input validation to deal with invalid input.
A few subtle points of the code:
FINDSTR /N provides incrementing file number. Avoids need for delayed expansion or CALL within menu builder loop. Delayed expansion should not be enabled when expanding a FOR variable containing a file name because it will corrupt names containing !.
: is a safe FOR delimiter because a file name cannot contain :.
delNum is cleared prior to SET /P because SET /P will preserve existing value if <Enter> is pressed without entering anything.
Checking for the existence of the variable is the simplest way to validate the input. This is why it is critical that any existing MENU variables are undefined prior to building the menu.
Must use delayed expansion in IF DEFINED validation, otherwise space in input could crash the script (thanks Anders for pointing out the flaw in the original code)
DEL target must be quoted in case it contains spaces, even when delayed expansion is used.
Added test to make sure at least one menu entry exists before continuing. There may not be any .exe files left to delete.
#echo off
setlocal EnableDelayedExpansion
set i=0
for %%f in (*.exe) do (
set /A i+=1
set file[!i!]=%%f
echo !i!. %%f
)
set i=0
set /P i=File to delete:
del !file[%i%]!
Not exactly pretty but it gets the job done
#echo off
setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
goto main
:addit
set /A end=end + 1
set %end%=%~1
echo %end%. %~1
goto :EOF
:main
set end=0
for %%A in ("*.exe") do (
call :addit "%%~A"
)
if "%end%"=="0" goto :EOF
echo.&set idx=
set /P idx=Delete (1...%end%)
if not "%idx"=="" if %idx% GEQ 1 if %idx% LEQ %end% (
for /F "tokens=1,* delims==" %%A in ('set %idx% 2^>nul') do (
if "%idx%"=="%%~A" (
echo.Deleting %%~B...
rem del "%%~B"
)
)
)

Resources