如何写一个字符“x”次

时间:2015-03-18 15:01:47

标签: batch-file repeat

我正在尝试创建一个使用用户输入创建以下内容的批处理文件。

  1. 要创建的记录数
  2. 将文本文件另存为
  3. 的文件名
  4. 使用什么字符
  5. 在选项3中重复字符的次数
  6. 我是批量制作的自学新手,但我确实理解了一些...... 以下是我所拥有的,我知道它是不正确的,也许它只是向后...我可能需要设置写入内容,在文本中的一行重复多少次,然后重复步骤的数量记录在文件中。 最终结果如下所示:

    0000000000  0000  000000000
    0000000000  0000  000000000
    

    等等用户输入的总记录量... %column1bytecount %% column2bytecount %% column3bytecount%REPEAT THIS%howmanyrecords%

    以下不起作用。 (我必须要近)

    @Echo Off
    
    Set /P howmanyrecords=How Many Records Would You Like To Produce: 
    set /P savefilename=Please Type Name Of Sample Pin File To Save: 
    Set /P charactertowrite=What Character Would You Like To Use (typically 0 is used): 
    Set /P Column1bytecount=How Many Bytes In Column 1: 
    
    
    for /L %%A in (1,1,%howmanyrecords%) do (
    echo 0000000000  0000  00000000 >> %savefilename%
    

2 个答案:

答案 0 :(得分:3)

@ECHO OFF
SETLOCAL

Set /P howmanyrecords=How Many Records Would You Like To Produce: 
set /P savefilename=Please Type Name Of Sample Pin File To Save: 
Set /P charactertowrite=What Character Would You Like To Use (typically 0 is used): 
Set /P Columnbytecount=How Many Bytes In Columns (separate by commas) : 

SET "line="
CALL :setline %columnbytecount%

for /l %%i in (1,1,%howmanyrecords%) do (
 >>%savefilename% echo %line%
)

GOTO :EOF

:setline
SET "thiscolumn=%1"
IF NOT DEFINED thiscolumn SET "line=%line:~0,-2%"&GOTO :EOF 
:buildloop
SET "line=%line%%charactertowrite%"
SET /a thiscolumn-=1
IF %thiscolumn% neq 0 GOTO buildloop
:: add separator - 2 spaces (not specified)
SET "line=%line%  "
SHIFT
GOTO setline

这应该可以完成工作。

我稍微改变了规格。您可以将列作为一系列数字输入,以逗号(或空格)分隔。例如,您可以输入10,4,9

该过程在line中构建输出行,然后将该行写入所需的次数。请注意,您不能在块语句(带括号的系列语句)中添加标签

setline子例程接受指定为columnbytecount的参数列表。它将第一个参数放入thiscolumn并将指定的字符添加到line这个次数。当计数达到零时,它会添加两个空格(您不要指定间距是什么,但它看起来像两个空格)和shifts参数列表,以便原始的第二个参数成为第一个。

重复该操作,直到参数列表用完为止,然后删除最后两个字符(当最后一列完成时添加了两个空格)通过到达文件结尾从子例程中退出。

子串语法是

%var:~start,length% if start >=0 and length >0
%var:~start,endpos% if start >=0 and endpos <0
%var:~start,length% if start <0 and length >0
%var:~start,endpos% if start <0 and endpos <0
  • 长度/ endpos可以省略。如果省略,子字符串是&#34;从开始位置到结束&#34;
  • 对于%模式,
  • !可能为delayedexpansion

答案 1 :(得分:1)

我认为这可以做你想做的事。

@Echo Off
setlocal enabledelayedexpansion

Set /P "howmanyrecords=How Many Records Would You Like To Produce: "
Set /P "savefilename=Please Type Name Of Sample Pin File To Save: "
Set /P "charactertowrite=What Character Would You Like To Use (typically 0 is used): "
Set /P "column1bytecount=How Many Bytes In Column 1: "

for /l %%i in (1,1,%howmanyrecords%) do (
    set line=
    for /l %%j in (1,1,%column1bytecount%) do (
        set line=!line!!charactertowrite!
    )

    echo !line!>> !savefilename!
)