批处理实现文本内容居中显示的代码

所属分类: 脚本专栏 / DOS/BAT 阅读数: 520
收藏 0 赞 0 分享
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原始窗口中,不调节窗口大小,不生成临时文件,居中显示文本内容。输出如下:
代码:


[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来取得行号同时兼容空行,
并将行号进行变量赋值;使用findstr /o来取得第行的字符偏移量,但要通过
上、下行的字符偏移差,因为字符偏移值是类计递加的,同时每行回车增加
两个字符偏移,所以不可避免要对最后一行强加回车,在不破坏原文件的情
况下只好生成临时文件;利用findstr /o能够一次性获得文本所有行的字符偏
移量也就是字符总数,效率和逐字符判断相比肯定是高得多了。
---------------------------------
复制代码 代码如下:

@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.
更多精彩内容其他人还在看

dos命令 cd命令使用说明[图文说明]

Cddir(change directory,可以缩写为cd),其功能是显示当前目录的名称,或更改当前的目录
收藏 0 赞 0 分享

dos(cmd)中删除、添加、修改注册表命令

在CMD命令行下修改Windows注册表方法及Hiv技术,注册表regedit的未公开的参数的可以在cmd下使用
收藏 0 赞 0 分享

CMD下读取/修改/删除注册表项的方法

先说注册表。很多命令行下访问注册表的工具都是交互式的,溢出产生的shell一般不能再次重定向输入/输出流,所以无法使用
收藏 0 赞 0 分享

批处理利用HOSTS文件(屏蔽,加速)网站的代码

批处理利用HOSTS文件(屏蔽,加速)网站的代码,直接运行按照提示操作即可
收藏 0 赞 0 分享

QQ多帐号自动登录批处理脚本

QQ多帐号自动登录批处理脚本,需要的朋友可以参考下
收藏 0 赞 0 分享

cmd模式下中文乱码的解决方法(注册表)

故障描述 cmd模式下中文乱码,客户运行操作软件,需要修改cmd,发现cmd乱码,客户不愿重做系统
收藏 0 赞 0 分享

删除默认共享的批处理

删除默认共享的批处理代码,这个代码比较智能,从c-z盘的都执行了,并通过修改注册表防止admin$共享在下次开机时重新加载
收藏 0 赞 0 分享

批处理创建完全,只读共享文件夹的批处理代码

批处理创建完全,只读共享文件夹的批处理代码,需要的朋友可以参考下
收藏 0 赞 0 分享

关闭所有分区自动播放和恢复所有分区自动播放的cmd批处理

关闭所有分区自动播放和恢复所有分区自动播放.cmd批处理,需要的朋友可以参考下
收藏 0 赞 0 分享

批处理技术内幕 ECHO命令介绍

echo是批处理中最简单的命令,但是你真的掌握了吗?你知道echo输出空行的十种方法吗?你知道用echo怎么输出on或者off或者/?吗?你知道echo, echo+ echo.哪个效率更高吗
收藏 0 赞 0 分享
查看更多