首页 文章

批处理文件从文本文件中读取'N'个字符

提问于
浏览
2

我在网上搜索了这个,发现了很多代码,用于从文本中检索整行,或者用另一个替换文本,但不是我想要的 .

使用带有标记的For循环将返回以空格分隔的集合(单词) .

我想从线上只拉几个字符 .

例如: 12345qwerty67890

如果在文本文件中打开,我想只拉“12345”并将其分配给变量 .

任何帮助是极大的赞赏 .

2 回答

  • 5

    在命令提示符下,执行 help set . 在那里,您将找到有关 set 命令的更多信息,而不是您想知道的 . 你感兴趣的部分说:

    May also specify substrings for an expansion.
    
        %PATH:~10,5%
    
    would expand the PATH environment variable, and then use only the 5
    characters that begin at the 11th (offset 10) character of the expanded
    result.  If the length is not specified, then it defaults to the
    remainder of the variable value.  If either number (offset or length) is
    negative, then the number used is the length of the environment variable
    value added to the offset or length specified.
    

    当然,为了将这种东西与 for 循环变量一起使用,您需要首先了解在Windows NT批处理文件中扩展变量的非常特殊的方式 . 如果您遇到问题,请告诉我,我可以添加更多信息 .

  • 2

    设置 HELP SET 并尝试以下操作来帮助您入门

    @echo off
    setlocal enabledelayedexpansion
    for /f "tokens=*" %%a in (sample.txt) do (
      set line=%%a
      set chars=!line:~0,5!
      echo !chars! --- !line!
    )
    

相关问题