处理期间Windows批处理文件循环等待动画命令
我已经创建了下面的批处理文件,使Wait“|/--\”动画。我想在处理mysql还原数据库命令时使用它处理期间Windows批处理文件循环等待动画命令,windows,batch-file,cmd,Windows,Batch File,Cmd,我已经创建了下面的批处理文件,使Wait“|/--\”动画。我想在处理mysql还原数据库命令时使用它mysql-u%DBuser%-p%DB%做某事 rem使用特殊参数重新启动此批处理文件 启动“/B”%~F0”:DoSomething rem同时执行等待动画 呼叫:循环 rem在这里做任何你想在mysql命令之后做的事情。。。 雷姆。。。终止 后藤:EOF :循环 set/a num=(num+1)%%4 努尔 如果存在正在做某事转到:循环 回音结束环 后藤:EOF :剂量 rem在这里放置
mysql-u%DBuser%-p%DB%<%thefile%“
其中thefile
是sql转储文件路径
@Echo关闭
setlocal EnableDelayedExpansion
对于('copy/Z'%~f0“nul')中的/f%%a,请设置“CR=%%a”
设置p=-1
设置num=1
设置“st[1]=|”
设置“st[2]=/”
设置“st[3]=--”
设置“st[4]=\”
如果/i%p%lss 0(
设置p=2
呼叫:循环
电话:DoSomeThing
)
:循环
如果/i%num%lss 4(
set/a num=num+1
)否则(
设置num=1
)
努尔
转到:循环
:剂量
超时/T 10>NUL
回声在做。。。
这里,:DoSomeThing
用于测试目的,应该将其替换或包含mysql命令。我遇到的问题是,:LOOP
永远有效,并且没有调用:DoSomeThing
在调用:LOOP
之前,我尝试调用:DoSomeThing
,但该循环在DoSomeThing完成后启动,因此它变得无用!有没有办法让DoSomeThing或MySQL命令在后台工作,而动画等待循环也可以工作 编辑:添加了一些解释
为了满足您的请求,需要同时执行两个线程,因此一个线程执行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
@Echo关闭
如果使用特殊参数(第二个线程)重新执行批处理文件,则rem
rem转到执行mysql命令的部分
如果“%~1”相等:DoSomething“转到%1
setlocal EnableDelayedExpansion
对于('copy/Z'%~f0“nul')中的/f%%a,请设置“CR=%%a”
set num=0
设置“st[0]=|”
设置“st[1]=/”
设置“st[2]=--”
设置“st[3]=\”
在使用mysql命令之前,您可以在这里执行任何操作。。。
echo做某事10秒钟。。。
rem创建标志文件
回声X>做某事
rem使用特殊参数重新启动此批处理文件
启动“/B”%~F0”:DoSomething
rem同时执行等待动画
呼叫:循环
rem在这里做任何你想在mysql命令之后做的事情。。。
雷姆。。。终止
后藤:EOF
:循环
set/a num=(num+1)%%4
努尔
如果存在正在做某事转到:循环
回音结束环
后藤:EOF
:剂量
rem在这里放置mysql命令
超时/T 10>NUL
rem删除标志文件
戴尔在做什么
rem并终止第二个线程
后藤:EOF
这似乎是一个很好的解决方案,但我需要对代码进行一些解释,特别是第二行,将其修改为使用mysql命令。您只需在下面插入mysql命令:DoSomeThing
标签,以代替TIMEOUT/t10>NUL
行。只需确保在末尾保留deldoingsomething
命令……实际上,我正在使用MySQL命令作为批处理的一部分来收集用户的数据输入。在该批中,我已经有了一个名为:restore
的标签子例程。当我试图用您提供的代码更改它时,输入中会出现问题。我相信代码的第二行是关键,我只需要解释一下它的代码,用:restore
替换:doSomeThing
,而不影响:restore
内部的输入。
@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