I have tried to make Windows batch file that blinking the word "Wait" & "Wait..". I tried the following code:
#Echo OFF
setlocal EnableDelayedExpansion
for /f %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"
SET p=-1
set num=2
set st[1]=Wait
set st[2]=Wait..
set st[3]=eer
:LOOP
if /i %num% equ 1 (
set num=2
) else (
set num=1
)
<nul set /P "=!st[%num%]!!CR!"
TIMEOUT /T 1 >NUL
GOTO :LOOP
The problem here, the IF seems to be worked only one time. i.e running the batch makes it prompt "Wait" only one time, then "Wait.." forever. What is the mistake here?
You problem is not the if (that works as intended), your problem are the spaces that should delete/overwrite the dots.
set "st[1]=Wait "
set "st[2]=Wait.."
Related
Here is what I am trying to do. Suppose I have a program called myprogram.exe, which I have to execute 1000 times.
Under Windows, I could usually do something as simple as:
for /L %n in (1,1,1000) do start /myfolder/myprogram.exe
However, suppose I only have 5 CPU threads I can devote to running the 1000 instances of myprogram.exe, such that I launch only 5, then when one of these finishes another one is launched, etc until the whole 1000 end.
Under Linux and using GNU Parallel, I could simply do:
seq 1000 | parallel -N0 -j5 "nohup myprogram.exe"
How could I achieve something like that in Windows command line? Notice that in my case using Cygwin is not an option, so resorting to xargs and GNU Parallel under Windows are not options either.
Here is a way, by using powershell to do the process count. and using a simply set /a as counter.
#echo off
setlocal enabledelayedexpansion
set /a cnt=0
:counter
if !cnt! lss 1000 (
for /F "tokens=*" %%i in ('powershell ^(Get-Process -Name 'myprogram'^).count') do set proc=%%i
if !proc! lss 5 (
start "C:\myfolder\myprogram.exe"
set /a cnt+=1
)
goto :counter
)
You could add echo !cnt! in the line before goto :counter if you want to see it count.
It can be done without using delayedexpansion but I prefer to use it here.
This will run five processes in parallel. Each time one of them finishes, the next process will be started (so there are always 5 of them until they all are done)
#ECHO off
setlocal enabledelayedexpansion
set bunch=5
for /l %%a in (1,1,1000) do (
call :loop
echo processing: %%a
start "MyCommand" cmd /c timeout !random:~-1!
)
call :loop
goto :eof
:loop REM waits for available slot
for /f %%x in ('tasklist /fi "windowtitle eq MyCommand" ^| find /c "cmd.exe"') do set x=%%x
if %x% geq %bunch% goto :loop
goto :eof
Add the /min switch to start to minimize the started processes.
Give them a unique windowtitle (MyCommand here) to be able to count them.
Replace cmd /c timeout !random:~-1! with your actual command.
EDIT a slightly modified script, which may work better, if myprogram is a GUI (script above will work better with CLI applications):
#ECHO off
setlocal enabledelayedexpansion
set bunch=5
for /l %%a in (1,1,100) do (
call :loop
echo processing: %%a
start notepad.exe
)
call :loop
goto :eof
:loop REM waits for available slot
for /f %%x in ('tasklist /fi "imagename eq Notepad.exe"^|find /c "."') do set x=%%x
if %x% geq %bunch% goto :loop
goto :eof
How can I show the progress of a long running operation in windows batch (cmd) file in percentage? Can you share some example code?
Here is how...
Note: This code is a slightly modified version of this answer.
#echo off
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
FOR /L %%n in (1,1,10) DO (
call :show_progress %%n 10
ping localhost -n 2 > nul
)
echo Done!
exit /b
:show_progress
setlocal EnableDelayedExpansion
set current_step=%1
set total_steps=%2
set /a "progress=(current_step * 100) / total_steps"
set /p ".=Progress: !progress!%%!CR!" <nul
if !progress! equ 100 echo.
exit /b
An example would be scanning a large file/database, showing progress instead of a blinking cursor, for ages!
set c=<no. of lines in file>
for /l %i in (1,1,%c%) do cls & set /p="Scanning for target ... "<nul & (set /a per=%i00/%c% >nul & echo !per!%)& ping 127.0.0.1 -n 2 > nul & REM when target found, send agents to greet
echo Complete.
The code in brackets () are for progress percentage.
Tested in Win 10 CmD>
newbie here who would appreciate some help. I'm trying to generate to generate random file names (i.e. for each file in the source directory, a different random alphanumeric figure should be generated)
The function that generates the random alphanumeric name works but when the value is returned to the main routine, it remains the same across all the files. Not sure where the problem is but I suspect it has something to do with delayed expansion.
#Echo Off
Setlocal EnableDelayedExpansion
dir "D:/Source" /b > List.txt
FOR /F %%i in (List.txt) DO (
CALL:Alpha Numb
echo !Numb!
md D:\Output\%%~ni
rar a -v50M -hpabc123 -m0 -ep "D:\Output\%%~ni\!Numb!.rar" "D:\Source\%%i"
)
goto:eof
:Alpha
Setlocal EnableDelayedExpansion
Set _RNDLength=40
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
REM Echo %_RndAlphaNum%
ENDLOCAL & SET "%~1=%_RndAlphaNum%"
Exit /b
I have created the following batch file that makes Wait "| / -- \" animation. I want to use it during processing mysql restore database command mysql -u %DBuser% -p %DB% < "%thefile%" where thefile is the sql dump file path
#Echo OFF
setlocal EnableDelayedExpansion
for /f %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"
SET p=-1
set num=1
set "st[1]=| "
set "st[2]=/ "
set "st[3]=--"
set "st[4]=\ "
if /i %p% lss 0 (
set p=2
call :LOOP
call :DoSomeThing
)
:LOOP
if /i %num% lss 4 (
set /a num=num+1
) else (
set num=1
)
<nul set /P "=Wait !st[%num%]!!CR!"
TIMEOUT /T 1 >NUL
GOTO :LOOP
:DoSomeThing
TIMEOUT /T 10 >NUL
echo Doing...
Here, :DoSomeThing is for testing purposes and It should be replaced or include the mysql command. I get the problem that :LOOP works for ever and there is no call to :DoSomeThing
I tried to call :DoSomeThing before call :LOOP but the LOOP started after DoSomeThing is finished so it becomes useless! Is there any way to make the DoSomeThing or the MySQL command works in the background while the animation wait loop works too?
EDIT: Some explanations added
In order to fulfill your request it is necessary to execute two threads simultanously, so one thread execute the mysql command and the other thread execute the looping wait animation. Both threads can be synchronized using a flag file that is created before mysql execution starts and is deleted after it ends, so the wait animation loops until the flag file is deleted.
The way to create a new thread is via start command, that may run a second Batch file that execute the mysql command and delete the flag file. However, in order to keep all the code in the same place, the start command may run the same Batch file (represented by "%~F0" in the code below). The key that allows this trick to work is a special parameter that indicate if the Batch file was re-executed from inside itself, so in this case the code just goto the section that execute the mysql command and delete the flag file.
#Echo OFF
rem If the Batch file was re-executed with the special parameter (second thread)
rem go to the section that execute the mysql command
if "%~1" equ ":DoSomething" goto %1
setlocal EnableDelayedExpansion
for /f %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"
set num=0
set "st[0]=| "
set "st[1]=/ "
set "st[2]=--"
set "st[3]=\ "
rem Do here anything you want before the mysql command...
echo Doing something for 10 seconds...
rem Create the flag file
echo X > DoingSomething
rem Re-start this Batch file with the special parameter
start "" /B "%~F0" :DoSomething
rem Simultaneously execute the waiting animation
call :LOOP
rem Do here anything you want after the mysql command...
rem ... and terminate
goto :EOF
:LOOP
set /a num=(num+1) %% 4
<nul set /P "=Wait !st[%num%]!!CR!"
TIMEOUT /T 1 >NUL
IF EXIST DoingSomething GOTO :LOOP
echo Ending loop
goto :EOF
:DoSomeThing
rem Place here the mysql command
TIMEOUT /T 10 >NUL
rem Delete the flag file
del DoingSomething
rem And terminate the second thread
goto :EOF
So far I've been trying to make a continuous .bat file that will start the server file, read every line that comes down and if the response "Server has become unresponsive" then the bat will close the file and re-open(this needs to be done every hour or so and I'm not always at the computer)
I do believe this is the correct code but I need to double check with some tech-savy minds to see if it's correct.
#echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%A in (`"findstr rust_server/n ^^ "`) do (
set "myVar=%%A"
call :processLine myVar
)
goto :eof
:processLine
SETLOCAL EnableDelayedExpansion
set "line=!%1!"
set "line=!line:*:=!"
echo(!line!
Find /I /V "Unresponsive for 10"
taskkill /fi "WindowTitle eq rust_server*"
start /d "C:\Rust Server" rust_server.exe
ENDLOCAL
goto :eof
Any ideas/suggestions would be greatly appreciated.
#Echo off
Title SERVER RESTARTER (place with your rust_server exe)
color 1f
SET n=0
:Loop
SET /A n=n+1
echo Server Restarter
echo -----Restart Server Batch VERSION-----
taskkill /IM rust_server.exe
echo Opening rust_server.exe server again
rust_server.exe
if %n% EQU 60 (
exit
) Else if %n% LEQ 24 (
Goto Loop