批處理實(shí)現(xiàn)文本內(nèi)容居中顯示的代碼
更新時(shí)間:2010年10月11日 09:07:05 作者:
CMD原始窗口中,不調(diào)節(jié)窗口大小,不生成臨時(shí)文件,居中顯示文本內(nèi)容。
a.txt
代碼:
[Promise don't come easy]
I should have known all along.
There was something wrong.
I just never read between the lines.
Then I woke up one day and found you on your way.
Leaving nothing but my heart behind.
What can I do to make it up to you.
Promises don't come easy.
But tell me if there's a way to bring you back home to stay.
Well I'd promises anything to you.
I've been walkin' around with my head hanging down.
Wondrin' what I'm gonna do.
'Cause when you walked out that door.
要求,CMD原始窗口中,不調(diào)節(jié)窗口大小,不生成臨時(shí)文件,居中顯示文本內(nèi)容。輸出如下:
代碼:
[Promise don't come easy]
I should have known all along.
There was something wrong.
I just never read between the lines.
Then I woke up one day and found you on your way.
Leaving nothing but my heart behind.
What can I do to make it up to you.
Promises don't come easy.
But tell me if there's a way to bring you back home to stay.
Well I'd promises anything to you.
I've been walkin' around with my head hanging down.
Wondrin' what I'm gonna do.
'Cause when you walked out that door.
Batcher:
sed -e :a -e "s/^.\{1,77\}$/ & /;ta" a.txt
sed.exe
http://xiazai.jb51.net/201010/tools/sed_jb51.rar
batman:思路說明:
以下代碼是充分findstr命令的示例,使用findstr /n來取得行號同時(shí)兼容空行,
并將行號進(jìn)行變量賦值;使用findstr /o來取得第行的字符偏移量,但要通過
上、下行的字符偏移差,因?yàn)樽址浦凳穷愑?jì)遞加的,同時(shí)每行回車增加
兩個(gè)字符偏移,所以不可避免要對最后一行強(qiáng)加回車,在不破壞原文件的情
況下只好生成臨時(shí)文件;利用findstr /o能夠一次性獲得文本所有行的字符偏
移量也就是字符總數(shù),效率和逐字符判斷相比肯定是高得多了。
---------------------------------
@echo off
copy /y 1.txt 2.txt>nul
echo.>>2.txt&echo end>>2.txt
for /f "tokens=1,2* delims=: " %%i in ('findstr /n /o .* 2.txt') do (
set "_%%i=%%j"&set ".%%i=%%k"
if %%i geq 2 call :lp %%i
)
del /q 2.txt&pause>nul&goto :eof
:lp
set /a n=%1-1
set /a num=_%1-_%n%-2
set /a kl=(80-num)/2
for /l %%i in (1,1,%kl%) do set /p= <NUL
call,set /p=%%.%n%%%<NUL&ECHO.
代碼:
[Promise don't come easy]
I should have known all along.
There was something wrong.
I just never read between the lines.
Then I woke up one day and found you on your way.
Leaving nothing but my heart behind.
What can I do to make it up to you.
Promises don't come easy.
But tell me if there's a way to bring you back home to stay.
Well I'd promises anything to you.
I've been walkin' around with my head hanging down.
Wondrin' what I'm gonna do.
'Cause when you walked out that door.
要求,CMD原始窗口中,不調(diào)節(jié)窗口大小,不生成臨時(shí)文件,居中顯示文本內(nèi)容。輸出如下:
代碼:
[Promise don't come easy]
I should have known all along.
There was something wrong.
I just never read between the lines.
Then I woke up one day and found you on your way.
Leaving nothing but my heart behind.
What can I do to make it up to you.
Promises don't come easy.
But tell me if there's a way to bring you back home to stay.
Well I'd promises anything to you.
I've been walkin' around with my head hanging down.
Wondrin' what I'm gonna do.
'Cause when you walked out that door.
Batcher:
sed -e :a -e "s/^.\{1,77\}$/ & /;ta" a.txt
sed.exe
http://xiazai.jb51.net/201010/tools/sed_jb51.rar
batman:思路說明:
以下代碼是充分findstr命令的示例,使用findstr /n來取得行號同時(shí)兼容空行,
并將行號進(jìn)行變量賦值;使用findstr /o來取得第行的字符偏移量,但要通過
上、下行的字符偏移差,因?yàn)樽址浦凳穷愑?jì)遞加的,同時(shí)每行回車增加
兩個(gè)字符偏移,所以不可避免要對最后一行強(qiáng)加回車,在不破壞原文件的情
況下只好生成臨時(shí)文件;利用findstr /o能夠一次性獲得文本所有行的字符偏
移量也就是字符總數(shù),效率和逐字符判斷相比肯定是高得多了。
---------------------------------
復(fù)制代碼 代碼如下:
@echo off
copy /y 1.txt 2.txt>nul
echo.>>2.txt&echo end>>2.txt
for /f "tokens=1,2* delims=: " %%i in ('findstr /n /o .* 2.txt') do (
set "_%%i=%%j"&set ".%%i=%%k"
if %%i geq 2 call :lp %%i
)
del /q 2.txt&pause>nul&goto :eof
:lp
set /a n=%1-1
set /a num=_%1-_%n%-2
set /a kl=(80-num)/2
for /l %%i in (1,1,%kl%) do set /p= <NUL
call,set /p=%%.%n%%%<NUL&ECHO.
相關(guān)文章
windows xp下沒有dos的choice命令的解決方法
dos6.0下能實(shí)現(xiàn)的choice選項(xiàng),但是因?yàn)閏hoice是外部命令,在xp中沒有choice.exe所以不能實(shí)現(xiàn)選擇菜單。解決辦法:用SET命令代替2017-03-03cmd環(huán)境變量命令set 設(shè)置永久環(huán)境變量命令setx
這篇文章主要介紹了cmd環(huán)境變量命令set 設(shè)置永久環(huán)境變量命令setx,需要的朋友可以參考下2020-08-08批處理中屏蔽錯(cuò)誤信息 nul 2>nul 的用法 (cmd>nul空格2>nul)
批處理中屏蔽錯(cuò)誤信息這個(gè) >nul 2>nul 可以屏蔽一切屏幕的輸出可以這樣理解2012-06-06Windows2003下批量添加和導(dǎo)出所有ip BAT腳本
這篇文章主要介紹了Windows2003下批量添加和導(dǎo)出所有ip BAT腳本,本文直接給出代碼實(shí)例,需要的朋友可以參考下2015-07-07使用批處理文件(.bat)啟動多個(gè)CMD窗口并執(zhí)行命令
由于每次啟動本機(jī)的kafka都需要打開2個(gè)cmd窗口,于是想起了批處理來幫忙一鍵啟動,本文就來詳細(xì)的介紹一下,感興趣的可以了解一下2023-10-10用bat和 reg實(shí)現(xiàn)關(guān)閉局域網(wǎng)共享
這篇文章主要是為大家介紹一下通過reg關(guān)閉局域網(wǎng)共享的代碼,其實(shí)這個(gè)cmd命令就是一個(gè)注冊表的導(dǎo)入功能,直接雙擊運(yùn)行reg文件也是可以的2007-10-10