Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 要读取的批处理文件';N';文本文件中的字符_Windows_Batch File_Character - Fatal编程技术网

Windows 要读取的批处理文件';N';文本文件中的字符

Windows 要读取的批处理文件';N';文本文件中的字符,windows,batch-file,character,Windows,Batch File,Character,我已经在网上搜索过了,找到了很多代码,可以从一个文本中检索整行内容,或者用另一个文本替换文本,但是没有找到我要找的内容 使用带有标记的For循环将返回用空格分隔的集合(单词) 我只想从这行中抽出几个字符 例如:12345qwerty67890 如果在文本文件中启用此选项,我只希望提取“12345”并将其分配给变量 非常感谢您提供的任何帮助。设置帮助设置,然后尝试以下步骤开始 @echo off setlocal enabledelayedexpansion for /f "tokens=*" %

我已经在网上搜索过了,找到了很多代码,可以从一个文本中检索整行内容,或者用另一个文本替换文本,但是没有找到我要找的内容

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

我只想从这行中抽出几个字符

例如:
12345qwerty67890

如果在文本文件中启用此选项,我只希望提取“12345”并将其分配给变量


非常感谢您提供的任何帮助。

设置
帮助设置
,然后尝试以下步骤开始

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

在命令提示下,执行帮助设置。在那里,您将发现有关
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批处理文件中展开变量的特殊方式。如果您对此有任何问题,请告诉我,我可以添加更多信息。

对不起,我不明白您在此处拆分的标准是什么。他想提取特定数量的字符。我添加了“windows”标记,因为这可能就是您的意思。谢谢迈克,我会尝试一下,让您知道,@PA.比我快了44秒。他提供了一个完整的例子,说明了我提到的变量扩展的特点。所以,他值得表扬。你也是,因为你花时间重新获得职位,给了我打败你的机会:)嗨,爸,我试过这个,但是%chars%没有任何回报第%行返回整个字符串,我猜是chars=!线:~0,5!行有问题吗?它只是给我'Echo在上'C:\>setlocal enabledelayedexpansion C:\>for/F“tokens=1”%a in(1.txt)do(set line=%a set chars=~0,3)C:\>(set line=12345BLRPN0W01085 set chars=~0,3)C:\>Echo在上。C:\>暂停按任意键继续。这是它尝试的代码…setlocal enabledelayedexpansion for/f“tokens=1”%%a in(1.txt)do(set line=%%a set chars=%line:~0,3%)ECHO%chars%它工作了i put chars=!线:~0,5!在下一行:)。。。谢谢