批处理文件读取以分号开始的.tmp文件的最后一行

3
大家好,我有一个奇怪的问题想问你们。
我有这个 .tmp 文件。
M l FNT;yriad_b
M l FNT;arial_bo
mm
zO
J
O R,P
H50,5,T
D -3.0,2.2

F98;arial_bo
F99;yriad_b
Sl1;0.0,0.0,100,100,100
P -1.8

T:Line01;6.5,6.5,0,98,pt23;XXX
G 0,8.5,0;L:70.5,0.7
T:Line04;5.5,14.2,0,99,pt18,q100;XXXX
T:Line05;5.5,18.2,0,99,pt9,q100;XXXX
T:Line88;5.5,26.0,0,99,pt21,q100;XXXX
T:Line08;5.5,68.0,0,99,pt21,q100;XXXX
B 5.5,70,9,code128, 7,.4;XXXX
T:Line99;5.5,61.8,0,99,pt11,q100;XXXX
B 5.5,85,5,code128, 7,.25;XXXX
T:Line09;5.5,82.8,0,99,pt17,q100;XXXX
T:Line10;5.5,33.3,0,99,pt17,q100;XXXX
B 5.5,35,2,code128, 7,.3;XXXX
T:Line11;5.5,48.1,0,99,pt18,q100;XX
T:Line19;16.0,48.1,0,99,pt18,q100;XX

B 5.5,50.0,0,code128, 7.0,.3;8


A 1







;ABC123

我需要获取最后一行的精确内容ABC123,不包含;字符。如果没有;在ABC123之前则下面的批处理脚本可以正常工作。目前我不知道为什么批处理会跳过以;字符开头的行。

批处理脚本。

for /f "delims=" %%x in (c:\print\pack2.tmp) do set Build=%%x

有人能帮我吗?

谢谢。

3个回答

3

它跳过以分号(;)开头的行,因为这是默认的行尾字符。有关详细信息,请参见FOR /?

为了避免这种情况,您可以将行尾字符更改为其他字符,例如

for /f "EOL=Y delims=" %%x in (c:\print\pack2.tmp) do set Build=%%x

如果您知道某个特定字符不能出现在这个位置,则可以选择它。 或者您可以使用findstr将文件封闭起来,使每一行都以行号开头(然后FOR/F也可以抓取空行)。
setlocal EnableDelayedExpansion
for /f "delims=" %%L in ('findstr /n "^" c:\print\pack2.tmp') do (
   set "line=%%L"
   set "Build=!line:*:=!"
)

谢谢,是的我的问题已经解决。我不知道 EOL 选项。 - LYR

1
由于for /F循环的eol选项默认为;字符,因此该行被跳过。
delims选项不同,您不能简单地通过声明选项字符串"eol="来禁用eol选项,因为这将将其设置为"字符。像"eol= delims="这样的字符串也无法工作,因为这会将SPACE作为eol字符。
有几种可能性可以禁用eol选项:
  1. Hiding it behind the delims option:

    for /F "usebackq eol=, delims=," %%L in ("file.txt") do set "Build=%%L"
    

    Specifying an eol character to one of the given delims characters disables eol as delims seems to have the higher priority to for /F. The order of eol and delims does not matter.

  2. Using the odd unquoted syntax:

    for /F usebackq^ delims^=^ eol^= %%L in ("file.txt") do set "Build=%%L"
    

    Every token separator (SPACE, TAB, ,, ;, = and also the non-break space 0xFF) must be escaped by preceding ^. Here, eol must be specified as the last option. The unescaped SPACE is not taken as the eol character, because it is consumed by the parser to separate the option string from the for variable reference %%L.

  3. Using the odd line-break syntax:

    for /F usebackq^ eol^=^
    
    delims^= %%L in ("file.txt") do set "Build=%%L"
    

    The empty line is mandatory here. The ^ after the eol^= part constitutes line continuation, so the first line break (CR+LF, carriage-return line-feed sequence) is ignored. The next character, an LF (the CR is already skipped by the parser when reading the line of code), is taken literally as the eol character. Since such cannot occur within lines of text as they mark the end of line, the eol option is finally disabled.


0

注释

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q40888302.txt"
FOR /f "usebackqtokens=1*eol=;delims=; " %%a IN ("%filename1%") DO (
SET var=%%~a
)
ECHO "%var%"
GOTO :EOF

您需要根据自己的情况更改sourcedir的设置。

我使用了一个名为q40888302.txt的文件来测试您的数据。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接