2011-12-21 50 views
2

我已搜查这个跨网,发现很多代码从文本检索整行或与另一个替换文本,但没有什么我一直在寻找读“N”字。批处理文件从一个文本文件

使用对于令牌环将返回与空格隔开的一组(字)。

我想从线拉只有几个字符。

如:12345qwerty67890

如果在一个文本文件,我想只拉“12345”,并将其分配给一个变量。

任何帮助是极大的赞赏。

+0

对不起,我不明白什么是你的分割标准在这里 – adarshr

+0

他要提取字符的某些预定数量。 –

+0

我添加了“windows”标签,因为这可能意味着什么。 –

回答

5

设置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! 
) 
+0

嗨PA,我试过这个,但%字符%不返回任何东西。 %line%返回整个字符串,我猜chars =!line:〜0,5!线路有问题? –

+0

它只是给我'Echo is'' –

+0

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 ECHO开启。 C:\>暂停 按任意键继续。 。 。 –

2

在命令提示符下,执行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批处理文件。如果您遇到问题,请告知我,我可以添加更多信息。

+0

谢谢迈克,生病试试这个,让你知道 –

+0

那么,@PA。打败了我44秒。 –

+0

他提供了一个完整的例子,关注我提到的有关可变扩展的特性。所以,他值得信任。 –

相关问题