Board logo

标题: [求助]dos7.1下,用echo将字符输出到文件,能否进行对齐 [打印本页]

作者: nobodytt     时间: 2009-2-1 19:58    标题: [求助]dos7.1下,用echo将字符输出到文件,能否进行对齐

比如用echo向文件中输入两行两列文字,则如果第一列的文字不一样长,那么第二列以后的文字就不能保持对齐,如下所示:

123456         test           
123          test


不知在dos7.1下有没有办法对echo输出到文件的内容进行对齐?
作者: Sufone     时间: 2009-2-2 12:31
echo命令只是将文本不加任何修改的显示,不借助其他命令无法实现楼主的要求
作者: nobodytt     时间: 2009-2-2 15:37
那么有没有什么好的外部命令可以实现?
作者: DOSforever     时间: 2009-2-6 00:51
你想怎么个对齐,左对齐?右对齐?每列首对齐?
作者: nobodytt     时间: 2009-2-6 17:06
每列左对齐
作者: DOSforever     时间: 2009-2-6 17:52
可以用 4DOS 的 @format 变量函数

  Quote:

4DOS Help Topic:  @FORMAT


@FORMAT[[-][[0]x][.y],string]:  Reformats a string, truncating it or padding
it with spaces as necessary.  If you use the minus [-], the string is
left-justified; otherwise, it is right-justified.  The x value is the
minimum number of characters in the result.  The y value is the maximum
number of characters in the result.  You can combine the options as
necessary.  For example:

    "%@format[12,JPSoftware]"   returns "  JPSoftware"
    "%@format[.3,JPSoftware]"   returns "JPS"

@FORMAT will add leading or trailing spaces if necessary to pad the result
to the minimum width specified by x.  If a leading zero is used before x,
the padding will be with 0's instead, for example:

    "%@format[4,5]"     returns "   5"
    "%@format[04,5]"    returns "0005"
    "%@format[-04,5]"   returns "5000"

See also the @COMMA function, which can make numeric values easier to read
by inserting thousands separators.

要每列左对齐的话,也就是说前一列的长度都要相同,少于所需长度的要填充空格(也可以是0),以你的例子为例,假定第一列的最大字串长度为6(列之间的空格不计,是随便你预先输入的,当然,也可以包括到函数中去)。

echo %@format[-6,123]          test

得到的结果就是

123             test