簡體   English   中英

Windows批處理文件循環在處理過程中等待動畫命令

[英]Windows batch file looping Wait animation during processing command

我創建了以下批處理文件,該文件使Wait“ | /-\\”動畫。 我想在處理mysql restore數據庫命令期間使用它mysql -u %DBuser% -p %DB% < "%thefile%" 其中, thefile是sql轉儲文件路徑

@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...

在這里, :DoSomeThing僅用於測試目的,應該替換或包括mysql命令。 我遇到了一個問題:LOOP永遠有效,並且沒有調用:DoSomeThing

我試圖在:LOOP之前調用:DoSomeThing ,但是在DoSomeThing完成后LOOP開始,因此變得毫無用處! 有什么方法可以使DoSomeThing或MySQL命令在后台運行,而動畫等待循環也可以運行?

編輯添加了一些說明

為了滿足您的請求,必須同時執行兩個線程 ,因此一個線程執行mysql命令,另一個線程執行循環等待動畫。 兩個線程都可以使用標記文件進行同步,該標記文件是在mysql執行開始之前創建的,並且在結束之后將被刪除,因此等待動畫循環播放直到標記文件被刪除。

創建新線程的方法是通過start命令,該命令可以運行第二個批處理文件,該文件執行mysql命令並刪除標志文件。 但是,為了將所有代碼都保留在同一位置, start命令可以運行相同的批處理文件(在下面的代碼中以"%~F0"表示)。 允許該技巧起作用的鍵是一個特殊參數 ,該參數指示是否從內部重新執行了批處理文件,因此,在這種情況下,代碼僅轉到執行mysql命令的部分並刪除標志文件。

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM