简体   繁体   English

批处理文件可从文本文件中读取“ N”个字符

[英]Batch File to read 'N' characters from a text file

I have searched this across the net and found many codes for retrieving the entire line from a text or replacing the text with another but not for what i was looking for. 我已经在网上搜索了此代码,发现有很多代码可用于从文本中检索整行或将文本替换为另一行,但不是我想要的。

Using the For loop with the tokens would return on the set (word) separated with spaces. 将For循环与标记一起使用将返回用空格分隔的集合(单词)。

I want to pull only a few characters from the line. 我只想从行中拉出几个字符。

Eg: 12345qwerty67890 例如: 12345qwerty67890

If this on in a text file i want to pull only '12345' and assign it to a variable. 如果在文本文件中打开,我只想拉'12345'并将其分配给变量。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

set HELP SET and try the following to get you started 设置“ 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!
)

At the command prompt, do help set . 在命令提示符下,执行help set There you will find more information about the set command than you would ever want to know. 在这里,您会找到比您想知道的更多有关set命令的信息。 The part you are interested in says: 您感兴趣的部分说:

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.

Of course, in order to use that kind of stuff with the for loop variable, you need to first get acquainted with the very peculiar way in which variables are expanded in Windows NT batch files. 当然,为了将这种东西与for循环变量一起使用,您需要首先熟悉在Windows NT批处理文件中扩展变量的非常特殊的方式。 Let me know if you have problems with that, and I can add more information. 如果您对此有疑问,请告诉我,我可以添加更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM