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

所属分类: 脚本专栏 / DOS/BAT 阅读数: 470
收藏 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.
更多精彩内容其他人还在看

arp病毒 防止arp病毒的批处理

建个文本文档 复制以上代码保存为bat文件.加载到启动项去,这样应该能防护到.每次开机时都绑定绑定网关Mac和IP 确实能防止中arp
收藏 0 赞 0 分享

del rd命令行下删除文件不需要确认

del rd命令行下删除文件不需要确认
收藏 0 赞 0 分享

dos下用recover修复损坏的TXT文件

修复损坏的TXT文件的简单方法
收藏 0 赞 0 分享

批处理设置windows服务器的代码ThecSafe1.9.4第1/3页

一个既是服务器安全设置工具,也是一个学习批处理非常好的教程。虽然入手看是难了点,只有带着问题去学习才能真正的去了解。
收藏 0 赞 0 分享

服务器常用批处理代码 原创

经常维护服务器,由于mssql数据库经常会占用大量内存,所以我查找资料写了如下代码,方便自己也利于别人使用,都是直接从服务器上直接拿来的
收藏 0 赞 0 分享

利用mshta调用运行js或vbs的

mshta同时调用js和vbs
收藏 0 赞 0 分享

DOS命令 Interlnk、Intersvr命令使用说明

interlnk    启动简易网客户机 [适用场合]  如果有两台电脑可以通过它和intersvr联成简易的网络,这是条很有          用的命令,但不常用
收藏 0 赞 0 分享

dos shutdown 关机命令

用dos命令实现windows 关机
收藏 0 赞 0 分享

systeminfo 系统命令快速查看你的系统几岁了

Windows系统安装使用到现在,想知道它的“高龄”是多少吗?
收藏 0 赞 0 分享

批处理删除空文件夹

有人问我,什么工具能寻找和删除硬盘上所有空的文件夹,或只包含空的子文件夹的文件夹?杀鸡焉用宰牛刀,其实不用任何软件,直接使用批处理命令就能实现这个目的。
收藏 0 赞 0 分享
查看更多