2013-08-03 33 views
3

我希望我的.bat腳本做這樣的事情在控制檯:如何在命令提示符下更改控制檯中的字符?

C:\Users\username>somescript.bat 
Doing something in: 3 

一秒鐘後...

C:\Users\username>somescript.bat 
Doing something in: 2 

我怎麼會做這樣的事情?另外,我不想清除控制檯,只是改變窗口上的一件事。

回答

5

您可以批量執行此操作。 Windows cmd無法直接訪問顯示器,但可以使用回車或退格字符。

@echo off 
setlocal EnableDelayedExpansion 

REM ** Create a single carriage return character in the `CR` variable 
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a" 

REM ** Count from 3 to 0 down 
REM ** ECHO for each number the line without linefeed 
REM ** And wait a second with the ping command 
for /L %%n in (3 -1 0) do (
    <nul set /p=Do something %%n!CR! 
    ping localhost -n 2 > nul 
) 
echo(

第一部分只是設置回車符爲CR變量。

<nul set /p =Text是不帶換行符顯示文本的技巧,所以光標位於最後一個字符後面。
但在這種情況下,我追加!CR!,所以光標移回到同一行中的第一列。

+0

這個腳本令我困惑...你確定它是正確的嗎? – Ethan

+0

不錯!非常適合我! – RGuggisberg

+0

@EthanShaughnessy我加了一些解釋 – jeb

1

你不能這樣做批量。 Windows cmd無法直接訪問顯示器。你應該使用另一種編程語言,例如。 C#。

相關問題