问题描述
我有code以下费用相加得到每个文件的第一个标记,但它不工作。请让我知道什么是错的,也是我怎么可以单独取线相互令牌。
I have the following pice of code to get the first token of each file but it does not work. Please let me know what is wrong and also How I can fetch each other token of the line individually.
我的制表符分隔txt文件是一样的东西如下:
MY tab separated txt file is something like below:
ABC 1000 GHY_GGG
ADD 2000
ABCSS 3000 JJY_GGG
我的code以下:
My code below:
for /f "tokens=* usebackq delims= " %%a in ("%TraderWorkspaceFile%") do (
set line=%%a
call :processToken
)
goto :eof
:processToken
for /f "tokens=1 delims= " %%f in ("%line%") do (
echo Got one token: %%f
)
goto :eof
谢谢!
推荐答案
在FOR / F命令来读取的行的文件,并在分离他们的标记的依照令牌= delims =选项。默认情况下,令牌是指刚刚在该行的第一个标记和delims包括空格和制表符(如果不给的话)。本作的命令:
The FOR /F command read lines of the file and separate they in tokens in accordance with "tokens= delims=" option. By default, "tokens" refer to just the first token in the line and "delims" include spaces and tabs (if they are not given). This FOR command:
for /f "usebackq" %%a in ("%TraderWorkspaceFile%") do echo %%a
会显示:
ABC
ADD
ABCSS
,而
for /f "tokens=2 usebackq" %%a in ("%TraderWorkspaceFile%") do echo %%a
会显示:
1000
2000
3000
请注意,delims =是一样的默认值,这样是没用的,而是delims =(无delims)是中第一个标记封闭的把戏整行。
Please note that "delims= " is the same as the default value, so is useless, but "delims=" (no delims) is a trick that encloses in the first token the entire line.
for /f "usebackq delims=" %%a in ("%TraderWorkspaceFile%") do (
set line=%%a
call :processToken
)
goto :eof
:processToken
for /f "tokens=1 delims= " %%f in ("%line%") do (
echo Got one token: %%f
)
goto :eof
会正确显示每行的第一个记号的 的,虽然:
:processToken
for /f %%f in ("%line%") do (
echo Got one token: %%f
)
goto :eof
会做同样的事情...
would do exactly the same thing...
您还可以得到个人空间或选项卡分隔的标记通过子程序的参数是这样的:
You may get also individual space-or-tab-separated tokens via the parameters of the subroutine this way:
:processLine
echo First token: %1, second token: %2, third one: %3
goto :eof
这必须调用这种方式(而不是调用:processToken):
that must be called this way (instead of call :processToken):
call :processLine %line%
这篇关于在批处理文件分隔标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!